feat: 工具类提交。
This commit is contained in:
1 parent
a2b38fa1e8
commit
557f900187
20 files changed
+2026
-3
No files matched your search
@@ -0,0 +1,177 @@
|
||||
import 'dart:io';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:android_id/android_id.dart';
|
||||
import 'package:device_info_plus/device_info_plus.dart';
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
import 'package:novyronst/common/api/api_string.dart';
|
||||
import 'package:package_info_plus/package_info_plus.dart';
|
||||
|
||||
@immutable
|
||||
class NyDeviceInfo {
|
||||
final String deviceId;
|
||||
final String systemType;
|
||||
final String systemVersion;
|
||||
final String brand;
|
||||
final String model;
|
||||
|
||||
final String appName;
|
||||
final String appPackageName;
|
||||
final String appVersion;
|
||||
final String idfv;
|
||||
|
||||
static NyDeviceInfo? _instance;
|
||||
|
||||
const NyDeviceInfo._({
|
||||
required this.deviceId,
|
||||
required this.systemType,
|
||||
required this.systemVersion,
|
||||
required this.brand,
|
||||
required this.model,
|
||||
required this.appName,
|
||||
required this.appPackageName,
|
||||
required this.appVersion,
|
||||
required this.idfv,
|
||||
});
|
||||
|
||||
static NyDeviceInfo get instance {
|
||||
if (_instance == null) {
|
||||
throw StateError('error');
|
||||
}
|
||||
return _instance!;
|
||||
}
|
||||
|
||||
@visibleForTesting
|
||||
static void setMockInstance({
|
||||
String deviceId = 'test-device-id',
|
||||
String systemType = 'test',
|
||||
String systemVersion = '1.0',
|
||||
String brand = 'test',
|
||||
String model = 'test',
|
||||
String appName = 'Glimzo',
|
||||
String appPackageName = 'com.example.skywood',
|
||||
String appVersion = '1.0.0',
|
||||
String idfv = 'test-idfv',
|
||||
}) {
|
||||
_instance = NyDeviceInfo._(
|
||||
deviceId: deviceId,
|
||||
systemType: systemType,
|
||||
systemVersion: systemVersion,
|
||||
brand: brand,
|
||||
model: model,
|
||||
appName: appName,
|
||||
appPackageName: appPackageName,
|
||||
appVersion: appVersion,
|
||||
idfv: idfv,
|
||||
);
|
||||
}
|
||||
|
||||
/// 初始化设备信息
|
||||
static Future<void> init() async {
|
||||
if (_instance != null) return;
|
||||
|
||||
String deviceId = '',
|
||||
systemType = '',
|
||||
systemVersion = '',
|
||||
brand = '',
|
||||
model = '',
|
||||
appName = 'Glimzo',
|
||||
appPackageName = '',
|
||||
appVersion = '',
|
||||
idfv = '';
|
||||
|
||||
// 获取应用信息
|
||||
await _initAppInfo()
|
||||
.then((info) {
|
||||
appPackageName = info['packageName'] ?? '';
|
||||
appVersion = info['version'] ?? '';
|
||||
appName = ApiString.appName;
|
||||
})
|
||||
.catchError((e) {
|
||||
debugPrint('[sDeviceInfo] getElPackageName: $e');
|
||||
});
|
||||
|
||||
// 获取设备信息
|
||||
if (Platform.isAndroid) {
|
||||
await _initAndroidInfo()
|
||||
.then((info) {
|
||||
deviceId = info['deviceId'] ?? '';
|
||||
systemType = 'android';
|
||||
systemVersion = info['systemVersion'] ?? '';
|
||||
brand = info['brand'] ?? '';
|
||||
model = info['model'] ?? '';
|
||||
})
|
||||
.catchError((e) {
|
||||
debugPrint('[GlimzoUtilDeviceInfo] Android: $e');
|
||||
});
|
||||
} else if (Platform.isIOS) {
|
||||
await _initIOSInfo()
|
||||
.then((info) {
|
||||
deviceId = info['deviceId'] ?? '';
|
||||
idfv = info['idfv'] ?? '';
|
||||
systemType = 'ios';
|
||||
systemVersion = info['systemVersion'] ?? '';
|
||||
brand = info['brand'] ?? '';
|
||||
model = info['model'] ?? '';
|
||||
})
|
||||
.catchError((e) {
|
||||
debugPrint('[GlimzoUtilDeviceInfo] iOS: $e');
|
||||
});
|
||||
}
|
||||
|
||||
_instance = NyDeviceInfo._(
|
||||
deviceId: deviceId,
|
||||
systemType: systemType,
|
||||
systemVersion: systemVersion,
|
||||
brand: brand,
|
||||
model: model,
|
||||
appName: appName,
|
||||
appPackageName: appPackageName,
|
||||
appVersion: appVersion,
|
||||
idfv: idfv,
|
||||
);
|
||||
}
|
||||
|
||||
/// 获取应用信息
|
||||
static Future<Map<String, String>> _initAppInfo() async {
|
||||
final packageInfo = await PackageInfo.fromPlatform();
|
||||
return {
|
||||
'packageName': packageInfo.packageName,
|
||||
'version': packageInfo.version,
|
||||
};
|
||||
}
|
||||
|
||||
/// 获取 Android 设备信息
|
||||
static Future<Map<String, String?>> _initAndroidInfo() async {
|
||||
final plugin = DeviceInfoPlugin();
|
||||
final androidInfo = await plugin.androidInfo;
|
||||
final androidIdPlugin = AndroidId();
|
||||
final androidId = await androidIdPlugin.getId();
|
||||
return {
|
||||
'deviceId': androidId ?? androidInfo.id,
|
||||
'systemVersion': androidInfo.version.release,
|
||||
'brand': androidInfo.brand,
|
||||
'model': androidInfo.model,
|
||||
};
|
||||
}
|
||||
|
||||
/// 获取 iOS 设备信息
|
||||
static Future<Map<String, String?>> _initIOSInfo() async {
|
||||
final plugin = DeviceInfoPlugin();
|
||||
final iosInfo = await plugin.iosInfo;
|
||||
final secureStorage = FlutterSecureStorage();
|
||||
String? idfvStore = await secureStorage.read(key: 'identifierForVendor');
|
||||
idfvStore ??= iosInfo.identifierForVendor;
|
||||
|
||||
if (idfvStore != null) {
|
||||
await secureStorage.write(key: 'identifierForVendor', value: idfvStore);
|
||||
}
|
||||
|
||||
return {
|
||||
'deviceId': idfvStore,
|
||||
'idfv': iosInfo.identifierForVendor ?? '',
|
||||
'systemVersion': iosInfo.systemVersion,
|
||||
'brand': iosInfo.model,
|
||||
'model': iosInfo.modelName,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
|
||||
import 'package:novyronst/common/help/help_image.dart';
|
||||
|
||||
// 加载数据的函数类型
|
||||
typedef LoadBlock<T> = Future<T> Function();
|
||||
|
||||
// 弹出Toast提示的类
|
||||
class AppToastUtil {
|
||||
static const String _defaultLoadingText = "";
|
||||
static const String _loadingText = "loading...";
|
||||
|
||||
/// 显示普通 Toast 提示
|
||||
static Future<void> showToast(
|
||||
Object msg, {
|
||||
Duration displayTime = const Duration(seconds: 2),
|
||||
}) async {
|
||||
await dismiss();
|
||||
return SmartDialog.showToast(
|
||||
msg.toString(),
|
||||
alignment: Alignment.center,
|
||||
debounce: true,
|
||||
displayTime: displayTime,
|
||||
);
|
||||
}
|
||||
|
||||
static Future<void> showError(
|
||||
Object msg, {
|
||||
Duration displayTime = const Duration(seconds: 2),
|
||||
}) {
|
||||
return showFail(msg, displayTime: displayTime);
|
||||
}
|
||||
|
||||
/// 显示失败提示
|
||||
static Future<void> showFail(
|
||||
Object msg, {
|
||||
Duration displayTime = const Duration(seconds: 2),
|
||||
Function()? onDismiss,
|
||||
}) async {
|
||||
await dismiss();
|
||||
return SmartDialog.showNotify(
|
||||
msg: msg.toString(),
|
||||
alignment: Alignment.center,
|
||||
notifyType: NotifyType.failure,
|
||||
displayTime: displayTime,
|
||||
debounce: true,
|
||||
onDismiss: onDismiss,
|
||||
);
|
||||
}
|
||||
|
||||
/// 显示成功提示
|
||||
static Future<T?> showSuccess<T>(
|
||||
Object msg, {
|
||||
Duration displayTime = const Duration(seconds: 2),
|
||||
}) async {
|
||||
await dismiss();
|
||||
return SmartDialog.showNotify<T>(
|
||||
msg: msg.toString(),
|
||||
notifyType: NotifyType.success,
|
||||
debounce: true,
|
||||
displayTime: displayTime,
|
||||
);
|
||||
}
|
||||
|
||||
static Future<T?> showInfo<T>(
|
||||
Object msg, {
|
||||
Duration displayTime = const Duration(seconds: 2),
|
||||
}) {
|
||||
return showWarning<T>(msg, displayTime: displayTime);
|
||||
}
|
||||
|
||||
static Future<T?> showWarning<T>(
|
||||
Object msg, {
|
||||
Duration displayTime = const Duration(seconds: 2),
|
||||
}) async {
|
||||
await dismiss();
|
||||
return SmartDialog.showNotify<T>(
|
||||
msg: msg.toString(),
|
||||
notifyType: NotifyType.warning,
|
||||
debounce: true,
|
||||
displayTime: displayTime,
|
||||
);
|
||||
}
|
||||
|
||||
/// 显示 Lottie 加载提示
|
||||
static Future<T?> showLottieLoading<T>({
|
||||
Duration? displayTime,
|
||||
}) async {
|
||||
await dismiss();
|
||||
return SmartDialog.showLoading<T>(
|
||||
displayTime: displayTime ?? const Duration(seconds: 5),
|
||||
maskColor: Colors.transparent,
|
||||
builder: (_) {
|
||||
return SizedBox(
|
||||
width: 120,
|
||||
height: 120,
|
||||
// child: Image.asset(ConstImage.loading, fit: BoxFit.fill),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/// 显示加载提示
|
||||
static Future<T?> showLoading<T>({
|
||||
String text = _loadingText,
|
||||
Duration displayTime = const Duration(seconds: 10),
|
||||
}) async {
|
||||
await dismiss();
|
||||
return SmartDialog.showLoading<T>(msg: text, displayTime: displayTime);
|
||||
}
|
||||
|
||||
/// 关闭特定状态的弹窗
|
||||
static Future<void> dismiss<T>({SmartStatus status = SmartStatus.loading}) {
|
||||
return SmartDialog.dismiss<T>(status: status);
|
||||
}
|
||||
|
||||
/// 关闭所有弹窗(占位)
|
||||
static void dismissAll() {
|
||||
SmartDialog.dismiss();
|
||||
}
|
||||
|
||||
/// 自动显示加载框并在完成后关闭
|
||||
static Future<T> loading<T>(
|
||||
LoadBlock<T> block, {
|
||||
String? text,
|
||||
bool isLoading = true,
|
||||
}) async {
|
||||
if (isLoading) {
|
||||
showLoading(text: text ?? _defaultLoadingText);
|
||||
}
|
||||
|
||||
try {
|
||||
final result = await block();
|
||||
return result;
|
||||
} finally {
|
||||
dismiss();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user