Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter InAppWebView - opening other website addresses in the browser

I achieved this thanks to the code below while using Webview

            navigationDelegate: (NavigationRequest request) {
              if (request.url.startsWith('https://google.com/')) {
                print('allowing navigation to $request');
                return NavigationDecision.navigate;
              } else  {
                print('Opening Default Browser');
                launchURL(request.url); // to open browser 
                return NavigationDecision.prevent;
              }
            },

But when I used inappwebview, the above code didn't work. What should I do to open external web addresses in the browser when using Inappwebview?

InAppWebView: https://pub.dev/packages/flutter_inappwebview

like image 294
akf Avatar asked Oct 24 '25 19:10

akf


1 Answers

You can use shouldOverrideUrlLoading:() for InAppWebView.

            shouldOverrideUrlLoading: (controller, shouldOverrideUrlLoadingRequest) async {
              var url = shouldOverrideUrlLoadingRequest.url;
              var uri = Uri.parse(url);



              if ((uri.toString()).startsWith('https://google.com')) {
                return ShouldOverrideUrlLoadingAction.ALLOW;
              }else {
                launchURL(uri.toString());
                return ShouldOverrideUrlLoadingAction.CANCEL;
              }
            },
like image 168
Akif Akkaya Avatar answered Oct 26 '25 09:10

Akif Akkaya



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!