更新
This commit is contained in:
99
lib/common/util/CheckNetwork.dart
Normal file
99
lib/common/util/CheckNetwork.dart
Normal file
@@ -0,0 +1,99 @@
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:connectivity_plus/connectivity_plus.dart';
|
||||
import 'package:ef/ef.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:vbvs_app/common/util/CommonVariables.dart';
|
||||
import 'package:vbvs_app/common/util/MyUtils.dart';
|
||||
import 'package:vbvs_app/pages/common/selectDialog.dart';
|
||||
import 'package:vbvs_app/pages/device_bind/componnet/bind_dialog.dart';
|
||||
|
||||
class Checknetwork {
|
||||
static final Connectivity _connectivity = Connectivity();
|
||||
static StreamSubscription<List<ConnectivityResult>>? subscription;
|
||||
static bool isShowDialog = false;
|
||||
|
||||
static connectivityResultChange(connectivityResult) {
|
||||
if (connectivityResult.contains(ConnectivityResult.mobile)) {
|
||||
print("ConnectivityResult:Mobile network available.");
|
||||
// Mobile network available.
|
||||
toInitData();
|
||||
} else if (connectivityResult.contains(ConnectivityResult.wifi)) {
|
||||
print("ConnectivityResult:Wi-fi is available.");
|
||||
toInitData();
|
||||
// Wi-fi is available.
|
||||
// Note for Android:
|
||||
// When both mobile and Wi-Fi are turned on system will return Wi-Fi only as active network type
|
||||
} else if (connectivityResult.contains(ConnectivityResult.ethernet)) {
|
||||
print("ConnectivityResult:Ethernet connection available.");
|
||||
toInitData();
|
||||
// Ethernet connection available.
|
||||
} else if (connectivityResult.contains(ConnectivityResult.vpn)) {
|
||||
print("ConnectivityResult:Vpn connection active.");
|
||||
toInitData();
|
||||
// Vpn connection active.
|
||||
// Note for iOS and macOS:
|
||||
// There is no separate network interface type for [vpn].
|
||||
// It returns [other] on any device (also simulator)
|
||||
} else if (connectivityResult.contains(ConnectivityResult.bluetooth)) {
|
||||
// Bluetooth connection available.
|
||||
} else if (connectivityResult.contains(ConnectivityResult.other)) {
|
||||
// Connected to a network which is not in the above mentioned networks.
|
||||
print(
|
||||
"ConnectivityResult:Connected to a network which is not in the above mentioned networks");
|
||||
toInitData();
|
||||
} else if (connectivityResult.contains(ConnectivityResult.none)) {
|
||||
// No available network types
|
||||
print("ConnectivityResult:No available network types");
|
||||
}
|
||||
}
|
||||
|
||||
static toInitData() async {
|
||||
CommonVariables.isNetWorkOn = true;
|
||||
if (isShowDialog) {
|
||||
Get.back();
|
||||
isShowDialog = false;
|
||||
}
|
||||
}
|
||||
|
||||
static checkNetwork() async {
|
||||
// Platform messages may fail, so we use a try/catch PlatformException.
|
||||
try {
|
||||
connectivityResultChange(await (_connectivity.checkConnectivity()));
|
||||
} on PlatformException catch (e) {
|
||||
print("Couldn't check connectivity status:Error:$e");
|
||||
}
|
||||
// 网络状态变化监听,登录成功后移出监听
|
||||
subscription = _connectivity.onConnectivityChanged
|
||||
.listen((List<ConnectivityResult> connectivityResult) {
|
||||
connectivityResultChange(connectivityResult);
|
||||
});
|
||||
if (CommonVariables.isNetWorkOn == false) {
|
||||
int index = 0;
|
||||
Timer.periodic(const Duration(seconds: 1), (t) {
|
||||
index++;
|
||||
if (index > 40 || CommonVariables.isNetWorkOn == true) {
|
||||
t.cancel();
|
||||
} else if (index > 20 && isShowDialog == false) {
|
||||
t.cancel();
|
||||
var context = Get.context;
|
||||
String name = "";
|
||||
if (Platform.isAndroid) {
|
||||
name = "安卓启用网络提示".tr;
|
||||
} else {
|
||||
name = "ios启用网络提示".tr;
|
||||
}
|
||||
// isShowDialog = true;
|
||||
// showCustomConfirmDialog(context!, name).then((d) {
|
||||
// isShowDialog = false;
|
||||
// });
|
||||
showTipDialog(context!,Text(name,style: TextStyle(
|
||||
color: themeController.currentColor.sc3,
|
||||
),));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user