颤抖如何在使用手势检测器进行onTap之后更改图像 [英] Flutter how to change image after onTap with Gesture Detector

查看:75
本文介绍了颤抖如何在使用手势检测器进行onTap之后更改图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在GestureDetector小部件内有一个图像.我想在调用 onTapDown 时更改此图像,然后在调用 onTapUp 时再次更改它.有可能做到这一点吗?在其他应用程序(带有Java的本机Android应用程序)中,我曾经使用按钮并使用选择器xml更改其背景,例如:

I have a Image inside a GestureDetector widget. I want to change this image when onTapDown is called, and then change it again when onTapUp is called. Is there possible to do that? In other app (Native Android app with java), I used to do it using a button and changing its background with a selector xml, like this:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/image1" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/image2" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/image2"/>
<item android:drawable="@drawable/image1" />

那么,有没有办法在颤振中做同样的事情?

so, is there a way to do the same in flutter?

---只需清除---

---JUST TO BE CLEAR---

在同一个小部件中,我需要一个未按下的图像,而另一个按下的图像.

In the same widget, I want an image if it is not pressed and a different image if it is pressed.

推荐答案

Image img;
// example: Image.asset('images/camera.png',)
Image imgUp =  Image.asset('your image directory for when tap up',);
Image imgDown =  Image.asset('your image directory for when tapdown',);

 @override
 void initState() {
 super.initState();
 img = imgUp;
 }


GestureDetector(
      //your image is here
      child: img,
      onTapDown: (tap){
        setState(() {
          // when it is pressed
          img =  imgDown;
        });
      },
      onTapUp: (tap){
        setState(() {
          // when it is released
          img = imgUp;
        });
      }, 

完整代码您可以在通过手势按下和释放图像时对其进行更改探测器您无需使用任何额外的库或包,这是非常容易的setState()以便在您按下释放键时更新视图

full code You can change the image when it is pressed and released using gesture detector It is super easy you don't need any extra library or package you have to just use setState() in order to update the view when you pressed an released

这篇关于颤抖如何在使用手势检测器进行onTap之后更改图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆