androidflutterwebviewlong-presstouchstart

Flutter Webview - trapping touchstart events on Android


This issue posted on Github seems to suggest that touch events are deliberately suppressed in the Android implementation ostensibly to enable the detection of longpresses for the purposes of copy/paste.

My own application absolutely needs to be able to trap touch events - and longpress text selections have no relevance whatsoever. I thought I would do this by using a local copy of the Flutter webview implementation code, and then suppressing the longpress filter. The relevant code in webview_android.dart reads

We prevent text selection by intercepting the long press event. This is a temporary stop gap due to issues with text selection on Android: https://github.com/flutter/flutter/issues/24585 - the text selection dialog is not responding to touch events. https://github.com/flutter/flutter/issues/24584 - the text selection handles are not showing. TODO(amirh): remove this when the issues above are fixed. onLongPress: () {},

To that end I downloaded the Flutter source and ensured that webview_flutter was available in a folder bearing that name two levels below my Flutter project root folder. After modifying pubspec.yaml to read

  webview_flutter:
    path: ../../webview_flutter

and running flutter clean && flutter run the APK was built and installed on my test device. However, it then reported

Setting up FlutterEngine. D/FlutterActivityAndFragmentDelegate(24999): No preferred FlutterEngine was provided. Creating a new FlutterEngine for this FlutterFragment. W/FlutterEngine(24999): Tried to automatically register plugins with FlutterEngine (io.flutter.embedding.engine.FlutterEngine@9480b1a) but could not find and invoke the GeneratedPluginRegistrant.

I do not understand this message. I'd be most obliged to anyone who might be able to tell me how/whether it can be fixed.


Solution

  • Here is what I have found: as far as I can tell the official Flutter webview is a neglected step child. There are bugs dating back three years that have received little attention. Some of its "features" such as longPress suppression/detection are downright ridiculous.

    I switched to using Flutter Webview plugin which fixes most of the issues I had encountered. Just remember to enable withzoom:true. I found that the webview does not automatically trap onPause and onResume events. I handled this as follows

     @override void didChangeAppLifecycleState(AppLifecycleState state) 
     {
      setState(() 
      {
       appState = state;
       switch(state)
       {
        case AppLifecycleState.inactive:
        case AppLifecycleState.paused:
             FlutterWebviewPlugin.evalJavascript('callBack()');break;//onPause
        case AppLifecycleState.resumed:
             FlutterWebviewPlugin.evalJavascript('callBack()');break;//onResume 
       }
      });
     }
    

    You will have to ensure that

    I should mention that I start from WidgetsApp and the above code is in the stateful class it uses with with WidgetsBindingObserver