在 Flutter 中,定位的 Widget 如何在其父 Stack 区域之外感觉到轻敲? [英] In Flutter, how can a positioned Widget feel taps outside of its parent Stack area?

查看:13
本文介绍了在 Flutter 中,定位的 Widget 如何在其父 Stack 区域之外感觉到轻敲?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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
  • 使 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屋!

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