initState method

  1. @override
void initState()

Implementation

@override
void initState() {
  super.initState();
  //default duration
  if (widget.duration != null) {
    duration = widget.duration;
  }

  //default beginColor
  if (widget.beginColor != null) {
    beginColor = widget.beginColor;
  } else {
    if (widget.style != null && widget.style!.inherit) {
      beginColor = widget.style!.color;
    }
  }

  final endColor = widget.endColor ?? Colors.transparent;
  final times = widget.times ?? 0;

  _controller = AnimationController(vsync: this, duration: duration);
  _colorAnimation = ColorTween(begin: beginColor, end: endColor)
      .animate(CurvedAnimation(parent: _controller, curve: Curves.linear));

  _controller.addStatusListener((status) {
    if (status == AnimationStatus.completed) {
      _counter++;
      _controller.reverse();
      if (_counter >= times && times > 0) {
        _endTween();
      }
    } else if (status == AnimationStatus.dismissed) {
      _controller.forward();
    }
    setState(() {});
  });

  _controller.forward();
}