app_config_service.dart 8.7 KB

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