import 'package:permission_handler/permission_handler.dart'; import 'package:vibration/vibration.dart'; /// 通用工具类 class Utils { /// 静态常量定义区 static const int DefaultVibrateDuration = 70; // 默认震动时长 static const timeout = const Duration(seconds: 1); static final RegExp ssidPattern = RegExp(r'^\"(.*)\"$'); static final RegExp cellNoPattern = RegExp(r'^1[3-9]\d{9}$'); static final RegExp numberPattern = RegExp('[0-9]'); static final RegExp namePattern = RegExp(r'[a-z,A-Z,0-9|\s]'); static final RegExp passwordPattern = RegExp( '[a-z,A-Z,0-9|@|#|\$|%|&|-|+|(|)|.|,|\'|:|;|!|?|/|\||*|"|=|{|}|[|\\]|\\\\|_|`|~|\\^|<|\\>]'); static final RegExp versionPattern = RegExp(r'(\d+)\.*(\d*)\.*(\d*)'); /// 手机号码格式检查 static bool testCellNo(String value) { if (value.isEmpty) { return false; } return cellNoPattern.hasMatch(value); } /// 震动 static Future vibrate({duration = DefaultVibrateDuration}) async { bool? hasVibrator = await Vibration.hasVibrator(); if (null != hasVibrator && !hasVibrator) { return; } bool? hasAmplitudeControl = await Vibration.hasAmplitudeControl(); if (null != hasAmplitudeControl && !hasAmplitudeControl) { Vibration.vibrate(duration: duration, amplitude: 128); } else { Vibration.vibrate(duration: duration); } } /// 权限申请 static Future checkPermission(Permission permission, {bool init = false}) async { var permissionName = permission.toString(); PermissionStatus permissionStatus = await permission.status; if (permissionStatus.isGranted) { print('$permissionName 已有权限'); return true; } else { bool isShow = await permission.shouldShowRequestRationale; if (init || isShow) { // 申请权限 Map statuses = await [permission].request(); // 申请结果 if (statuses[permission] == PermissionStatus.granted) { print('$permissionName 权限申请成功'); return true; } else { print('$permissionName 权限申请被拒绝 ${statuses[permission]}'); return false; } } else { print('$permissionName 权限已被禁用,请在设置中手动开启重新进入程序'); openAppSettings(); return false; } } } } /// 版本比较工具类 class Version { String origin = ""; // 原版本字符串 int major = -1; // 主版本 int secondary = -1; // 次要版本 int revision = -1; // 修订号 Version(String origin) { this.origin = ""; this.major = -1; this.secondary = -1; this.revision = -1; if (origin.isEmpty) { return; } origin = origin.trim(); this.origin = origin; var m = Utils.versionPattern.firstMatch(origin); if (null == m || m.groupCount < 2) { return; } var str = m.group(1); var v = int.tryParse(str!); if (null != v) { this.major = v; } if (m.groupCount > 1) { str = m.group(2); v = int.tryParse(str!); if (null != v) { this.secondary = v; } } if (m.groupCount > 2) { str = m.group(3); v = int.tryParse(str!); if (null != v) { this.revision = v; } } } bool moreThan(Version obj) { if (obj.major < 0 || this.major < 0) { return false; } if (obj.major != this.major) { return this.major > obj.major; } if (obj.secondary != this.secondary) { return this.secondary > obj.secondary; } return this.revision > obj.revision; } }