如何完全重启我的 Flutter 应用程序?

时间:2020-12-20 11:33:34

标签: flutter

当我的应用失去互联网时,我想显示一个显示“无互联网”的屏幕。直到这里它工作。但我的 webview 不会重新加载。因为当我点击重新连接按钮时,我想完全重新启动我的应用程序。 我试图用 Phoenix 包来做到这一点,但它不起作用。 有谁知道我做错了什么?或者有人有更好的解决方案吗? 这是我的源代码:

import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:flutter/services.dart';
import 'dart:async';
import 'package:connectivity/connectivity.dart';
import 'package:flutter_phoenix/flutter_phoenix.dart';

void main() {
  runApp(Phoenix(
      child: MaterialApp(
        home: Herospace(),
        debugShowCheckedModeBanner: false,
      ),
  ),);
  SystemChrome.setEnabledSystemUIOverlays([]);
}

class Herospace extends StatefulWidget {
  @override
  _HerospaceState createState() => _HerospaceState();
}

class _HerospaceState extends State<Herospace> {
  final keyIsFirstLoaded = 'is_first_loaded';

  

  Completer<WebViewController> _controller = Completer<WebViewController>();

  var connectionStatus = false;

  Future<bool> check() async {
    var connectivityResult = await (Connectivity().checkConnectivity());
    if (connectivityResult == ConnectivityResult.mobile) {
      setState(() {
        connectionStatus = true;
      });
    } else if (connectivityResult == ConnectivityResult.wifi) {
      setState(() {
        connectionStatus = true;
      });
    } else {
      setState(() {
        connectionStatus = false;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
        future: check(), // a previously-obtained Future or null
        builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
          if (connectionStatus == true) {
            //if Internet is connected
            return SafeArea(
              bottom: false,
              left: false,
              right: false,
              child: Scaffold(
                body: WebView(
                  initialUrl: 'https://herospace.ch',
                  javascriptMode: JavascriptMode.unrestricted,
                  onWebViewCreated: (WebViewController webViewController) {
                    _controller.complete(webViewController);
                  },
                ),
                floatingActionButton:
                    NavigationControls(_controller.future), // <-- added this
              ),
            );
          } else {
            //If internet is not connected
            return Scaffold(
              body: Padding(
                padding: const EdgeInsets.all(50.0),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    Image.asset('assets/images/noWifi.png', height: 250),
                    Text(
                      "Ou, du brauchst Internet um diese App zu nutzen. Bitte schalte es an.",
                      style:
                          TextStyle(fontSize: 17, fontWeight: FontWeight.bold),),
                  FlatButton(
                  color: Colors.red,
                  textColor: Colors.white,
                  child: const Text('Restart'),
                  onPressed: () => Phoenix.rebirth(context),
                ),
                    
                    Text(
                      "Bitte aktiviere das Internet und starte die App erneut.",
                      style: TextStyle(
                        fontSize: 15,
                        fontStyle: FontStyle.italic,
                      ),
                    ),
                    
                  ],
                ),
              ),
            );
          }
        });
  }
}

1 个答案:

答案 0 :(得分:0)

据我所知,connectivity 包有一个流,它在连接状态改变时接收事件。因此,请尝试用 SafeArea 而不是 StreamBuilder 包裹您的 FutureBuilder。因此,每次更改连接状态时,StreamBuilder 都会更新子级。