简体   繁体   中英

WebView in Flutter

I am needing help with Web view in Flutter I have followed so many tutorials even Github and nothing is working I use Visual Studio Code. Please if you can help make sure your code is working all I need is 4 buttons with an icon the phone to work and 3 social sites to link to in Web view. If there is a better option please tell me that is all I need and this will be for IOS and Android.

Thank You

I had created app using webview_flutter with flutter 1.22.0. Maybe this code will help you.

    class _HomePageState extends State<HomeHomePage> {
  final globalKey = GlobalKey<ScaffoldState>();
  final Completer<WebViewController> _controller =
      Completer<WebViewController>();
  WebViewController controller;
  AsyncSnapshot<WebViewController> controllerr;
  int consumer = 0;
  Map _source = {ConnectivityResult.none: false};
  MyConnectivity _connectivity = MyConnectivity.instance;
  @override
  void initState() {
    super.initState();
    if (Platform.isAndroid) WebView.platform = SurfaceAndroidWebView();
    _connectivity.initialise();
    _connectivity.myStream.listen((source) {
      setState(() => _source = source);
    });
  }

  @override
  Widget build(BuildContext context) {
    bool connected = false;

    String string;
    switch (_source.keys.toList()[0]) {
      case ConnectivityResult.none:
        string = "Offline";
        break;
      case ConnectivityResult.mobile:
        string = "Mobile: Online";
        setState(() {
          connected = false;
        });
        break;
      case ConnectivityResult.wifi:
        string = "WiFi: Online";
        setState(() {
          connected = false;
        });
    }
    return !connected && _source.keys.toList()[0] != ConnectivityResult.none
        ? FutureBuilder<WebViewController>(
            future: _controller.future,
            builder: (BuildContext context,
                AsyncSnapshot<WebViewController> controller) {
              final bool webViewReady =
                  controller.connectionState == ConnectionState.done;
              final WebViewController controllerSnap = controller.data;

              return Scaffold(
                  appBar: AppBar(
                    backgroundColor: Colors.blueGrey,
                    toolbarHeight: 0,
                  ),
                  key: globalKey,
                  body: WillPopScope(
                    onWillPop: !webViewReady
                        ? null
                        : () async {
                            var status = await controllerSnap.canGoBack();
                            if (status) {
                              await controllerSnap.goBack();
                              return false;
                            } else {
                              SystemNavigator.pop();
                              return true;
                            }
                          },
                    child: WebView(
                      onWebResourceError: (WebResourceError error) {
                        if (error.description.toString() ==
                            'net::ERR_INTERNET_DISCONNECTED') {
                          setState(() {
                            consumer++;
                            connected = true;
                          });
                        }
                        setState(() {
                          connected = true;
                        });
                      },
                      javascriptMode: JavascriptMode.unrestricted,
                      initialUrl: 'http://...',
                      onWebViewCreated:
                          (WebViewController webViewController) async {
                        if (consumer > 0) {
                        } else {
                          await _controller.complete(webViewController);
                        }
                      },
                      navigationDelegate: (request) {
                        return _buildNavigationDecision(request);
                      },
                    ),
                  ));
            },
          )
        : Scaffold(
            appBar: AppBar(
              toolbarHeight: 0,
            ),
            body: WillPopScope(
              onWillPop: doubleClickBack,
              child: (Center(
                child: Image.asset(
                  'assets/connecction.jpg',
                  fit: BoxFit.cover,
                ),
              )),
            ),
          );
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM