我在 Flutter 中有一个 Web View 应用程序。插件/包是“flutter_webview_plugin”。在 Webview Scaffold 中,我有 url= 'www.google.com'。现在我希望任何不包含“google.com”的网址都在手机的本地浏览器中打开。
如果网址不包含“google.com”,也许我可以限制 WebView 插件
请告诉我如何实现这一目标。
这是我的代码:
WebviewScaffold(
url: "http://www.google.com")
进一步说明(根据 Günter Zöchbauer 的要求):我有一个 Web View 应用程序。所以我的网站有很多链接。我不希望在 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) 添加了指定导航委托(delegate)的功能。
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; },
关于webview - Flutter:Web View 应用程序仅在应用程序中运行特定网站,其他网站在手机本地浏览器中打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55125577/