所以我试图在 Flutter 中创建一个动画,每次用户按下按钮时都需要不同的结果。
我根据Flutter Animations 教程实现了以下代码,并创建了一个函数来更新它。
class _RoulettePageWidgetState extends State<RoulettePageWidget>
with SingleTickerProviderStateMixin {
   Animation<double> _animation;
   Tween<double> _tween;
   AnimationController _animationController;
   int position = 0;
   @override
   void initState() {
      super.initState();
      _animationController =
          AnimationController(duration: Duration(seconds: 2), vsync: this);
      _tween = Tween(begin: 0.0, end: 100.0);
      _animation = _tween.animate(_animationController)
          ..addListener(() {
              setState(() {});
          });
   }
   void setNewPosition(int newPosition) {
      _tween = Tween(
        begin: 0.0,
        end: math.pi*2/25*newPosition);
      _animationController.reset();
      _tween.animate(_animationController);
      _animationController.forward();
   }
   @override
   Widget build(BuildContext context) {
      return Container(
         child: Column(
            children: <Widget>[
               Center(
                  child: Transform.rotate(
                     angle: _animationController.value,
                     child: Icon(
                        Icons.arrow_upward,
                     size: 250.0,
                  ),
               )),
               Expanded(
                  child: Container(),
               ),
               RaisedButton(
                  child: Text('SPIN'),
                  onPressed: () {
                     setState(() {
                        setNewPosition(math.Random().nextInt(25));
                     });
                  },
               )
            ],
         )
      );
   }
}
如您所见,我正在更新_tween's begin:,end:但这似乎并没有改变动画。
那么每次用户按下按钮时我应该怎么做才能创建一个“不同的”动画呢?
总体思路是使动画彼此建立在一个随机的新值之上,例如:
- 第一次旋转:0 -> 10
- 第二次旋转:10 -> 13
- 第三次旋转:13 -> 18
- ... ETC
所以我想知道我是否可以更新动画,或者我应该每次都创建一个新动画?我能想到的另一件事是跟踪位置并每次使用相同的动画(0.0 -> 100.0)作为传输的百分比。
因此,我不会从 10 -> 15 创建一个新动画,而是这样做:
currentValue = 10 + (15-10)/100*_animationController.value