Flutter WebView

pub package

一个提供 WebView 组件的 Flutter 插件。

在 iOS 上,WebView 组件由 WKWebView 支持;
在 Android 上,WebView 组件由 WebView 支持。

用法

webview_flutter 添加为 pubspec.yaml 文件中的依赖项

您现在可以将 WebView 组件包含在您的 widget 树中。有关如何使用该 widget 的更多详细信息,请参阅该
WebView
widget 的 Dartdoc。

Android 平台视图

WebView 依赖于
平台视图来嵌入
Android 的 webview 到 Flutter 应用中。默认情况下,使用基于虚拟显示的平台视图
后端,此实现有多个
键盘.
当需要键盘输入时,我们建议使用基于混合组合的平台视图
实现。请注意,在 Android 10 之前的版本中,混合组合存在一些
性能上的不足.

使用混合组合

要启用混合组合,请在 initState() 中设置 WebView.platform = SurfaceAndroidWebView();
例如

import 'dart:io';

import 'package:webview_flutter/webview_flutter.dart';

class WebViewExample extends StatefulWidget {
  @override
  WebViewExampleState createState() => WebViewExampleState();
}

class WebViewExampleState extends State<WebViewExample> {
  @override
  void initState() {
    super.initState();
    // Enable hybrid composition.
    if (Platform.isAndroid) WebView.platform = SurfaceAndroidWebView();
  }

  @override
  Widget build(BuildContext context) {
    return WebView(
      initialUrl: 'https://flutterdart.cn',
    );
  }
}

SurfaceAndroidWebView() 要求 API 级别 19。插件本身不强制 API 级别,因此如果您想让应用在运行此 API 级别或更高版本的设备上可用,请在 <your-app>/android/app/build.gradle 中添加以下内容:

android {
    defaultConfig {
        // Required by the Flutter WebView plugin.
        minSdkVersion 19
    }
}

为 Android 启用 Material Components

要在用户与 WebView 中的输入元素交互时使用 Material Components,
请按照 启用 Material Components 的说明中所述的步骤进行操作。

GitHub

查看 Github