[英]Display animated gif in flutter, but don't animate
I have an animated gif that I am displaying in Flutter.我有一个在 Flutter 中显示的动画 gif。 I now have the requirement to display that gif in another place but not animate it.
我现在需要在另一个地方显示该 gif,但不对其进行动画处理。 Is there a way I can do this, without having to include a duplicate non-aniamted graphic?
有没有办法可以做到这一点,而不必包含重复的非动画图形?
Thanks!谢谢!
You could use flutter_gifimage package:您可以使用flutter_gifimage package:
GifController controller = GifController(vsync: this);
...
GifImage(controller: controller,
image: AssetImage("images/animate.gif"))
...
controller.stop();
So, finally I wrote code that shows animated GIFs without animation.所以,最后我编写了代码来显示没有 animation 的动画 GIF。
Use:利用:
StillGIF.asset(
e,
height: 100.0,
width: 100.0,
)
Implementation:执行:
class StillGIF extends StatefulWidget {
final ImageProvider image;
final double width;
final double height;
StillGIF({
Key? key,
required this.image,
required this.width,
required this.height,
}) : super(key: key);
factory StillGIF.asset(
String image, {
Key? key,
required double width,
required double height,
}) =>
StillGIF(
key: key,
image: AssetImage(image),
width: width,
height: height,
);
factory StillGIF.file(
String image, {
Key? key,
required double width,
required double height,
}) =>
StillGIF(
key: key,
image: FileImage(File(image)),
width: width,
height: height,
);
factory StillGIF.network(
String image, {
Key? key,
required double width,
required double height,
}) =>
StillGIF(
key: key,
image: NetworkImage(image),
width: width,
height: height,
);
@override
State<StillGIF> createState() => _StillGIFState();
}
class _StillGIFState extends State<StillGIF> {
RawImage? image;
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) async {
Uint8List? data;
if (widget.image is NetworkImage) {
final resolved = Uri.base.resolve((widget.image as NetworkImage).url);
final request = await HttpClient().getUrl(resolved);
final HttpClientResponse response = await request.close();
data = await consolidateHttpClientResponseBytes(response);
} else if (widget.image is AssetImage) {
final key =
await (widget.image as AssetImage).obtainKey(ImageConfiguration());
data = (await key.bundle.load(key.name)).buffer.asUint8List();
} else if (widget.image is FileImage) {
data = await (widget.image as FileImage).file.readAsBytes();
}
final codec =
await PaintingBinding.instance.instantiateImageCodecFromBuffer(
await ImmutableBuffer.fromUint8List(
data!.buffer.asUint8List(),
),
);
FrameInfo frame = await codec.getNextFrame();
setState(() {
image = RawImage(
image: frame.image,
height: widget.height,
width: widget.width,
fit: BoxFit.cover,
);
});
});
}
@override
Widget build(BuildContext context) {
return image ??
SizedBox(
width: widget.width,
height: widget.height,
);
}
}
You can configure other options like BoxFit
in RawImage
constructor.您可以在
RawImage
构造函数中配置其他选项,例如BoxFit
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.