扩展容器中的手势检测
Gesture detection in Expanded Container
我的小部件有一个区域是使用扩展容器创建的,例如:
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只在text widget中注册,不会在最后展开的container中注册。
检测不在Container上触发,需要widget填充space检测是否正确?
编辑
我将扩展容器的手势检测更改为:
Expanded(
child: Material(
color: Colors.red[600],
child: InkWell(
onTap: () {print("InkWell");},
), // InkWell
), // Material
), // Expanded
而且点击仍然无法识别。
第一段代码应该可以正常工作,我认为你的代码的另一部分出错了,你能提供完整的代码吗?以及您在控制台中获得的内容。
这是您在 DartPad 中完全可用的代码片段:https://dartpad.dev/f76d383475c5c7c35ca4bf59b0a83789
我的小部件有一个区域是使用扩展容器创建的,例如:
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只在text widget中注册,不会在最后展开的container中注册。
检测不在Container上触发,需要widget填充space检测是否正确?
编辑 我将扩展容器的手势检测更改为:
Expanded(
child: Material(
color: Colors.red[600],
child: InkWell(
onTap: () {print("InkWell");},
), // InkWell
), // Material
), // Expanded
而且点击仍然无法识别。
第一段代码应该可以正常工作,我认为你的代码的另一部分出错了,你能提供完整的代码吗?以及您在控制台中获得的内容。
这是您在 DartPad 中完全可用的代码片段:https://dartpad.dev/f76d383475c5c7c35ca4bf59b0a83789