Android 在Flatter WebView中检查连接并强制刷新

Android 在Flatter WebView中检查连接并强制刷新,android,flutter,flutter-layout,flutter-web,Android,Flutter,Flutter Layout,Flutter Web,我还是个新手,还没开始尝试一些新东西。我正在继续我的FlitterWebViewpersonal project应用程序。它有5个选项卡,每个选项卡在脚手架中包含一个WebView。我想要的是: 检测用户点击选项卡(例如:主页)的时间,然后将其保存到变量中 保存后,用户退出主页选项卡并重新进入时,应用程序将通过与变量中存储的时间进行比较,检查用户离开选项卡的时间是否超过1分钟 如果超过1分钟: 检查连接 如果连接可用,则对WebView执行强制刷新 如果没有可用的连接,则显示一个对话框,说明

我还是个新手,还没开始尝试一些新东西。我正在继续我的Flitter
WebView
personal project应用程序。它有5个选项卡,每个选项卡在脚手架中包含一个WebView。我想要的是:

  • 检测用户点击选项卡(例如:主页)的时间,然后将其保存到
    变量中
  • 保存后,用户退出主页选项卡并重新进入时,应用程序将通过与
    变量中存储的时间进行比较,检查用户离开选项卡的时间是否超过1分钟
    如果超过1分钟:
    
    • 检查连接
      • 如果连接可用,则对WebView执行强制刷新
      • 如果没有可用的连接,则显示一个对话框,说明“无连接” 如果不超过1分钟:
    • 应用程序将继续,不会调用任何内容
  • 到目前为止我试过的是

    var lastloaded                  //the variable where the access time of the tab will be saved
    
      void tappingOnTab() {         //the function that will store that data and check if its > 1 min
        final now = DateTime.now();
        if(lastloaded != null){
          if(now.difference(lastloaded).inMinutes > 1) {
            _checkConnection();
          }
        lastloaded = now;
        }
      }
    
    
      _checkConnection() async {      //the function that will check for connection if its greater than 1 min
        var result = await (Connectivity().checkConnectivity());
        if(result == ConnectivityResult.none) {
          _showDialog("No internet", "You are not connected to the internet");
        } else if (result == ConnectivityResult.mobile) {
          _showDialog("Internet Access", "You are connected over mobile data");
        } else if (result == ConnectivityResult.wifi) {
          _showDialog("Internet Access", "You are connected over wifi");
        }
      }
    
      _showDialog(title, text) {
        showDialog(
          context: context,
          builder: (context) {
            return AlertDialog(
              title: Text(title),
              content: Text(text),
              actions: <Widget>[
                FlatButton(
                  child: Text("Ok"),
                  onPressed: () {
                    Navigator.of(context).pop();
                  },
                )
              ],
            );
          }
        );
      }
    }
    

    您应该能够在AlertDialog中的相同onPressed方法上使用控制器刷新WebView。
    import 'dart:async';
    import 'dart:io';
    import 'package:firebase_messaging/firebase_messaging.dart';
    import 'package:flutter/material.dart';
    import 'package:syncshop_webview/widgets/notification_widget.dart';
    import 'package:webview_flutter/webview_flutter.dart';
    import 'package:connectivity/connectivity.dart';
    
    
    class HomePage extends StatefulWidget {
      // HomePage({Key key}) : super(key: key);
    
      @override
      _HomePageState createState() => _HomePageState();
    }
    
    class _HomePageState extends State<HomePage> {
      final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
      // WebViewController myController;
      final flutterWebviewPlugin = new WebView();
    var lastloaded;
    
      _exitApp(BuildContext context, Future<WebViewController> controller) async {
        controller.then((data) async {
          WebViewController controller = data;
          var goBack = await controller.canGoBack();
          if (goBack == true) {
            print("onwill go back");
            controller.goBack();
          } else {
            print("onwill not go back");
            Navigator.pop(context);
          }
        });
      }
    
      @override
      void initState() {
        super.initState();
        firebaseCloudMessagingListeners();
      }
    
      void firebaseCloudMessagingListeners() {
        if (Platform.isIOS) iOSPermission();
    
        _firebaseMessaging.getToken().then((token) {
          print(token);
        });
    
        _firebaseMessaging.configure(
          onMessage: (Map<String, dynamic> message) async {
            setState(() {
              print("${message['data']['url']}");
              Navigator.push(
                  context,
                  MaterialPageRoute(
                      builder: (BuildContext context) => NotificationClicked()));
            });
          },
          onResume: (Map<String, dynamic> message) async {
            print("${message['data']['url']}");
          },
          onLaunch: (Map<String, dynamic> message) async {
            print("${message['data']['url']}");
          },
        );
      }
    
      void iOSPermission() {
        _firebaseMessaging.requestNotificationPermissions(
            IosNotificationSettings(sound: true, badge: true, alert: true));
        _firebaseMessaging.onIosSettingsRegistered
            .listen((IosNotificationSettings settings) {
          print("Settings registered: $settings");
        });
      }
    
    
    
      Completer<WebViewController> _controller = Completer<WebViewController>();
    
      void tappingOnTab() {
        final now = DateTime.now();
        if(lastloaded != null){
          if(now.difference(lastloaded).inMinutes > 1) {
            _checkConnection();
          }
        lastloaded = now;
        }
      }
    
      @override
      Widget build(BuildContext context) {
        return WillPopScope(
            onWillPop: () => _exitApp(context, _controller.future),
            child: SafeArea(
        child: Scaffold(
            body: WebView(
        initialUrl: 'https://syncshop.online/en/',
        javascriptMode: JavascriptMode.unrestricted,
        onWebViewCreated: (controller) {
        _controller.complete(controller);
        },
        onPageFinished: (controller) async {
          //SOME INJECTION OF CODE HERE
        },
              ),
          //floating action button here
          ),
            ),
          );
      }
      _checkConnection() async {
        var result = await (Connectivity().checkConnectivity());
        if(result == ConnectivityResult.none) {
          _showDialog("No internet", "You are not connected to the internet");
        } else if (result == ConnectivityResult.mobile) {
          _showDialog("Internet Access", "You are connected over mobile data");
        } else if (result == ConnectivityResult.wifi) {
          _showDialog("Internet Access", "You are connected over wifi");
        }
      }
    
      _showDialog(title, text) {
        showDialog(
          context: context,
          builder: (context) {
            return AlertDialog(
              title: Text(title),
              content: Text(text),
              actions: <Widget>[
                FlatButton(
                  child: Text("Ok"),
                  onPressed: () {
                    Navigator.of(context).pop();
                  },
                )
              ],
            );
          }
        );
      }
    }