How to add a Webview in Flutter?

前端 未结 7 1623
逝去的感伤
逝去的感伤 2020-12-15 17:43

I know its possible to add a WebView as a full page but couldn\'t find any sample code to do it. I assume you could use a PageView as it\'s base but not sure how to call the

7条回答
  •  温柔的废话
    2020-12-15 18:37

    You can use Flutter webview plugin. Here is the url for the plugin https://pub.dartlang.org/packages/webview_flutter

    Webview with CircularProgressIndicator Here are some screenshots. Before After

    import 'dart:async';
    import 'package:flutter/material.dart';
    import 'package:webview_flutter/webview_flutter.dart';
    
    class WebView extends StatefulWidget {
      @override
      _WebViewState createState() => _WebViewState();
    }
    
    class _WebViewState extends State {
    
      final Completer _controller =
          Completer();
    
      num _stackToView = 1;
    
      void _handleLoad(String value) {
        setState(() {
          _stackToView = 0;
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
            appBar: AppBar(
              leading: Builder(builder: (BuildContext context) {
                return IconButton(
                  icon: Icon(Icons.volume_up, color: Colors.black,),
                  onPressed: () {
                    Navigator.pop(context);
                  },
                );
              }),
              backgroundColor: Colors.white10,
              elevation: 0,
            ),
            body: IndexedStack(
              index: _stackToView,
              children: [
                Column(
                  children: [
                    Expanded(
                        child: WebView(
                      initialUrl: "https://www.google.co.in/",
                      javascriptMode: JavascriptMode.unrestricted,
                      onPageFinished: _handleLoad,
                      onWebViewCreated: (WebViewController webViewController) {
                        _controller.complete(webViewController);
                      },
                    )),
                  ],
                ),
                Container(
                  child: Center(child: CircularProgressIndicator(),)
                ),
              ],
            ));
      }
    }
    

提交回复
热议问题