I'm looking for a way to disable long press gestures for the flutter WebView package: https://pub.dev/packages/webview_flutter

I've found that by wrapping the web view in a GestureDetector I'm able to prevent this, which works fine. However, after the first long press then scrolling in the web view no longer works correctly.

Sorry if this has been answered before but I can't find any info on how to do this, nor figure out a way to do it myself.

Thanks in advance for any help/advice


Solution 1: Chris

Ok, so after much trial and error I finally figured it out

Basically you need to set and reuse the WebViewController (this is set in onWebViewCreated) and then inside of onPageFinished you can inject your JavaScript string using evaluateJavascript function on that controller.

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

class Example extends StatefulWidget {
  @override
  ExampleState createState() => ExampleState();
}

class ExampleState extends State<Example> {
  final Completer<WebViewController> _controller = Completer<WebViewController>();

  WebViewController _webViewController;

  String jsString = 'document.addEventListener("contextmenu", event => event.preventDefault());';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: WebView(
        initialUrl: 'Your URL goes here',
        javascriptMode: JavascriptMode.unrestricted,
        gestureNavigationEnabled: false,
        onWebViewCreated: (WebViewController webViewController) async {
          _webViewController = webViewController;
          _controller.complete(webViewController);
        },
        onPageFinished: (_) {
          _webViewController.evaluateJavascript(jsString);
        },
      ),
    );
  }
}


Solution 2: DiyorbekDev

class _CourseReadState extends State<CourseRead> {
final GlobalKey webViewKey = GlobalKey();
WebViewController? _webViewController;
final Completer<WebViewController> _controller =
Completer<WebViewController>();
String jsString =
'document.addEventListener("contextmenu", event => event.preventDefault());';
@override
void initState() {
super.initState();
if (Platform.isAndroid) {
WebView.platform = SurfaceAndroidWebView();
}
}
@override
Widget build(BuildContext ctx) {
return BlocBuilder<CourseReadBloc, CourseReadState>(

  builder: (context, state) {
    if (state is CourseReadLoaded) {
      return Scaffold(
        appBar: MyAppBar(height: 60.h, title: 'title'),
        body: WebView(
          key: webViewKey,
          gestureNavigationEnabled: false,
          gestureRecognizers: {
            Factory&lt;OneSequenceGestureRecognizer&gt;(
              () =&gt; EagerGestureRecognizer(),
            ),
          },
          onWebViewCreated: (WebViewController webViewController) async {
            _webViewController = webViewController;
            _controller.complete(webViewController);
          },
          onPageFinished: (_) {
            _webViewController!.runJavascriptReturningResult(jsString);
          },
          javascriptMode: JavascriptMode.unrestricted,
          initialUrl: Uri.dataFromString(
            state.html,
            mimeType: 'text/html',
            encoding: Encoding.getByName('utf-8'),
          ).toString(),
        ),
      );
    }
    if (state is CourseReadError) {
      return Scaffold(
        appBar: MyAppBar(height: 60.h, title: 'title'),
        body: Center(child: Text(state.error)),
      );
    }
    return const Scaffold(body: progressIndicator);
  },
);

} }