处理父窗口小部件中的点击事件 [英] Handle tap event in parent widget

查看:79
本文介绍了处理父窗口小部件中的点击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序树中,我有两个小部件:

In my app tree I have two widgets:

GestureDetector(
   onTap: () => print('Outer'),
   child: IconButton(
     icon: Icon(Icons.add),
     onPressed: () => print('Inner')),
   ),
)

他们两个都试图对touch事件做出反应,但仅调用子级的回调(这是默认行为).是否可以以防止祖先将触摸事件传播给其子代的方式定义小部件?或者,也许唯一的方法就是自己实现这种行为,例如通过使用GestureArena?

Both of them are trying to react to touch event but only child's callback is invoked (which is default behaviour). Is it possible to define widgets in a way that will prevent ancestor from propagating touch event to its children? Or maybe the only way is to implement such behaviour on your own, e.g. by making use of GestureArena?

推荐答案

我认为您正在寻找 AbsorbPointer 小部件:

I think you are looking for the AbsorbPointer widget:

在点击测试期间吸收指针的小部件.

A widget that absorbs pointers during hit testing.

当吸收为true时,此小部件将阻止其子树从 通过本身终止命中测试来接收指针事件.它 仍然在布局过程中占用空间并像往常一样绘制其子级.它 只是阻止其子级成为定位事件的目标, 因为它从RenderBox.hitTest返回true.

When absorbing is true, this widget prevents its subtree from receiving pointer events by terminating hit testing at itself. It still consumes space during layout and paints its child as usual. It just prevents its children from being the target of located events, because it returns true from RenderBox.hitTest.

您可以像这样使用它:

GestureDetector(
  onTap: () => print('Outer'),
  child: AbsorbPointer(
    child: IconButton(
      icon: Icon(Icons.add),
      onPressed: () => print('Inner'),
    ),
  ),
),

这篇关于处理父窗口小部件中的点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆