简体   繁体   English

Flutter - GestureDetector onTapDown 颜色变化

[英]Flutter - GestureDetector onTapDown color changing

I do want to implement in my bottom sheet a gesturedetector, that should change the color of the container when it calls the onTapDown and the onTapCancel function of the GestureDetector .我确实想在我的底部工作表中实现一个手势检测器,它应该在调用 GestureDetector 的onTapDownonTapCancel函数时更改容器的GestureDetector But the function is not changing anything.但是这个功能并没有改变任何东西。 I've also put the code below inside a StatefulWidget so that I am able to call setState((){}) .我还将下面的代码放在StatefulWidget中,这样我就可以调用setState((){})

This is my code:这是我的代码:

bool enabled = false;
    return InkWell(
      child: Container(
        child: Row(
          children: <Widget>[
            Padding(
              padding: EdgeInsets.only(top: 16.0, bottom: 16.0, left: 30.0),                
            ),
            Text(
              text,
              style: TextStyle(
                color: (enabled
                  ? textColor
                  : Colors.black54
                ),
                fontWeight: FontWeight.bold
              ),
            )
          ],
        ),
        decoration: BoxDecoration(
          color: (enabled
            ? background
            : Colors.transparent
          )
        ),
      ),
      onTapDown: (TapDownDetails details){
        setState(() {
          enabled = true;
        });
      },
      onTapCancel: (){
        setState(() {
          enabled = false;
        });
      },
      onTap: (){
        String r_value;
        if(text == sheetText[0]){
          r_value = "delete";
        } else if(text == sheetText[1]){
          r_value = "edit";
        } else if(text == sheetText[2]){
          r_value = "notification";
        } else {
          return;
        }
        Navigator.pop(context, r_value);
      },
    );

I hope somebody is able to help me.我希望有人能够帮助我。

Remove bool enabled = false;删除bool enabled = false; from your build method, because every time you call setState it will have the same value.从你的build方法,因为每次你调用setState时它都会有相同的值。

Change your enabled variable to an instance field.enabled的变量更改为实例字段。

    class YourClass ...

    bool enabled = false;

    ..
    @override
      Widget build(BuildContext context) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 flutter 中使用手势检测器轻敲容器时更改容器颜色 - Changing a container color when container is tapped using gesturedetector in flutter flutter GestureDetector 与 flutter map 或 mapbox - flutter GestureDetector with the flutter map or mapbox 使用 Flutter 更改 TaskDescription 栏颜色 - Changing the TaskDescription bar color with Flutter Flutter:GestureDetector 中的 setState() 不起作用 - Flutter: setState() inside GestureDetector is not working GestureDetector 改变子部件的样式属性 - GestureDetector changing the style properties of the child widget 不要使用 GestureDetector 中的 setstate 方法更改我的容器颜色 - not change My Container Color with method setstate in GestureDetector Flutter:在一堆小部件中结合 Draggable 和 GestureDetector - Flutter: combining Draggable and GestureDetector in a Stack of widgets 如何在 Flutter 中使用 GestureDetector 隐藏 appBar? - How to hide appBar using GestureDetector in Flutter? Flutter Button (GestureDetector) with Navigator.push function 无法按下 - Flutter Button (GestureDetector) with Navigator.push function cannot be pressed Flutter:更改启动画面时“无法解析符号‘@android:color/white’” - Flutter: "Cannot resolve symbol '@android:color/white'" while changing splash screen
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM