onTap事件,我添加了一个动画,并从动画完成中调用了一个对话框,但问题是当我第二次点击按钮并启动动画时,对话框会打开两次,动画完成时和第三次,对话框打开3次,依此类推。
代码:
变量:
double _angle = 0;
double _current = 0;
AnimationController _ctrl;
Animation _ani;
从initState()方法初始化:
var _duration = Duration(milliseconds: 5000);
_ctrl = AnimationController(vsync: this, duration: _duration);
_ani = CurvedAnimation(parent: _ctrl, curve: Curves.fastLinearToSlowEaseIn);
AnimatedBuilder:
AnimatedBuilder(
animation: _ani,
builder: (context, child) {
final _value = _ani.value;
this.valueee=_value ;
print("_value:$_value");
final _angle = _value * this._angle;
return center(
child:GestureDetector(
onTap : startAnimation,
//
//...
),
),
},
),
我是从小部件树中的onTap:事件调用它的:
startAnimation(){
if (!_ctrl.isAnimating) {
var _random = Random().nextDouble();
_angle = 20 + Random().nextInt(5) + _random;
_ctrl.forward(from: 0.0).then((_) {
_current = (_current + _random);
_current = _current - _current ~/ 1;
_ctrl.reset();
//_ctrl.stop();
});
}
_ctrl.addStatusListener((status) {
if(status == AnimationStatus.completed) {
openDialogBox();
}
});
}
答案 0 :(得分:0)
每次调用startAnimation()
方法时,将调用_ctrl.addStatusListener
方法,然后将新的StatusListener添加到animationController。
将_ctrl.addStatusListener
移到_ani = CurvedAnimation(parent: _ctrl, curve: Curves.fastLinearToSlowEaseIn);
下方
像这样:
var _duration = Duration(milliseconds: 5000);
_ctrl = AnimationController(vsync: this, duration: _duration);
_ani = CurvedAnimation(parent: _ctrl, curve: Curves.fastLinearToSlowEaseIn);
_ctrl.addStatusListener((status) {
if(status == AnimationStatus.completed) {
openDialogBox();
}
});
和
startAnimation(){
if (!_ctrl.isAnimating) {
var _random = Random().nextDouble();
_angle = 20 + Random().nextInt(5) + _random;
_ctrl.forward(from: 0.0).then((_) {
_current = (_current + _random);
_current = _current - _current ~/ 1;
_ctrl.reset();
//_ctrl.stop();
});
}
}