在Flutter中,定位的Widget如何感觉到其父Stack区域之外的点击? [英] In Flutter, how can a positioned Widget feel taps outside of its parent Stack area?
问题描述
Stack
在Positioned
内包含MyWidget
.
Stack(
overflow: Overflow.visible,
children: [
Positioned(
top: 0.0,
left: 0.0,
child: MyWidget(),
)],
);
由于溢出是Overflow.visible
并且MyWidget
大于Stack
,因此它显示在Stack
之外,这就是我想要的.
Since overflow is Overflow.visible
and MyWidget
is larger than the Stack
, it displays outside of the Stack
, which is what I want.
但是,我无法点击Stack
区域之外的MyWidget
区域.它只是忽略了那里的水龙头.
However, I can't tap in the area of MyWidget
which is outside of the Stack
area. It simply ignores the tap there.
如何确保MyWidget
在此处接受手势?
How can I make sure MyWidget
accepts gestures there?
推荐答案
之所以会发生此行为,是因为堆栈在检查孩子是否被击中之前先检查指针是否在其边界之内:
This behavior occurs because the stack checks whether the pointer is inside its bounds before checking whether a child got hit:
类:RenderBox(RenderStack对其进行扩展)
Class: RenderBox (which RenderStack extends)
bool hitTest(BoxHitTestResult result, { @required Offset position }) {
...
if (_size.contains(position)) {
if (hitTestChildren(result, position: position) || hitTestSelf(position)) {
result.add(BoxHitTestEntry(this, position));
return true;
}
}
return false;
}
我的解决方法是删除
if (_size.contains(position))
检查. 不幸的是,如果不从框架中复制代码,这是不可能的.
check. Unfortunately, this is not possible without copying code from the framework.
这是我所做的:
- 复制Stack类并将其命名为Stack2
- 复制RenderStack并将其命名为RenderStack2
- Made Stack2参考RenderStack2
- 从上方添加了不带_size.contains检查的hitTest方法
- 已复制Positioned并将其命名为Positioned2,并使其引用Stack2作为其通用参数
- 在我的代码中使用了Stack2和Positioned2
此解决方案绝不是最佳解决方案,但可以实现所需的行为.
This solution is by no means optimal, but it achieves the desired behavior.
这篇关于在Flutter中,定位的Widget如何感觉到其父Stack区域之外的点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!