首先找到flutter項目根目錄下的pubspec.yaml, 在裡面添加一行webview的組件, 類似這樣:
dependencies: flutter: sdk: flutter # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^0.1.2 flutter_webview_plugin: ^0.3.0+2 dev_dependencies: flutter_test: sdk: flutter
上述代碼中, 支架了flutter_webview_plugin這一行。注意,是在dependencies的縮進區域布置的, 別搞錯了...
找到flutter項目目錄/lib/main.dart這個文档, 代碼如下:
import "package:flutter/material.dart"; import 'package:flutter_webview_plugin/flutter_webview_plugin.dart'; void main()=>runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return new MaterialApp( routes: { "/": (_) => new WebviewScaffold( url: "https://www.baidu.com", appBar: new AppBar( title: new Text("Widget webview"), ), ) }, ); } }
在測試的時候, 你可能會發現大於8.0版本的android系統無法讀入網頁, ios有時候不讀非https的頁面, 這是另外的故障, 需要修改一些配置, 在這裡暫時不多加探討。