简体   繁体   English

Flutter 动画图标AnimationController

[英]Flutter animated icon AnimationController

So I got two pages: The first page is my HomePage from where I can access my second page via a button.所以我有两个页面:第一页是我的主页,我可以从那里通过按钮访问我的第二页。 The second page is where I put an animated Icon inside.第二页是我在里面放了一个动画图标的地方。 I'm trying to animate the icon and so far everything works just well.我正在尝试为图标制作动画,到目前为止一切正常。 The icon bounces just like I want it to be but as soon as I go back to my HomePage all of a sudden an error appears and the app doesn't function anymore (I've added a screenshot but I don't really understand what I need to change inside of my code).图标弹跳就像我想要的那样但是当我 go 回到我的主页时突然出现一个错误并且应用程序不再是 function (我添加了一个屏幕截图但我真的不明白是什么我需要更改我的代码内部)。 What can I do to prevent this from happening?我能做些什么来防止这种情况发生?

This is my code:这是我的代码:

class InfoScreen extends StatefulWidget {
  @override
  _InfoScreen createState() => _InfoScreen();
}
@override
class _InfoScreen extends State<InfoScreen> with TickerProviderStateMixin {
  AnimationController _animationController;
  Animation<double> _animation;
  bool _isPlaying = false;

  void _animate() {
    if (_isPlaying)
      _animationController.stop();
    else
      _animationController.forward();
    setState(() {
      _isPlaying = !_isPlaying;
    });
  }

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

    _animationController = AnimationController(
      vsync: this,
      duration: Duration(milliseconds: 450),
    );

    _animation = Tween<double>(begin: 0.85, end: 1.05).animate(
        CurvedAnimation(parent: _animationController, curve: Curves.easeIn));
    _animationController.forward();
    _animation.addStatusListener((status) {
      if (status == AnimationStatus.completed)
        _animationController.reverse();
      else if (status == AnimationStatus.dismissed)
        _animationController.forward();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Info'),
        flexibleSpace: Container(
          decoration: BoxDecoration(
            gradient: LinearGradient(
                colors: [Color(0xffFBD23E), Color(0xffF6BE03)],
                begin: Alignment.topCenter,
                end: Alignment.bottomCenter),
          ),
        ),
      ),
      body: Container(
        constraints: BoxConstraints.expand(),
        decoration: BoxDecoration(
          gradient: LinearGradient(
              colors: [Color(0xffFEFDFD), Color(0xffBDBDB2)],
              begin: Alignment.topLeft,
              end: Alignment.bottomRight),
        ),
        child: ScaleTransition(
              scale: _animation,
              child: Icon(
                Icons.favorite,
                color: Color(0xffF40930),
              ),
            ),
 ],
        ),
      ),
    );
  }
}

在此处输入图像描述

We need to dispose the AnimationController .我们需要处理AnimationController Override the dispose method and dispose the _animationController .覆盖 dispose 方法并处理_animationController

  @override
  void dispose() {
    _animationController.dispose();
    super.dispose();
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM