app_config_service.dart 9.0 KB

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