Flutter如何知道GIF是否做了动画而不循环

rxztt3cl  于 2023-10-22  发布在  Flutter
关注(0)|答案(3)|浏览(176)

如何知道GIF是否已完成播放?我想建立另一个小部件,并呼吁一个功能,但GIF做动画一次后。有没有办法做到这一点?

Scaffold(
  body: Container(
    constraints: const BoxConstraints.expand(),
    child: const Image(
      image: AssetImage(
        "images/splas_screen.GIF",
      ),
      fit: BoxFit.fill,
      
    ),
  ),
);

这就是我目前显示GIF的方式,因为我仍然不知道如何只播放一次GIF而不循环并在之后调用函数。

js4nwp54

js4nwp541#

您可以使用flutter_gif并将repeat属性设置为ImageRepeat.noRepeat,如下所示:

GifImage(image:AssetImage("images/splas_screen.GIF"), controller: controller,repeat:ImageRepeat.noRepeat ),

这是你需要的一个完整的例子:

class TestAnimation extends StatefulWidget {
  const TestAnimation({super.key});

  @override
  State<TestAnimation> createState() => _TestAnimationState();
}

class _TestAnimationState extends State<TestAnimation>
    with TickerProviderStateMixin {
  late FlutterGifController controller;
  @override
  void initState() {
    super.initState();
    controller = FlutterGifController(vsync: this);
    controller.animateTo(10, duration: Duration(milliseconds: 300)); // Note that animate to your last frame of your animation, here mine is 10.
    controller.addListener(() {
      if (controller.isCompleted) {
        print("compleate");
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Testing")),
      body: Column(
        children: [
          GifImage(
            image: AssetImage("assets/images/Pivot_Wave.gif"),
            controller: controller,
            repeat: ImageRepeat.repeat,
          ),
        ],
      ),
    );
  }
}
vptzau2j

vptzau2j2#

尝试使用此软件包
flutter_gif:联系我们

相关问题