import 'package:chat/data/constants.dart';
import 'package:flutter/material.dart';

class FrameAnimation extends StatefulWidget {
  final List<int> codeList;
  final double size;
  final Color color;
  final int interval;
  final bool isRepeat;

  FrameAnimation(
      {this.codeList,
      this.size,
      this.interval = 200,
      this.isRepeat = true, this.color,
     });

  @override
  _FrameAnimationState createState() => _FrameAnimationState();
}

class _FrameAnimationState extends State<FrameAnimation>
    with SingleTickerProviderStateMixin {
  Animation<double> _animation;
  AnimationController _controller;
  int interval = 1000;
  List<Widget> frames = [];

  @override
  void initState() {
    super.initState();

    interval = widget.interval;

    final int iconCount = widget.codeList.length;
    final int maxTime = interval * iconCount;

    for (var i = 0; i < iconCount; i++) {
      frames.add(Icon(
          IconData(widget.codeList[i], fontFamily: Constants.IconFontFamily),
          size: widget.size,
          color:widget.color));
    }

    // 启动动画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<double>(begin: 0, end: iconCount.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.codeList.length;

    return IndexedStack(
      alignment: Alignment.centerRight,
      children: frames,
      index: index,
    );
  }
}
