Flutter detect PointerEvents over Widget

202

EDIT

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.

Share:
202
dknaack
Author by

dknaack

Updated on December 02, 2022

Comments

  • dknaack
    dknaack 11 months

    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!

    • ch271828n
      ch271828n over 1 year
      could you please show your full code that does not work?
    • Sajjad
      Sajjad over 1 year
      I guess the problem comes from inAppWebView, this library make me mad ! , and get a lot of memory (about 30 MB), so if possible for you change this library . also please try useHybridComposition: true
    • dknaack
      dknaack over 1 year
      As mentioned, useHybridComposition works, but the View becomes slow.
  • dknaack
    dknaack over 1 year
    Thanks for the answer. I already tried the Container and others with the same result. It works with the Container but NOT with the InAppWebView in it. Text.rich doesnt work either.
  • ch271828n
    ch271828n over 1 year
    @dknaack Hi then please show your code :)
  • ch271828n
    ch271828n over 1 year
    I see: possibly because the way InAppWebView embeds into Flutter. Please try pub.dev/packages/webview_flutter, notice it has two modes - hybrid composition and virtual display. Try both modes, and see which works?
  • dknaack
    dknaack over 1 year
    I was in the process of setting up a demo repo, but tried your suggested "hybrid composition" thing. 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.
  • dknaack
    dknaack over 1 year
    I added a link to a GitHub Repository.
  • ch271828n
    ch271828n over 1 year
    @dknaack about slow - have you used release build? never use debug build to test speed, otherwise you can very fake results. in addition, I see the core reason: if not using hybridcomposition, the webview's view indeed bypasses the normal Flutter gesture system, so surely you will not get any events!
  • dknaack
    dknaack over 1 year
    Same with the Release build, yes. Can you think of another way to hook into this?
  • ch271828n
    ch271828n over 1 year
    @dknaack yes, but much harder: hook at android/ios level. you know, android/ios has its own touch event dispatch mechanisms, then read events from there, and forward events back to Dart.
  • ch271828n
    ch271828n over 1 year
    @dknaack does that work?
  • dknaack
    dknaack over 1 year
    What works? You mean the native package? I could create it for android but not for iOS.
  • ch271828n
    ch271828n over 1 year
    @dknaack why not ios? feel free to ask technical problems
  • ch271828n
    ch271828n over 1 year
    is there any updates?
  • dknaack
    dknaack over 1 year
    Not really. I still try to find a solution without writting my own native module.
  • ch271828n
    ch271828n over 1 year
    @dknaack looking forward to it! feel free to ask if you have problems
  • ch271828n
    ch271828n over 1 year
    The bounty is expiring...
  • dknaack
    dknaack over 1 year
    ok. I accept the answer. Maybe i come back later. Thank you very much!
  • ch271828n
    ch271828n over 1 year
    @dknaack You are welcome, and hope you solve the problem!