【问题标题】:Flutter - GestureDetector onTapDown color changingFlutter - GestureDetector onTapDown 变色
【发布时间】:2018-12-12 19:45:50
【问题描述】:

我确实想在我的底部工作表中实现一个手势检测器,当它调用onTapDownGestureDetectoronTapCancel 函数时,它应该改变容器的颜色。但是该功能没有改变任何东西。我还将下面的代码放在StatefulWidget 中,这样我就可以调用setState((){})

这是我的代码:

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);
      },
    );

希望有人能帮助我。

【问题讨论】:

  • enabled 是您的 State 类中的一个字段吗?
  • 只是一个布尔值来控制容器是否被按下
  • bool enabled 是您的 State 类中的字段吗?
  • 不,它不是我的状态类中的字段
  • 所以让它成为你State类中的一个字段

标签: android ios dart flutter


【解决方案1】:

build 方法中删除bool enabled = false;,因为每次调用setState 时,它的值都相同。

将您的 enabled 变量更改为实例字段。

    class YourClass ...

    bool enabled = false;

    ..
    @override
      Widget build(BuildContext context) {

【讨论】:

  • 抱歉迟到了1分钟-.-
猜你喜欢
  • 2023-03-26
  • 1970-01-01
  • 2019-04-13
  • 1970-01-01
  • 1970-01-01
  • 2023-01-10
  • 2021-10-09
  • 2021-02-24
  • 1970-01-01
相关资源
最近更新 更多