Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - Disable navigation InAppWebView

I'm new to Flutter, and I want to put a web view between other widgets and I'm using the InAppWebView flutter in order to do it, but I want to prevent the user to navigate to other pages when he clicks on a button inside the web view. Do I have to write some native code for iOS and Android to prevent the navigation?

Thanks

like image 937
Mireille Avatar asked Jul 10 '26 03:07

Mireille


2 Answers

Since you are using InAppWebView package you can intercept each URL the Webview would navigate to before it is actually navigate and then make a decision of whether to navigate or not using the callback shouldOverrideUrlLoading like the following:

shouldOverrideUrlLoading: (controller, navigationAction) async {
    // You can access the URL like this
    final url = navigationAction.request.url.toString();

    if (condition) {
        // This one means do not navigate
        return NavigationActionPolicy.CANCEL;
    }

    // This one means navigate
    return NavigationActionPolicy.ALLOW;
},

Note: To use the callback shouldOverrideUrlLoading, you have to enable it in the initialOptions of the webview like the following because the default is false:

initialOptions: InAppWebViewGroupOptions(
    crossPlatform: InAppWebViewOptions(
        useShouldOverrideUrlLoading: true,
    ),
),
like image 91
Moaz El-sawaf Avatar answered Jul 17 '26 14:07

Moaz El-sawaf


something like that:

InAppWebView(
....
 shouldOverrideUrlLoading: (controller, navigationAction) async {
  final uri = navigationAction.request.url!;
  if(whitenAvigationHosts.contains(uri.host)){
    return NavigationActionPolicy.ALLOW;
  }
  return NavigationActionPolicy.CANCEL;
})
                
like image 35
Marcelo Juan Cabrera Gutierrez Avatar answered Jul 17 '26 16:07

Marcelo Juan Cabrera Gutierrez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!