flutter 如何停止GIF循环抖动?

a2mppw5e  于 2023-01-02  发布在  Flutter
关注(0)|答案(4)|浏览(305)

如何停止gif循环1次后?它是动画永远。

Image(
  color: Colors.red,
  image: new AssetImage(Icn.splash),
  width: 150.0,
),
hmae6n7t

hmae6n7t1#

解决方案

ttcibm8c

ttcibm8c2#

我希望这个Flutter包能有所帮助;
控制gif动画的flutter插件:https://pub.dev/packages/gif_ani

tp5buhyn

tp5buhyn3#

分两步走:
1.使用https://onlinegiftools.com/change-gif-loop-count和/或https://ezgif.com/loop-count等在线gif编辑工具将gif循环计数从0(无限)编辑为1,然后下载编辑后的gif,这样可以确保gif只播放一次。
1.手动清除图像缓存以确保在用户导航离开和/或使用后退按钮重新访问页面时再次播放gif,假设目标是在用户每次访问页面时播放gif,但只播放一次。
样本代码:

import 'package:flutter/material.dart';

class OneTimeGifLoader extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _OneTimeGifLoaderState();
  }
}

class _OneTimeGifLoaderState extends State<OneTimeGifLoader> {
  late AssetImage myAssetImage;

  @override
  void initState() {
    super.initState();
    myAssetImage = const AssetImage("assets/path/to/my/image.gif");
  }

  @override
  void dispose() {
    super.dispose();
    myAssetImage.evict(); //Evict image cache
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text("One time gif loader"),
        ),
        body: Container(
          width: double.infinity,
          height: double.infinity,
          decoration: BoxDecoration(
            image: DecorationImage(
              image: myAssetImage,
              fit: BoxFit.contain,
            ),
            color: Colors.white,
          ),
        ),
      ),
    );
  }
}
vsdwdz23

vsdwdz234#

此代码:

Image.asset("assets/tasky_logo.gif",
            gaplessPlayback: false, fit: BoxFit.fill)

相关问题