Saif Amer
Saif Amer

Reputation: 11

How to disable long click on WebView in flutter? I need to deactivate it for android and ios

 Widget build(BuildContext context) {
return Scaffold(
  // backgroundColor:const Color(0xff37bfc3) ,
  appBar: AppBar(
    backgroundColor: Colors.white,
    centerTitle: true,
    automaticallyImplyLeading: false,
  ),
  body: Stack(
    children: [
      Padding(
        padding: const EdgeInsets.only(top: 0.0),
        child: WebView(
          onWebViewCreated: (WebViewController webViewController) async {
            _webViewController = webViewController;
            _controller.complete(webViewController);
          },
          onPageFinished: (_) {
            _webViewController.evaluateJavascript(jsString);
            setState(() {
              isLoading = false;
            });
          },
          gestureNavigationEnabled: false,
          zoomEnabled: false,
          debuggingEnabled: false,
          allowsInlineMediaPlayback: false,
          key: _key,
          initialUrl: this.url,
          javascriptMode: JavascriptMode.unrestricted,

        ),
      ),
      isLoading
          ? Center(
              child: CircularProgressIndicator.adaptive(),
            )
          : Stack(),
    ],
  ),
);

}

i need help plz How to disable long click on WebView in flutter? I need to deactivate it for android and IOS i need help plz How to disable long click on WebView in flutter? I need to deactivate it for android and IOS

enter image description here

Upvotes: 0

Views: 1518

Answers (1)

DiyorbekDev
DiyorbekDev

Reputation: 774

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 Scaffold(
            appBar: MyAppBar(height: 60.h, title: 'title'),
            body: WebView(
              key: webViewKey,
              gestureNavigationEnabled: false,
              gestureRecognizers: {
                Factory<OneSequenceGestureRecognizer>(
                  () => 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(),
            ),
          );
      },
    );
  }
}

Upvotes: 1

Related Questions