[英]How can i animate my icon when being tapped in flutter
I am trying to animate an icon in my app whenever it is being tapped.我试图在我的应用程序中为一个图标设置动画,只要它被点击。 I achieved the simple animation that i want, but it will always animate once after I come on that screen.
我实现了我想要的简单 animation,但在我进入该屏幕后它总是会动画一次。 I want that it should be animated every time I tap on the
IconButton
, and it should be rotated clockwise with duration of 2 seconds and when again pressed, rotated anticlockwise with duration of 2 seconds.我希望每次点击
IconButton
时它都应该是动画的,它应该顺时针旋转 2 秒,再次按下时,逆时针旋转 2 秒。 Please help me请帮我
Here is the part of code -这是代码的一部分 -
.......
AnimationController _controller;
Animation<double> _animation;
@override
void initState() {
// TODO: implement initState
super.initState();
_cameraController = CameraController(cameras[0], ResolutionPreset.medium, imageFormatGroup: ImageFormatGroup.jpeg);
cameraValue = _cameraController.initialize();
_fetchAssets();
_controller = AnimationController(
duration: const Duration(seconds: 2),
vsync: this,
value: 025,
lowerBound: 0.0,
upperBound: 1.0,
)..repeat();
_animation = CurvedAnimation(parent: _controller, curve: Curves.linear);
_controller.forward();
}
.......
RotationTransition(
turns: _animation,
child: IconButton(
icon: Icon(
Icons.flip_camera_ios,
color: Colors.white,
size: MediaQuery.of(context).size.width*0.07,
),
onPressed: () async {
setState(() {
if(cameraPosition=="Rear") {
cameraPosition="Front";
}
else if(cameraPosition=="Front"){
cameraPosition="Rear";
}
});
_cameraController = CameraController(
cameras[
cameraPosition=="Rear" ? 0
: 1
],
ResolutionPreset.medium,
);
},
),
),
........
use the following flutter package: https://pub.dev/packages/scale_button使用以下 flutter package: https://pub.dev/packages/scale_button
ScaleButton(
child: yourIcon())
If you put your icon animation code inside initState(), it will only being triggered once every time you start/restart your screen.如果你把你的图标 animation 代码放在 initState() 中,它只会在你每次启动/重启屏幕时触发一次。 To make it being triggered every time you press the IconButton you should copy or move your icon animation code into the onPressed property of the Icon Button, inside the setState() function.
为了让它在你每次按下 IconButton 时触发,你应该将你的图标 animation 代码复制或移动到图标按钮的 onPressed 属性中,在 setState() function 内。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.