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
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,
),
),
something like that:
InAppWebView(
....
shouldOverrideUrlLoading: (controller, navigationAction) async {
final uri = navigationAction.request.url!;
if(whitenAvigationHosts.contains(uri.host)){
return NavigationActionPolicy.ALLOW;
}
return NavigationActionPolicy.CANCEL;
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With