我在Flutter中有一个Web视图应用程序。插件/包是“flutter_webview_plugin”。在Webview Scaffold中,我有url ='www.google.com'。现在我希望任何不包含“google.com”的网址都会在手机的本地浏览器中打开。
如果URL不包含“google.com”,我可以限制Web视图插件
请让我知道如何实现这一目标。
这是我的代码:
WebviewScaffold(
url: "http://www.google.com")
进一步澄清(根据GünterZöchbauer的要求):我有一个Web视图应用程序。所以我拥有的网站中有很多链接。我不希望我的域链接以外的链接加载到webview应用程序中。我希望它在外部打开。
要在外部浏览器中启动URL,请使用https://pub.dartlang.org/packages/url_launcher包。
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'; } }
您只需检查URL内容并调用_launchURL
或显示内联webview。
更新(根据以下评论)
https://github.com/flutter/plugins/pull/1323(适用于iOS)和https://github.com/flutter/plugins/pull/1236(Android)添加了指定导航委托的功能。
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; },