如何在 webview flutter 中允许 mailto 方案

how to allow mailto schemes in webview flutter

我正在使用 Flutter Webview 插件开发 Flutter webview 应用程序。

import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:url_launcher/url_launcher.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
            body: SafeArea(
              child : const WebView(
                initialUrl: 'https://google.com',
                javascriptMode: JavascriptMode.unrestricted,
              ),
            )
        )
    );
  }
}

但是,如果打开的网页中的任何 links 是一个应用程序 link,例如:fb://profile,我将得到 net::ERR_UNKNOWN_URL_SCHEME.

在android中,我发现解决方案是覆盖中提到的shouldOverrideUrlLoading,但我应该在flutter中做什么?

我正在尝试提到的解决方案 here

import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:url_launcher/url_launcher.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
            body: SafeArea(
              child : const WebView(
                initialUrl: 'https://google.com',
                javascriptMode: JavascriptMode.unrestricted,
                navigationDelegate: (NavigationRequest request)  {
                  if (request.url.contains("mailto:")) {
                    launch(request.url);
                    return NavigationDecision.navigate;
                  }
                },
              ),
            )
        )
    );
  }
}

但是它会抛出像

这样的错误

Error: Not a constant expression. if (request.url.contains("mailto:")) {

您可以复制粘贴 运行 下面的完整代码
第 1 步:您可以从 const WebView
中删除 const 关键字 第 2 步:您可以使用 NavigationDecision.prevent

工作演示

完整代码

进口'package:flutter/material.dart'; 导入 'package:webview_flutter/webview_flutter.dart'; 导入 'package:url_launcher/url_launcher.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
            body: SafeArea(
      child: WebView(
        initialUrl:
            'https://google.com', //'''https://www.scottseverance.us/mailto.html',
        javascriptMode: JavascriptMode.unrestricted,
        navigationDelegate: (NavigationRequest request) {
          print(request.url);
          if (request.url.contains("mailto:")) {
            launch(request.url);
            return NavigationDecision.prevent;
          } else {
            return NavigationDecision.navigate;
          }
        },
      ),
    )));
  }
}