Flutter 通过 Widget 检测 PointerEvents

Flutter detect PointerEvents over Widget

我有一个示例代码可以检测悬停在小部件上的手写笔。

代码来自this Whosebug Quesion.

简而言之。它使用 GestureBinding.instance?.pointerRouter.addGlobalRoute 进行绑定,并检查事件是否属于手写笔类型。 当手写笔悬停在屏幕上(无接触)时会触发此事件。

它在 Text()Container() 等小部件上效果很好

问题:

我想在不同的 Widget 上使用此功能,即 Flutter InAppWebView but the event will not get triggered until the pen has contact with the surface. Even on the Container it does not work, if the child is the InAppWebView

我想这个问题也会出现在其他Widgets上。

我尝试了 ListenerAbsorbPointerIgnorePointer

更新 1:

当我开始将手写笔悬停在屏幕上时,我可以在调试输出中看到以下内容。

I/ViewRootImpl(23491): updatePointerIcon pointerType = 20001, calling pid = 23491
D/InputManager(23491): setPointerIconType iconId = 20001, callingPid = 23491

更新 2:

InAppWebView 有一个选项 useHybridComposition,默认为 false。将其设置为 true 即可解决问题。但是 WebView 变得非常慢。

HERE is a repository that shows the problem.

谢谢!

编辑

如下所述,这个问题有两个答案。

  1. useHybridComposition 设置为真。对于缓慢,可能会向该回购提出问题。

  2. 在 android/ios 级别而不是 Flutter 级别挂钩,并将事件转发回 Flutter。


调试方法大概是这样的:首先,像你的_handleEvent一样打印出指针事件。然后你就会知道指针事件是刚刚发生,还是根本没有发生。

其次,尝试哪些小部件可以,哪些不可以。 Text 可以,WebView 不行。那么Container可以吗? InkWell 可以吗? IconButton 可以吗? IconButton 可以吗?等。通过执行此操作,您可以深入了解使 Text 发挥作用的特殊之处。

第三,作为一个 hacky 解决方法,你能试试 Text.rich(WidgetSpan(child: your_web_view)) 吗?既然你说文本可以,而其他小部件不行。

最后,可能需要深入研究 Text 的源代码,看看发生了什么神奇的事情——可能是一些特殊的配置? - 让它发挥作用。