flutter GestureDetector嵌套TextField 接收不到点击事件

GestureDetector的behavior有三个类型

enum HitTestBehavior {
  /// Targets that defer to their children receive events within their bounds
  /// only if one of their children is hit by the hit test.
  deferToChild,

  /// Opaque targets can be hit by hit tests, causing them to both receive
  /// events within their bounds and prevent targets visually behind them from
  /// also receiving events.
  opaque,

  /// Translucent targets both receive events within their bounds and permit
  /// targets visually behind them to also receive events.
  translucent,
}

默认是deferToChild,只有当children可以接收点击事件才能响应点击。

  • 原因:
    业务场景是点击textfield跳转到新页面,所以套一层GestureDetector来响应,然后将TextField的enabled设置为false,所以默认情况下响应不了点击。

  • 解决方法:
    将GestureDetector的behavior属性修改为behavior: HitTestBehavior.opaque即可。

你可能感兴趣的:(flutter GestureDetector嵌套TextField 接收不到点击事件)