flutter의 웹뷰 사례

3564 단어 필기
flutter 웹뷰 로드
플러그인 flutter 설치webview_plugin: ^0.2.1
listview에서 item 점프 페이지를 클릭하여 자세한 페이지 사례를 불러옵니다
 
import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';

/**
 * @Description      ,h5
 * @Author  zhibuyu
 * @Date 2018/10/19  9:09
 * @Version  1.0
 */
class NewsWebPage extends StatefulWidget{
  String  news_url;
  String title;

  NewsWebPage(this.news_url,this.title);

  @override
  State createState()=>new NewsWebPageState(news_url,title);

}
class NewsWebPageState extends State{
  String  news_url;
  String title;
  //         
  bool loading = true;
  //                    
  bool isLoadingCallbackPage = false;
  GlobalKey scaffoldKey = new GlobalKey();
  // URL     
  StreamSubscription onUrlChanged;
  // WebView         
  StreamSubscription onStateChanged;
  //        ,     WebView     
  FlutterWebviewPlugin flutterWebViewPlugin = new FlutterWebviewPlugin();

  NewsWebPageState(this.news_url, this.title);

  @override
  void initState() {
    onStateChanged = flutterWebViewPlugin.onStateChanged.listen((WebViewStateChanged state){
      // state.type       ,   :WebViewState.shouldStart, WebViewState.startLoad, WebViewState.finishLoad
      switch (state.type) {
        case WebViewState.shouldStart:
        //     
          setState(() {
            loading = true;
          });
          break;
        case WebViewState.startLoad:
        //     
          break;
        case WebViewState.finishLoad:
        //     
          setState(() {
            loading = false;
          });
          if (isLoadingCallbackPage) {
            //        ,   js      
            parseResult();
          }
          break;
      }
    });
  }
  //   WebView    
  void parseResult() {
//    flutterWebViewPlugin.evalJavascript("get();").then((result) {
//      // result json   ,  token  
//
//    });
  }

  @override
  Widget build(BuildContext context) {
    List titleContent = [];
    titleContent.add(new Text(
     title,
      style: new TextStyle(color: Colors.white),
    ));
    if (loading) {
      //        ,               
      titleContent.add(new CupertinoActivityIndicator());
    }
    titleContent.add(new Container(width: 50.0));
    // WebviewScaffold        ,          WebView   URL
    return new WebviewScaffold(
      key: scaffoldKey,
      url:news_url, //    URL
      appBar: new AppBar(
        title: new Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: titleContent,
        ),
        iconTheme: new IconThemeData(color: Colors.white),
      ),
      withZoom: true,  //       
      withLocalStorage: true, //   LocalStorage
      withJavascript: true, //     js  
    );
  }

  @override
  void dispose() {
    //       
    // Every listener should be canceled, the same should be done with this stream.
    onUrlChanged.cancel();
    onStateChanged.cancel();
    flutterWebViewPlugin.dispose();
    super.dispose();
  }
}

점프하는 곳에서 호출하다
Navigator.of(widget.parentContext).push(
new MaterialPageRoute(builder: (context) {
return new NewsWebPage(link,tiltes,);//link, title는 전달할 매개 변수입니다
},
));
자식에게 전재하는 것은 크게 말하지 않는다https://blog.csdn.net/u010123643/article/details/83379231

좋은 웹페이지 즐겨찾기