Flutter:Web视图应用程序只在应用程序中运行特定网站,其他人在本地浏览器中打开手机

问题描述 投票:0回答:1

我在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应用程序中。我希望它在外部打开。

webview dart flutter uiwebview android-webview
1个回答
0
投票

要在外部浏览器中启动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)添加了指定导航委托的功能。

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;
      },
© www.soinside.com 2019 - 2024. All rights reserved.