app_config_service.dart 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. import 'dart:ui';
  2. import 'package:device_info_plus/device_info_plus.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:get/get.dart';
  5. import 'package:package_info_plus/package_info_plus.dart';
  6. import 'package:plugin_basic/service/config_services_injection.dart';
  7. import 'package:plugin_platform/engine/sp/sp_util.dart';
  8. import 'package:shared/utils/device_utils.dart';
  9. import 'package:shared/utils/log_utils.dart';
  10. import 'package:shared/utils/screen_util.dart';
  11. import '../constants/app_constant.dart';
  12. /*
  13. * 获取并保存当前应用的信息。
  14. * 获取并保存当前设备的信息。
  15. */
  16. class ConfigService extends GetxService {
  17. static ConfigService get to => Get.find();
  18. //选择的国家,默认新加坡
  19. RxInt selectCountry = 1.obs; //0 越南 1 新加坡 2 英国 3马来 4荷兰 5泰国
  20. bool isTHOAType = false; //泰国的模块当前选择的是ER还是OA类型,默认为ER类型
  21. // 设备信息
  22. /// android 设备信息
  23. AndroidDeviceInfo? androidDeviceInfo;
  24. /// ios 设备信息
  25. IosDeviceInfo? iosDeviceInfo;
  26. // 当前的languageCode
  27. Locale locale = PlatformDispatcher.instance.locale;
  28. PackageInfo? _appPackageInfo;
  29. String get version => _appPackageInfo?.version ?? '-';
  30. String get appName => _appPackageInfo?.appName ?? '-';
  31. String get packageName => _appPackageInfo?.packageName ?? '-';
  32. String get buildNumber => _appPackageInfo?.buildNumber ?? '-';
  33. final RxBool _isDarkModel = Get.isDarkMode.obs;
  34. // 是否是暗黑模式
  35. bool get isDarkModel => _isDarkModel.value;
  36. // 顶部安全距离(pt)
  37. double get safeAreaPaddingTop => _safeAreaPaddingTop!;
  38. // 底部安全距离(pt)
  39. double get safeAreaPaddingBottom => _safeAreaPaddingBottom!;
  40. double? _safeAreaPaddingTop;
  41. double? _safeAreaPaddingBottom;
  42. //屏幕大小
  43. Size? mSize;
  44. //密度
  45. double? mRatio;
  46. //设备像素px
  47. double? width;
  48. double? height;
  49. //设备宽高比,大于1.9是全面屏手机
  50. double whRatio = 1;
  51. //是否是全面屏设备(宽高比大于1.9)
  52. bool isFullScreenDevice = false;
  53. // 上下边距 (主要用于 刘海 和 内置导航键)
  54. double? topPadding;
  55. double? bottomPadding;
  56. double? textScaleFactor;
  57. Brightness? platformBrightness;
  58. EdgeInsets? viewInsets;
  59. EdgeInsets? padding;
  60. bool? alwaysUse24HourFormat;
  61. bool? accessibleNavigation;
  62. bool? invertColors;
  63. bool? disableAnimations;
  64. bool? boldText;
  65. Orientation? orientation;
  66. @override
  67. void onInit() {
  68. super.onInit();
  69. }
  70. @override
  71. void onReady() {
  72. super.onReady();
  73. //赋值选中的国家(新加坡)
  74. int country = SPUtil.getInt(AppConstant.storageSelectedCountry, defValue: 1) ?? 1;
  75. selectCountry.value = country;
  76. //打印应用与设备的信息
  77. getDeviceInfo();
  78. getAppInfos();
  79. //打印当前设备的一些Media信息
  80. getMediaInfo(context: Get.context!);
  81. paddingSizeTop(context: Get.context!);
  82. paddingSizeBottom(context: Get.context!);
  83. }
  84. // ServicesInjection 已经获取到了信息,直接赋值过来并打印当前的应用信息
  85. Future<void> getAppInfos() async {
  86. // package_info 插件获取到当前运行平台上App的包信息
  87. _appPackageInfo = ConfigServicesInjection.packageInfo;
  88. Log.d('package_info 插件获获取到的app【appName】$appName');
  89. Log.d('package_info 插件获获取到的app【packageName】$packageName');
  90. Log.d('package_info 插件获获取到的app【version】$version');
  91. Log.d('package_info 插件获获取到的app【buildNumber】$buildNumber');
  92. }
  93. // ServicesInjection 已经获取到了信息,直接赋值过来并打印当前的设备信息
  94. void getDeviceInfo() {
  95. if (DeviceUtils.isIOS) {
  96. iosDeviceInfo = ConfigServicesInjection.iosDeviceInfo;
  97. Log.d('device_info_plus插件获取到设备信息【iosDeviceInfo】$iosDeviceInfo');
  98. } else if (DeviceUtils.isAndroid) {
  99. androidDeviceInfo = ConfigServicesInjection.androidDeviceInfo;
  100. Log.d('device_info_plus插件获取到设备信息【androidDeviceInfo】$androidDeviceInfo');
  101. }
  102. }
  103. // 获取当前的languageCode
  104. void initLocale() {}
  105. // 更换当前的languageCode
  106. void onLocaleUpdate(Locale value) {
  107. locale = value;
  108. Get.updateLocale(value);
  109. Log.d('glabalService-app_config_service.dart-更换后的locale【locale】$locale');
  110. }
  111. // 切换主题模式 dark(暗黑) / light(明亮)
  112. Future<void> switchThemeModel() async {
  113. _isDarkModel.value = !_isDarkModel.value;
  114. // Get.changeTheme(
  115. // _isDarkModel.value == true ? AppTheme.dark : AppTheme.light,
  116. // );
  117. await Future.delayed(const Duration(seconds: 1));
  118. }
  119. // 获取media相关信息
  120. void getMediaInfo({BuildContext? context}) {
  121. //屏幕大小
  122. mSize = MediaQuery.of(context!).size;
  123. //密度
  124. mRatio = MediaQuery.of(context).devicePixelRatio;
  125. //设备像素 px
  126. width = mSize!.width * mRatio!;
  127. height = mSize!.height * mRatio!;
  128. whRatio = (height ?? 1) / (width ?? 1);
  129. isFullScreenDevice = whRatio > 1.9;
  130. // 上下边距 (主要用于 刘海 和 内置导航键)
  131. topPadding = MediaQuery.of(context).padding.top;
  132. bottomPadding = MediaQuery.of(context).padding.bottom;
  133. textScaleFactor = MediaQuery.of(context).textScaleFactor;
  134. platformBrightness = MediaQuery.of(context).platformBrightness;
  135. viewInsets = MediaQuery.of(context).viewInsets;
  136. padding = MediaQuery.of(context).padding;
  137. alwaysUse24HourFormat = MediaQuery.of(context).alwaysUse24HourFormat;
  138. accessibleNavigation = MediaQuery.of(context).accessibleNavigation;
  139. invertColors = MediaQuery.of(context).invertColors;
  140. disableAnimations = MediaQuery.of(context).disableAnimations;
  141. boldText = MediaQuery.of(context).boldText;
  142. orientation = MediaQuery.of(context).orientation;
  143. final screenDensity = MediaQuery.devicePixelRatioOf(context);
  144. //初始化Flustarts工具类中 ScreenUtils 工具,可以设置设计稿的大小,可以选择性的使用 getAdapterSizeCtx 适配大小屏不同尺寸的展示
  145. setDesignWHD(375, 667, density: screenDensity);
  146. Log.d('service-app_config_service.dart---设备信息【MediaQuery.of(context)】:${MediaQuery.of(context)}');
  147. Log.d('service-app_config_service.dart---设备像素比dpr(物理像素/逻辑像素)【mRatio】:$mRatio');
  148. Log.d('service-app_config_service.dart--- ScreenUtil像素比: $screenDensity');
  149. Log.d('service-app_config_service.dart---逻辑像素pt【mSize.width * mSize.height】:${mSize!.width} x ${mSize!.height}');
  150. Log.d('service-app_config_service.dart---屏幕分辨率px(物理像素)【mSize.width * mRatio】 X 【mSize.height * mRatio】:$width x $height '
  151. '屏幕的宽高比:$whRatio');
  152. Log.d('service-app_config_service.dart---上边刘海(状态栏)【MediaQuery.of(context).padding.top】:$topPadding');
  153. Log.d('service-app_config_service.dart---下边导航【MediaQuery.of(context).padding.bottom】:$bottomPadding');
  154. Log.d('service-app_config_service.dart---textScaleFactor【MediaQuery.of(context).textScaleFactor】: $textScaleFactor');
  155. Log.d('service-app_config_service.dart---platformBrightness【MediaQuery.of(context).platformBrightness】: $platformBrightness');
  156. Log.d('service-app_config_service.dart---viewInsets【MediaQuery.of(context).viewInsets】: $viewInsets');
  157. Log.d('service-app_config_service.dart---padding【MediaQuery.of(context).padding】: $padding');
  158. Log.d('service-app_config_service.dart---alwaysUse24HourFormat: $alwaysUse24HourFormat');
  159. Log.d('service-app_config_service.dart---accessibleNavigation: $accessibleNavigation');
  160. Log.d('service-app_config_service.dart---invertColors: $invertColors');
  161. Log.d('service-app_config_service.dart---disableAnimations: $disableAnimations');
  162. Log.d('service-app_config_service.dart---boldText: $boldText');
  163. Log.d('service-app_config_service.dart---orientation: $orientation');
  164. }
  165. double paddingSizeBottom({required BuildContext context}) {
  166. final MediaQueryData data = MediaQuery.of(context);
  167. EdgeInsets padding = data.padding;
  168. padding = padding.copyWith(bottom: data.viewPadding.bottom);
  169. Log.d("glabalService-config.dart该设备底部的安全距离为----${padding.bottom}");
  170. _safeAreaPaddingBottom = padding.bottom;
  171. return padding.bottom;
  172. }
  173. double paddingSizeTop({required BuildContext context}) {
  174. final MediaQueryData data = MediaQuery.of(context);
  175. EdgeInsets padding = data.padding;
  176. padding = padding.copyWith(top: data.viewPadding.top);
  177. Log.d("glabalService-config.dart该设备顶部的安全距离为----${padding.top}");
  178. _safeAreaPaddingTop = padding.top;
  179. return padding.top;
  180. }
  181. int compareVersionNumbers(String version1, String version2) {
  182. List<int> v1 = version1.split('.').map(int.parse).toList();
  183. List<int> v2 = version2.split('.').map(int.parse).toList();
  184. int minLength = v1.length < v2.length ? v1.length : v2.length;
  185. for (int i = 0; i < minLength; i++) {
  186. if (v1[i] < v2[i]) {
  187. return -1; // version1 < version2
  188. } else if (v1[i] > v2[i]) {
  189. return 1;
  190. }
  191. }
  192. if (v1.length < v2.length) {
  193. return -1;
  194. } else if (v1.length > v2.length) {
  195. return 1; // version1 > version2
  196. }
  197. return 0; //version1 = version2
  198. }
  199. }