简体   繁体   English

在 flutter 中被点击时如何为我的图标设置动画

[英]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.

相关问题 如何创建在被点击时播放音频文件的通知? - How can I create a notification that plays an audio file when being tapped? 我如何为汉堡抽屉图标设置动画 - How can i animate hamburger drawer icon 如何在我的 flutter 应用程序中使汉堡图标更大 - how can i make the burger icon bigger in my flutter app 当我点击底部导航栏“收藏夹”时,删除了 ListTile 的尾随图标属性 - trailing icon property of ListTile removed when i tapped on bottomnavigationbar 'favorite' 如何使旋转动画流畅? - How can I animate my rotation smoothly? 在Android设备上,如何在摄像头处于活动状态的情况下用手指轻拍.png - On Android, How can I get a small .png to appear where I tapped with my finger with the camera active 在我的应用程序上点击屏幕时,如何隐藏数字键盘? (爪哇) - How would I hide a Numeric Keyboard when screen is tapped on my app? (Java) 为什么我的颤抖的应用启动器图标成一个圆圈? 我可以用方形图标代替它吗? - Why my flutter app launcher icon in a circle? Can I use a square icon to replace this one? 我如何将图标放在颤动的自定义按钮中 - How can i put icon inside custom button in flutter 如何在 flutter 的 TextFormField 中的图标和文本之间添加分隔线 - How can i add a divider between icon and text in TextFormField in flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM