123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- 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<Null> 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<bool> 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<Permission, PermissionStatus> 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;
- }
- }
|