美文网首页flutterFlutter
Flutter之Webview组件

Flutter之Webview组件

作者: 习惯了_就好 | 来源:发表于2019-05-06 17:01 被阅读2次

    https://pub.flutter-io.cn/packages/flutter_webview_plugin

    /**
     *
     * const WebviewScaffold({
        Key key,
        this.appBar,
        @required this.url,
        this.headers,//
        this.withJavascript,//是否允许执行js代码
        this.clearCache,//
        this.clearCookies,//
        this.enableAppScheme,//
        this.userAgent,//
        this.primary = true,//
        this.persistentFooterButtons,//
        this.bottomNavigationBar,//
        this.withZoom,//是否允许网页缩放
        this.withLocalStorage,//是否允许LocalStorage
        this.withLocalUrl,//
        this.scrollBar,//是否显示滚动条
        this.supportMultipleWindows,//
        this.appCacheEnabled,//
        this.hidden = false,//
        this.initialChild,//
        this.allowFileURLs,//
        this.resizeToAvoidBottomInset = false,//
        this.invalidUrlRegex,//
        this.geolocationEnabled//
        })
     */
    
    
    class Widget_WebView_State extends State<Widget_WebView_Page>
        with SingleTickerProviderStateMixin {
      FlutterWebviewPlugin flutterWebviewPlugin = FlutterWebviewPlugin();
      var title = "WebView组件";
      var tabs;
      TabController controller;
    
      @override
      void initState() {
        super.initState();
        /**
         * 监听web页加载状态
         */
        flutterWebviewPlugin.onStateChanged.listen((
            WebViewStateChanged webViewState) {
    //      setState(() {
    //        title = webViewState.type.toString();
    //      });
          switch (webViewState.type) {
            case WebViewState.finishLoad:
              handleJs();
              break;
            case WebViewState.shouldStart:
              break;
            case WebViewState.startLoad:
              break;
            case WebViewState.abortLoad:
              break;
          }
        });
    
        /**
         * 监听页面加载url
         */
    //    flutterWebviewPlugin.onUrlChanged.listen((String url) {
    //      setState(() {
    //        title = url;
    //      });
    //    });
    
        /**
         * 监听x轴滑动距离
         * 好像没什么用
         */
    //    flutterWebviewPlugin.onScrollXChanged.listen((double offsetX) {
    //      title = offsetX.toString();
    //    });
    
    //    flutterWebviewPlugin.onScrollYChanged.listen((double offsetY) {
    //      title = offsetY.toString();
    //    });
    
        tabs = <Widget>[
          Tab(
            child: GestureDetector(
              child: Text("刷新"),
              onTap: () {
                flutterWebviewPlugin.reload();
              },
            ),
          ),
          Tab(
            child: GestureDetector(
              child: Text("返回"),
              onTap: () {
                flutterWebviewPlugin.goBack();
              },
            ),
          ),
          Tab(
            child: GestureDetector(
              child: Text("加载指定url"),
              onTap: () {
                flutterWebviewPlugin.reloadUrl("https://www.360.com");
              },
            ),
          ),
        ];
        controller =
            TabController(initialIndex: 0, length: tabs.length, vsync: this);
      }
    
      @override
      Widget build(BuildContext context) {
        return WebviewScaffold(
          url: "http://www.baidu.com",
          appBar: AppBar(
            title: Text(title),
            backgroundColor: Colors.grey,
            leading: GestureDetector(
              child: Icon(Icons.arrow_back),
              onTap: () {
                flutterWebviewPlugin.close();
              },
            ),
            bottom: TabBar(
              tabs: tabs,
              controller: controller,
              indicatorColor: Colors.red,
            ),
            actions: <Widget>[
            ],
          ),
          scrollBar: false,
          withZoom: false,
        );
      }
    
      @override
      void dispose() {
        flutterWebviewPlugin.dispose();
        super.dispose();
      }
    
      void handleJs() {
        flutterWebviewPlugin.evalJavascript(
            "abc(${title}')")
            .then((result) {
    
        });
      }
    }
    

    相关文章

      网友评论

        本文标题:Flutter之Webview组件

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