import 'package:flutter/material.dart'; class ImgFrameAnimation extends StatefulWidget { final List imgList; final int interval; final bool isRepeat; ImgFrameAnimation( {this.imgList, this.interval = 200, this.isRepeat = true, }); @override _ImgFrameAnimationState createState() => _ImgFrameAnimationState(); } class _ImgFrameAnimationState extends State with SingleTickerProviderStateMixin { Animation _animation; AnimationController _controller; int interval = 1000; @override void initState() { super.initState(); interval = widget.interval; final int itemCount = widget.imgList.length; final int maxTime = interval * itemCount; // 启动动画controller _controller = new AnimationController( duration: Duration(milliseconds: maxTime), vsync: this); _controller.addStatusListener((AnimationStatus status) { if (status == AnimationStatus.completed) { _controller.forward(from: 0.0); // 完成后重新开始 } }); _animation = new Tween(begin: 0, end: itemCount.toDouble()) .animate(_controller) ..addListener(() { setState(() {}); }); _controller.forward(); } @override void dispose() { _controller.stop(); _controller.dispose(); print('frame animation dipos'); super.dispose(); } @override Widget build(BuildContext context) { int index = _animation.value.floor() % widget.imgList.length; return IndexedStack( alignment: Alignment.centerRight, children: widget.imgList, index: index, ); } }