我有一个特殊情况,我有一个
Listener
可以检测小部件上的 onPointerHover
事件,可以通过多种方式进行转换,例如旋转、缩放、平移。我现在希望用户在将鼠标移动到小部件的一角时能够调整小部件的大小,该角应检测到 -5px 到 +5px 的范围。检测到这个内部是没有问题的,即使它被变换了,我可以使用 localPosition
并得到正确的点。困难在于外部监听不起作用,因为它位于渲染的小部件区域之外。当然,我可以将侦听器设置为小部件树的顶部,但这会使获取用户在小部件转换时悬停在小部件上的位置变得复杂。
但是,我当前的计划是使用自定义解决方案扩展当前的
Listener
,当用户位于渲染的子级之外时也会发出该解决方案。在我看来,问题在于它没有检测到 Renderbox
类中的外部事件。
bool hitTest(BoxHitTestResult result, {required Offset position}) {
assert(() { /* Some code */ }());
/// The following line may be the problem
if (_size!.contains(position)) {
if (hitTestChildren(result, position: position) || hitTestSelf(position)) {
result.add(BoxHitTestEntry(this, position));
return true;
}
}
return false;
}
现在的问题是,即使我重写
hitTest
方法也不会发生任何事情,事实上 print('hit');
也永远不会打印到控制台。我做错了什么,或者有其他方法阻止事件吗?
class ExtendedRenderProxyBox extends RenderBox with RenderObjectWithChildMixin<RenderBox>, RenderProxyBoxMixin<RenderBox> {
ExtendedRenderProxyBox([RenderBox? child]) {
this.child = child;
}
@override
bool hitTest(BoxHitTestResult result, {required Offset position}) {
print('hit');
return true;
}
}
我自己找到了解决方案。我只是使用 defer_pointer 包,它解决了我的问题。