frame_image.dart 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import 'package:flutter/material.dart';
  2. // ignore: must_be_immutable
  3. class FrameAnimationImage extends StatefulWidget {
  4. List<String> assetList;
  5. double width,height;
  6. int interval = 200;
  7. FrameAnimationImage({ Key? key,required this.assetList,required this.width,required this.height, required this.interval }) : super(key: key);
  8. @override
  9. State<FrameAnimationImage> createState() => _FrameAnimationImageState();
  10. }
  11. class _FrameAnimationImageState extends State<FrameAnimationImage> with SingleTickerProviderStateMixin{
  12. late Animation animation;
  13. late AnimationController controller;
  14. @override
  15. void initState() {
  16. int imageCount = widget.assetList.length;
  17. int maxTime = widget.interval*imageCount;
  18. controller = AnimationController(vsync: this,duration: Duration(milliseconds: maxTime));
  19. controller.addListener(() {
  20. setState(() {
  21. });
  22. });
  23. animation = Tween(begin: 0.0,end: imageCount.toDouble()).animate(controller)..addListener(() {setState(() {
  24. });});
  25. controller.repeat();
  26. super.initState();
  27. }
  28. @override
  29. void dispose() {
  30. controller.dispose();
  31. super.dispose();
  32. }
  33. @override
  34. Widget build(BuildContext context) {
  35. int ix = animation.value.floor()%widget.assetList.length;
  36. List<Widget> images = [];
  37. for (int i=0;i<widget.assetList.length;i++){
  38. if (i!=ix){
  39. images.add(Image.asset(widget.assetList[i],width: 0,height: 0,));
  40. }
  41. }
  42. images.add(Image.asset(widget.assetList[ix],width: widget.width,height: widget.height,));
  43. return Stack (
  44. alignment: Alignment.center,
  45. children: images,
  46. );
  47. }
  48. }