flutterflutter-layoutflutter-inappwebview

Flutter detect PointerEvents over Widget


I have a sample code which detects a hovering stylus over a widget.

The code is from this Stackoverflow Quesion.

In short. It binds using GestureBinding.instance?.pointerRouter.addGlobalRoute and checks in the handler if the event is of type stylus. This gets triggered when the stylus is hovering over the screen (no contact).

It works great on Widgets like Text(), Container() etc.

Question:

I want to use this functionality on a different Widget, the 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.

I think this problem will occur on other Widgets too.

I tried the Listener, AbsorbPointer and IgnorePointer.

Update 1:

I can see the following in the debug output when I start hovering the stylus over the screen.

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

Update 2:

The InAppWebView has an option useHybridComposition which is false by default. Setting it to true solves the issue. But the WebView is becoming very slow.

HERE is a repository that shows the problem.

Thanks!


Solution

  • As desribed below, this question has two solutions.

    1. Set useHybridComposition to true. For slowness, maybe raise an issue to that repo.

    2. Hook at android/ios level instead of Flutter level, and forward events back to Flutter.


    The debugging method maybe like this: Firstly, print out the pointer events in methods like your _handleEvent. Then you will know whether the pointer event just occur, or they even do not occur.

    Secondly, try what widgets are OK and what are not. Text is OK, WebView is not. Then is Container OK? Is InkWell OK? Is IconButton OK? Is IconButton OK? etc. By doing this you gain insight of what is special about Text that makes it work.

    Thirdly, as a hacky workaround, could you please try Text.rich(WidgetSpan(child: your_web_view))? Since you say Text is OK while other widgets are not OK.

    Lastly, maybe need to dig into Text's source to see what magic happens - probably some special configuration? - to let it work.