Skip to content

Commit 901bd97

Browse files
committed
fix: disable GIF looping to mitigate high CPU & GPU usage
1 parent 9d60d9a commit 901bd97

File tree

3 files changed

+33
-11
lines changed

3 files changed

+33
-11
lines changed

lib/components/network_image.dart

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import 'package:cached_network_image/cached_network_image.dart';
2+
import 'package:gif_view/gif_view.dart';
23
import 'package:flutter/material.dart';
34

5+
import '../constants/constants.dart';
6+
47
Widget networkImage(
58
String imageUrl, {
69
BoxFit? fit,
@@ -30,17 +33,27 @@ Widget networkImage(
3033
);
3134
}
3235

33-
return CachedNetworkImage(
34-
fit: fit,
35-
width: width,
36-
height: height,
37-
imageUrl: imageUrl,
38-
imageBuilder: imageBuilder,
39-
placeholder: (context, url) => placeHolder(context),
40-
errorWidget: (context, url, error) => placeHolder(context),
41-
fadeOutDuration: const Duration(milliseconds: 200),
42-
fadeInDuration: const Duration(milliseconds: 200),
43-
);
36+
return imageUrl.endsWith(Constants.SUFFIX_GIF)
37+
? GifView.network(
38+
imageUrl,
39+
fit: fit,
40+
width: width,
41+
height: height,
42+
errorBuilder: (context, url, error) => placeHolder(context),
43+
fadeDuration: const Duration(milliseconds: 200),
44+
loop: false,
45+
)
46+
: CachedNetworkImage(
47+
fit: fit,
48+
width: width,
49+
height: height,
50+
imageUrl: imageUrl,
51+
imageBuilder: imageBuilder,
52+
placeholder: (context, url) => placeHolder(context),
53+
errorWidget: (context, url, error) => placeHolder(context),
54+
fadeOutDuration: const Duration(milliseconds: 200),
55+
fadeInDuration: const Duration(milliseconds: 200),
56+
);
4457
}
4558

4659
Widget clipNetworkImage(

pubspec.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,14 @@ packages:
565565
url: "https://pub.flutter-io.cn"
566566
source: hosted
567567
version: "4.7.2"
568+
gif_view:
569+
dependency: "direct main"
570+
description:
571+
name: gif_view
572+
sha256: "4c7e17c134719531dabab54af121e4600d63283f56f3aff57c16db54766b67bc"
573+
url: "https://pub.flutter-io.cn"
574+
source: hosted
575+
version: "1.0.3"
568576
glob:
569577
dependency: transitive
570578
description:

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ dependencies:
6969
url_launcher: ^6.3.0
7070
uuid: ^4.4.2
7171
window_manager: ^0.5.0
72+
gif_view: ^1.0.3
7273

7374
dev_dependencies:
7475
flutter_test:

0 commit comments

Comments
 (0)