在 pubspec.yaml 中导入 flutter_webview_plugin 插件
使用时 import
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';
class WebView extends StatefulWidget {
final url;
final title;
WebView({Key key, @required this.url, @required this.title})
: super(key: key);
@override
_WebViewState createState() => _WebViewState();
}
class _WebViewState extends State<WebView> {
FlutterWebviewPlugin flutterWebviewPlugin = FlutterWebviewPlugin();
void initState() {
super.initState();
flutterWebviewPlugin.onStateChanged.listen((WebViewStateChanged state) {
switch (state.type) {
case WebViewState.shouldStart:
//准备加载
break;
case WebViewState.startLoad:
//开始加载
break;
case WebViewState.finishLoad:
//加载完成
break;
case WebViewState.abortLoad:
break;
}
});
}
@override
Widget build(BuildContext context) {
return WebviewScaffold(
url: widget.url,
appBar: Text(
widget.title,
),
withZoom: true, //允许网页缩放
withLocalStorage: true,
withJavascript: true, //允许执行 js 代码
);
}
@override
void dispose() {
flutterWebviewPlugin.dispose();
super.dispose();
}
}
ps:支持http需要在iOS的info.plist文件加入以下字段:
<key>io.flutter.embedded_views_preview</key>
<true/>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
image.png
image.png
网友评论