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