【发布时间】:2021-06-29 15:05:44
【问题描述】:
因此,a 有一个有状态的父级,其值为 timer。该定时器可由用户重新设置。我还有一个带有动画控制器的子状态小部件。父级将timer 值传递给该子级以重置动画。这是一些代码:
class _ParentState extends State<Parent> {
int timer = 20;
void _userInput() {
setState(() {
timer = 20;
}
}
@override
Widget build(BuildContext context) {
return Child(
time: timer
);
}
}
注意:为简单起见,此处已删除
因此,当我的用户触发_userInput 方法时,计时器值会发生变化,但孩子不会。这是我的完整子小部件:
class TimerProgress extends StatefulWidget {
TimerProgress({
Key key,
@required this.time,
@required this.onCompleted
}) : super(key: key);
final int time;
final Function onCompleted;
@override
_TimerProgressState createState() => _TimerProgressState();
}
class _TimerProgressState extends State<TimerProgress> with SingleTickerProviderStateMixin {
AnimationController _controller;
Animation<double> _widthAnimation;
@override
void initState() {
_controller = AnimationController(
duration: Duration(seconds: widget.time),
vsync: this
);
_widthAnimation = Tween<double>(
begin: 200,
end: 0
).animate(CurvedAnimation(
parent: _controller,
curve: Curves.linear
));
_controller.addListener(() {
if (_controller.value == 1) {
widget.onCompleted();
}
});
_controller.forward();
super.initState();
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return ...
}
}
所以这一切都有效,但理想情况下我希望重置动画,这不起作用......
【问题讨论】: