美文网首页
flutter webview 带进度条,加载框

flutter webview 带进度条,加载框

作者: 张漂亮1号 | 来源:发表于2020-01-14 14:22 被阅读0次

    网页加载中,没有进度条,一片空白,实在不雅观,实现的效果如下:

    image

    自定义webview,展示进度条和加载框

    import 'package:flutter/material.dart';
    import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';
    
    //展示网页数据
    // ignore: must_be_immutable
    class MyWebViewPage extends StatefulWidget {
      String url;
      String title;
    
      MyWebViewPage({Key key, @required this.url, @required this.title});
    
      @override
      createState() => _PageState(url: url, title: title);
    }
    
    class _PageState extends State<MyWebViewPage> {
      _PageState({Key key, @required this.url, @required this.title});
    
      String url;
      String title;
      FlutterWebviewPlugin _webViewPlugin = FlutterWebviewPlugin();
    
      double lineProgress = 0.0;
    
      initState() {
        super.initState();
        _webViewPlugin.onProgressChanged.listen((progress) {
          print(progress);
          setState(() {
            lineProgress = progress;
          });
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return WebviewScaffold(
          appBar: _setTitle(context),
          url: widget.url,
          mediaPlaybackRequiresUserGesture: false,
          withZoom: false,
          withLocalStorage: true,
          hidden: true,
        );
      }
    
      _progressBar(double progress, BuildContext context) {
        return Container(
          child: LinearProgressIndicator(
            backgroundColor: Colors.blueAccent.withOpacity(0),
            value: progress == 1.0 ? 0 : progress,
            valueColor: new AlwaysStoppedAnimation<Color>(Colors.lightBlue),
          ),
          height: 1,
        );
      }
    
      _setTitle(context) {
        return AppBar(
          brightness: Brightness.light,
          title: Text(title, style: TextStyle(color: Colors.black, fontSize: 20)),
          elevation: 1,
          leading: IconButton(
              icon: Icon(Icons.arrow_back_ios, color: Colors.black),
              onPressed: () => Navigator.pop(context)),
          backgroundColor: Colors.white,
          centerTitle: true,
          bottom: PreferredSize(
            child: _progressBar(lineProgress, context),
            preferredSize: Size.fromHeight(0.1),
          ),
        );
      }
    
      @override
      void dispose() {
        _webViewPlugin.dispose();
        super.dispose();
      }
    }
    
    

    更多详解:
    喜欢可以加@群号:913934649

    简书: https://www.jianshu.com/u/88db5f15770d

    csdn:https://me.csdn.net/beyondforme

    掘金:https://juejin.im/user/5e09a9e86fb9a016271294a7

    相关文章

      网友评论

          本文标题:flutter webview 带进度条,加载框

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