扩展容器中的手势检测

时间:2020-10-26 17:53:08

标签: flutter gesture

我的小部件的一个区域是使用扩展容器创建的,例如:

child: Column(
  crossAxisAlignment: CrossAxisAlignment.stretch,
  children: [
    Container(
      color: Colors.blueGrey[200],
      child: Text(
        startTime,
        textAlign: TextAlign.left,
        style: TextStyle(
          backgroundColor: Colors.blueGrey[200],
          fontSize: 16,
          fontWeight: FontWeight.normal),
        ),
      ),
      Expanded(
        /* If this gesture detector is instantiated the tap is never found;
        child: GestureDetector(
        behavior: HitTestBehavior.translucent,
        onTap: () => print("OnTap"),
        */
        child:
          Container(color: Colors.red[600]),
        //),
      ),  // Expanded
  ], // children
), // Column

如果上面的代码是这样包装的

Expanded(
  child: GestureDetector(
    behavior: HitTestBehavior.translucent,
    onTap: () => print("OnTap"),
    child: Container(
      color: Colors.red[600],
      child: // Code above
    ), // Container
  ), //GestureDetector  
), // Expanded

然后onTap仅注册在文本小部件中,而不注册在最后一个扩展的容器中。

检测是否不会在Container上触发并且需要一个小部件来填充要检测的空间是否正确?

修改 我将扩展容器的手势检测更改为:

Expanded(
  child: Material(
    color: Colors.red[600],
    child: InkWell(
      onTap: () {print("InkWell");},
    ),  // InkWell
  ),  // Material
),  // Expanded

仍然无法识别点击。

1 个答案:

答案 0 :(得分:0)

第一段代码应该可以正常工作,我认为您在代码的另一部分遇到错误,请提供完整的代码吗?以及您在控制台中得到的内容。

这是您在DartPad中完全可用的代码段:https://dartpad.dev/f76d383475c5c7c35ca4bf59b0a83789