animation - 您如何更改 didUpdateWidget 上 flutter 动画的持续时间?

标签 animation flutter duration

我想按需更新计时器,但我不知道如何去做。我想检查在更改持续时间之前更新小部件时是否满足某些条件。到目前为止,我已经尝试使用“didUpdateWidget”函数,但我收到了一个代码错误。当我将 mixin 更改为 TickerProviderStateMixin 时,持续时间不会更新。

class _ProgressBarState extends State<ProgressBar>
    with SingleTickerProviderStateMixin {

  int _duration;
  int _position;
  bool _isPaused;

  Animation animation;
  AnimationController animationController;

  @override
  void initState() {
    super.initState();
    _duration = widget.duration;
    _position = widget.position;
    _isPaused = widget.isPaused;
    animationController = AnimationController(
    duration: Duration(milliseconds: _duration), vsync: this);
    animation = Tween(begin: 0.0, end: 1.0).animate(animationController);
  }

  @override
    void didUpdateWidget(ProgressBar oldWidget) {
      // TODO: implement didUpdateWidget
      setState(() {
        _duration = widget.duration;
        _position = widget.position;
        _isPaused = widget.isPaused;
      });

      updateController(oldWidget);
      super.didUpdateWidget(oldWidget);
    }


  void updateController(ProgressBar oldWidget){
    if(oldWidget.duration != _duration){
      animationController.dispose();
      animationController = AnimationController(duration: Duration(milliseconds: _duration), vsync:this);
    }
    if(_isPaused){
      animationController.stop();
    } else{
        animationController.forward(from: _position/_duration);
      }
  }
//...
}

最佳答案

仔细看了文档,发现直接修改AnimationController的属性就可以了。哈哈...

animationController.duration = Duration(milliseconds: _duration)

关于animation - 您如何更改 didUpdateWidget 上 flutter 动画的持续时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53162032/

相关文章:

flutter - Flutter图像选择器不会在Android 10中显示

html - 悬停时背景图像和颜色随持续时间变化

duration - 软件估算速度和准确性

cocoa - 对图层支持的 View 进行动画处理时缩放图层内容

html - 如何将使用 Adob​​e Animate CC 生成的 HTML5 动画添加到 Ionic 2 项目页面?

javascript - 从过滤列表中删除时的 ng-repeat 动画

firebase - 在同一字段上存储多个查询

Flutter 离线认证

css - 动画时长?

python - 您自己的 GUI 中的 Matplotlib 动画