gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 13:28:55 25 4
gpt4 key购买 nike

我想按需更新计时器,但我不知道如何去做。我想检查在更改持续时间之前更新小部件时是否满足某些条件。到目前为止,我已经尝试使用“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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com