webview - Flutter:Web View 应用程序仅在应用程序中运行特定网站,其他网站在手机本地浏览器中打开

标签 webview dart flutter uiwebview android-webview

我在 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)的功能。

https://github.com/amirh/plugins/blob/ad309267f50f924f9e4620f2126e72a2686c88a0/packages/webview_flutter/example/lib/main.dart#L56-L60显示示例

    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/

相关文章:

java - 如何从 URL 在 webView 中加载 PDF?

ios - 将目录中的所有图像显示到 Flutter 中的列表

flutter - 如何同时使用 AnimatedSwitcher 和 CustomScrollView

dart - 如何为屏幕设置不同的主题?

flutter - 我无法选中/取消选中复选框( flutter )

java - Android 应用程序(Webview)因闪屏而崩溃

c# - 在 Xamarin.Forms 的 WebView 中通过 Javascript onClick 事件调用 C# 函数

webview - Chrome 打包应用程序如何与 webview 交互(监听从中触发的事件)

constructor - Dart 工厂(构造函数)与静态方法;例如,为什么 int.parse() 不是工厂构造函数?

flutter - 如何打造浏览量重叠效果?