覆盖子GestureDetectors的GestureDetector?

Luk*_*tti 0 flutter

有什么方法可以使GestureDetector覆盖所有子GestureDetectors的功能?

我有一个复杂的Widget,我希望能够轻松地从高级别覆盖其所有行为。例如,将自由用户锁定在功能之外。

Rém*_*let 6

你可以改变HitTestBehavior behaviorGestureDetector,以HitTestBehavior.opaque

GestureDetector(
   behavior: HitTestBehavior.opaque,
   ...
)
Run Code Online (Sandbox Code Playgroud)

默认情况下,它使用HitTestBehavior.deferToChild.

  • 如果我添加一个 IgnorePointer 作为子项,然后将我的 Widget 添加为该子项,则对我有用。GestureDetector -> IgnorePointer -> MyChild (4认同)
  • 不幸的是,这并没有压倒我的孩子们。 (2认同)

bof*_*mer 5

要暂时禁用所有子手势检测器,请使用IgnorePointer小部件:

  @override
  Widget build(BuildContext context) {

    bool ignoreChildGestures = true;

    return GestureDetector(
      onTap: () {
        print('parent tapped');
      },
      child: IgnorePointer(
        ignoring: ignoreChildGestures,
        child: GestureDetector(
          onTapDown: (details) {
            // won't be called when ignoring is set to true
            print('child tap down!');
          },
        ),
      ),
    );
  }
Run Code Online (Sandbox Code Playgroud)

  • 它可以工作,但顶级 GestureBehavior 必须将其行为属性设置为不透明 (2认同)