gpt4 book ai didi

flutter - 处理父部件中的点击事件

转载 作者:IT王子 更新时间:2023-10-29 06:54:50 24 4
gpt4 key购买 nike

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

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

他们都试图对触摸事件使用react,但只有 child 的回调被调用(这是默认行为)。是否有可能以防止祖先将触摸事件传播给其子项的方式定义小部件?或者也许唯一的方法是自己实现这种行为,例如通过使用 GestureArena?

最佳答案

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

A widget that absorbs pointers during hit testing.

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'),
),
),
),

关于flutter - 处理父部件中的点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52164463/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com