Hibok
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

73 rindas
1.7 KiB

  1. import 'package:flutter/material.dart';
  2. class ImgFrameAnimation extends StatefulWidget {
  3. final List<Widget> imgList;
  4. final int interval;
  5. final bool isRepeat;
  6. ImgFrameAnimation(
  7. {this.imgList,
  8. this.interval = 200,
  9. this.isRepeat = true,
  10. });
  11. @override
  12. _ImgFrameAnimationState createState() => _ImgFrameAnimationState();
  13. }
  14. class _ImgFrameAnimationState extends State<ImgFrameAnimation>
  15. with SingleTickerProviderStateMixin {
  16. Animation<double> _animation;
  17. AnimationController _controller;
  18. int interval = 1000;
  19. @override
  20. void initState() {
  21. super.initState();
  22. interval = widget.interval;
  23. final int itemCount = widget.imgList.length;
  24. final int maxTime = interval * itemCount;
  25. // 启动动画controller
  26. _controller = new AnimationController(
  27. duration: Duration(milliseconds: maxTime), vsync: this);
  28. _controller.addStatusListener((AnimationStatus status) {
  29. if (status == AnimationStatus.completed) {
  30. _controller.forward(from: 0.0); // 完成后重新开始
  31. }
  32. });
  33. _animation = new Tween<double>(begin: 0, end: itemCount.toDouble())
  34. .animate(_controller)
  35. ..addListener(() {
  36. setState(() {});
  37. });
  38. _controller.forward();
  39. }
  40. @override
  41. void dispose() {
  42. _controller.stop();
  43. _controller.dispose();
  44. print('frame animation dipos');
  45. super.dispose();
  46. }
  47. @override
  48. Widget build(BuildContext context) {
  49. int index = _animation.value.floor() % widget.imgList.length;
  50. return IndexedStack(
  51. alignment: Alignment.centerRight,
  52. children: widget.imgList,
  53. index: index,
  54. );
  55. }
  56. }