Reputation: 616
I have a web view application in Flutter. The plugin/package is "flutter_webview_plugin". In Webview Scaffold, I have url= 'www.google.com'. Now I want that any URL which doesn't contain "google.com" opens in the local browser of the phone.
Maybe if I can restrict the Web view plugin if URL doesn't contain "google.com"
Please let me know how can I achieve this.
Here is my code:
WebviewScaffold(
url: "http://www.google.com")
Further Clarification (as requested by Günter Zöchbauer): I have a web view application. So the website that I have, has a lot of links in it. I don't want links other than my domain links to load in webview app. I want it to open externally.
Upvotes: 2
Views: 7471
Reputation: 657937
To launch an URL in an external browser use the https://pub.dartlang.org/packages/url_launcher package.
import 'package:flutter/material.dart'; import 'package:url_launcher/url_launcher.dart'; void main() { runApp(Scaffold( body: Center( child: RaisedButton( onPressed: _launchURL, child: Text('Show Flutter homepage'), ), ), )); } _launchURL() async { const url = 'https://flutter.io'; if (await canLaunch(url)) { await launch(url); } else { throw 'Could not launch $url'; } }
You just need to check the URL content and call _launchURL
or show the inline webview.
Update (according to the comment below)
https://github.com/flutter/plugins/pull/1323 (for iOS) and https://github.com/flutter/plugins/pull/1236 (Android) added the ability to specify navigation delegates.
https://github.com/amirh/plugins/blob/ad309267f50f924f9e4620f2126e72a2686c88a0/packages/webview_flutter/example/lib/main.dart#L56-L60 shows an example
return WebView( ... navigationDelegate: (NavigationRequest request) { if (request.url.startsWith('https://www.youtube.com/')) { print('blocking navigation to $request}'); return NavigationDecision.prevent; } print('allowing navigation to $request'); return NavigationDecision.navigate; },
Upvotes: 4