美文网首页
Flutter加载WebView

Flutter加载WebView

作者: 倪大头 | 来源:发表于2019-07-26 09:50 被阅读0次

    在 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

    相关文章

      网友评论

          本文标题:Flutter加载WebView

          本文链接:https://www.haomeiwen.com/subject/vqzlrctx.html