阻止动画立即播放
Prevent animation from playing immediately
我想在点击时提供动画。如果用户点击一个按钮,那么只有动画开始,一段时间后它应该停止。这是我的代码:
AnimationController _animationController;
Animation<double> _animation;
@override
initState(){
_animationController = AnimationController(
vsync: this,
duration: Duration(seconds: 3),
lowerBound: 0.5,
upperBound: 1.0,
);
_animation = CurvedAnimation(parent: _animationController, curve: Curves.bounceInOut);
_animation.addListener(() {
setState(() {});
});
_animationController.forward();
}
@override
dispose(){
_animationController.dispose();
super.dispose();
}
以上代码用于动画。
ScaleTransition(
scale: _animation,
child: Center(
child: InkWell(
onTap: (){
_animation.addStatusListener((status) {
if (status == AnimationStatus.completed) {
_animationController.reverse();
} else if (status == AnimationStatus.dismissed) {
_animationController.forward(from: 0.0);
}
});
},
child: new Image.asset(
'images/like.png'
color: Colors.white,
height: 50.0,
),
),
),
),
我喜欢上面的代码。但是当我 运行 应用程序时,动画会自动启动而无需单击按钮。但我只想在用户点击按钮时提供动画。当用户点击按钮时,只有动画开始,当点击按钮时,动画应该停止。
在我的例子中,动画自动开始。当用户点击按钮时,按钮应该弹起。
发生这种情况是因为您在 initState
:
开始播放动画
_animationController.forward();
删除该行,直到您点击它才会开始。
此外,以下代码是不必要的,因为 Scale
会自行处理其状态:
_animation.addListener(() {
setState(() {});
});
如果你也从你的 initState
方法中删除它,你会没事的。
删除_animationController.forward();从 initState 并将其放入 GestureDetector
的 ontap 方法中
我想在点击时提供动画。如果用户点击一个按钮,那么只有动画开始,一段时间后它应该停止。这是我的代码:
AnimationController _animationController;
Animation<double> _animation;
@override
initState(){
_animationController = AnimationController(
vsync: this,
duration: Duration(seconds: 3),
lowerBound: 0.5,
upperBound: 1.0,
);
_animation = CurvedAnimation(parent: _animationController, curve: Curves.bounceInOut);
_animation.addListener(() {
setState(() {});
});
_animationController.forward();
}
@override
dispose(){
_animationController.dispose();
super.dispose();
}
以上代码用于动画。
ScaleTransition(
scale: _animation,
child: Center(
child: InkWell(
onTap: (){
_animation.addStatusListener((status) {
if (status == AnimationStatus.completed) {
_animationController.reverse();
} else if (status == AnimationStatus.dismissed) {
_animationController.forward(from: 0.0);
}
});
},
child: new Image.asset(
'images/like.png'
color: Colors.white,
height: 50.0,
),
),
),
),
我喜欢上面的代码。但是当我 运行 应用程序时,动画会自动启动而无需单击按钮。但我只想在用户点击按钮时提供动画。当用户点击按钮时,只有动画开始,当点击按钮时,动画应该停止。 在我的例子中,动画自动开始。当用户点击按钮时,按钮应该弹起。
发生这种情况是因为您在 initState
:
_animationController.forward();
删除该行,直到您点击它才会开始。
此外,以下代码是不必要的,因为 Scale
会自行处理其状态:
_animation.addListener(() {
setState(() {});
});
如果你也从你的 initState
方法中删除它,你会没事的。
删除_animationController.forward();从 initState 并将其放入 GestureDetector
的 ontap 方法中