62 lines
1.9 KiB
Dart
Executable File
62 lines
1.9 KiB
Dart
Executable File
import 'package:flutter/material.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:connectivity_plus/connectivity_plus.dart';
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
|
|
/// 网络权限工具类
|
|
class KRNetworkPermission {
|
|
/// 检查网络权限
|
|
static Future<bool> kr_checkNetworkPermission() async {
|
|
if (GetPlatform.isIOS) {
|
|
// iOS 检查网络权限
|
|
final status = await Permission.location.status;
|
|
if (!status.isGranted) {
|
|
return false;
|
|
}
|
|
} else if (GetPlatform.isAndroid) {
|
|
// Android 检查网络权限
|
|
final connectivityResult = await Connectivity().checkConnectivity();
|
|
if (connectivityResult == ConnectivityResult.none) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/// 请求网络权限
|
|
static Future<bool> kr_requestNetworkPermission() async {
|
|
if (GetPlatform.isIOS) {
|
|
// iOS 请求网络权限
|
|
final status = await Permission.location.request();
|
|
if (!status.isGranted) {
|
|
// 直接打开设置页面
|
|
await openAppSettings();
|
|
}
|
|
return status.isGranted;
|
|
} else if (GetPlatform.isAndroid) {
|
|
// Android 请求网络权限
|
|
final connectivityResult = await Connectivity().checkConnectivity();
|
|
if (connectivityResult == ConnectivityResult.none) {
|
|
// 直接打开设置页面
|
|
await openAppSettings();
|
|
}
|
|
return connectivityResult != ConnectivityResult.none;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/// 检查网络连接状态
|
|
static Future<bool> kr_checkNetworkConnection() async {
|
|
try {
|
|
final connectivityResult = await Connectivity().checkConnectivity();
|
|
return connectivityResult != ConnectivityResult.none;
|
|
} catch (e) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// 监听网络状态变化
|
|
static Stream<ConnectivityResult> kr_networkStream() {
|
|
return Connectivity().onConnectivityChanged;
|
|
}
|
|
} |