import 'package:flutter/material.dart';

class WhiteOpacityAnim extends StatefulWidget {
  final Widget child;
  final animFinishCallback;
  WhiteOpacityAnim(
      {Key key, @required this.child, @required this.animFinishCallback});
  @override
  _WhiteOpacityAnimState createState() => _WhiteOpacityAnimState();
}

class _WhiteOpacityAnimState extends State<WhiteOpacityAnim>
    with SingleTickerProviderStateMixin {
  Animation<double> _animation;
  AnimationController _controller;

  @override
  void initState() {
    super.initState();
    // 启动动画controller
    _controller = new AnimationController(
        duration: Duration(milliseconds: 1000), vsync: this);
    _controller.addStatusListener((AnimationStatus status) {
      setState(() {});
      if (status == AnimationStatus.completed) {
        widget.animFinishCallback();
      }
    });

    _animation = new Tween<double>(begin: 1, end: 0).animate(_controller)
      ..addListener(() {
        setState(() {});
      });

    _controller.forward();
  }

  @override
  void dispose() {
    _controller.stop();
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: widget.child,
      color: Colors.white.withOpacity(_animation.value),
    );
  }
}
