首页 > 解决方案 > Flutter Animation-start 在第二次运行时不起作用

问题描述

在我的应用程序/游戏中,我有这个风扇,当你点击它时它会转身,然后一直持续到你再次点击。它工作正常,与curves.easein等。但是当你尝试第二次运行它(或任何高于1的数字)时,它会跳过最初的曲线开始,当我在2秒后重复运行时首先开始(所以它开始时没有所需的曲线)

按钮代码:

GestureDetector(
                       onTap: () {
                         Duration startStopTime = Duration(seconds: 3);
                         if(_animating) {
                           _rotationAnimationController.animateBack(1, duration: startStopTime, curve: Curves.easeOut);
                           setState(() {});
                         } else {
                           _rotationAnimationController.animateTo(1, duration: startStopTime, curve: Curves.easeInCubic);
                           Future.delayed(startStopTime, () {
                             _rotationAnimationController.repeat();
                           });
                         }
                         setState(() {
                           _animating = !_animating;
                         });
                       },
                       child: Padding(
                         padding: const EdgeInsets.only(top: 70),
                         child: Container(
                           color: Colors.blue[300],
                           width: MediaQuery.of(context).size.width/6*5,
                           height: MediaQuery.of(context).size.width/6*5,
                         ),
                       ),
                     ),

风扇小部件:

IgnorePointer(
                       child: Container(
                         child: Transform.rotate(
                           angle: _animation.value,
                             child: Image.asset('assets/FanArms.png')
                         ),
                       ),
                   ),

动画:

 var _animating = false;

  AnimationController _rotationAnimationController;
  Animation<double> _animation;

  @override
  void initState() {
    super.initState();
    _rotationAnimationController = AnimationController(
      duration: Duration(milliseconds: 2000),
      vsync: this,
    );
    _animation =
        Tween<double>(begin: 0, end: 4 * pi ).animate(_rotationAnimationController)
      ..addListener(() {
        setState(() {});
      });
  }

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

这是我的应用程序中我的粉丝的视频 https://imgur.com/a/wtxFNvL

  • 就在风扇停止时,我再次单击它,大约需要 3 秒钟才能启动(而且没有平滑曲线)

是的,我的应用程序很奇怪,我知道 :)

Thaks allready :)

标签: androidflutteranimationcurve

解决方案


推荐阅读