123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- import 'dart:ui';
- import 'package:device_info_plus/device_info_plus.dart';
- import 'package:flutter/material.dart';
- import 'package:get/get.dart';
- import 'package:package_info_plus/package_info_plus.dart';
- import 'package:plugin_basic/service/config_services_injection.dart';
- import 'package:shared/utils/device_utils.dart';
- import 'package:shared/utils/log_utils.dart';
- import 'package:shared/utils/screen_util.dart';
- /*
- * 获取并保存当前应用的信息。
- * 获取并保存当前设备的信息。
- */
- class ConfigService extends GetxService {
- static ConfigService get to => Get.find();
- // 设备信息
- /// android 设备信息
- AndroidDeviceInfo? androidDeviceInfo;
- /// ios 设备信息
- IosDeviceInfo? iosDeviceInfo;
- // 当前的languageCode
- Locale locale = PlatformDispatcher.instance.locale;
- PackageInfo? _appPackageInfo;
- String get version => _appPackageInfo?.version ?? '-';
- String get appName => _appPackageInfo?.appName ?? '-';
- String get packageName => _appPackageInfo?.packageName ?? '-';
- String get buildNumber => _appPackageInfo?.buildNumber ?? '-';
- final RxBool _isDarkModel = Get.isDarkMode.obs;
- // 是否是暗黑模式
- bool get isDarkModel => _isDarkModel.value;
- // 顶部安全距离(pt)
- double get safeAreaPaddingTop => _safeAreaPaddingTop!;
- // 底部安全距离(pt)
- double get safeAreaPaddingBottom => _safeAreaPaddingBottom!;
- double? _safeAreaPaddingTop;
- double? _safeAreaPaddingBottom;
- //屏幕大小
- Size? mSize;
- //密度
- double? mRatio;
- //设备像素px
- double? width;
- double? height;
- //设备宽高比,大于1.9是全面屏手机
- double whRatio = 1;
- //是否是全面屏设备(宽高比大于1.9)
- bool isFullScreenDevice = false;
- // 上下边距 (主要用于 刘海 和 内置导航键)
- double? topPadding;
- double? bottomPadding;
- double? textScaleFactor;
- Brightness? platformBrightness;
- EdgeInsets? viewInsets;
- EdgeInsets? padding;
- bool? alwaysUse24HourFormat;
- bool? accessibleNavigation;
- bool? invertColors;
- bool? disableAnimations;
- bool? boldText;
- Orientation? orientation;
- @override
- void onInit() {
- super.onInit();
- }
- @override
- void onReady() {
- super.onReady();
- //打印应用与设备的信息
- getDeviceInfo();
- getAppInfos();
- //打印当前设备的一些Media信息
- getMediaInfo(context: Get.context!);
- paddingSizeTop(context: Get.context!);
- paddingSizeBottom(context: Get.context!);
- }
- // ServicesInjection 已经获取到了信息,直接赋值过来并打印当前的应用信息
- Future<void> getAppInfos() async {
- // package_info 插件获取到当前运行平台上App的包信息
- _appPackageInfo = ConfigServicesInjection.packageInfo;
- Log.d('package_info 插件获获取到的app【appName】$appName');
- Log.d('package_info 插件获获取到的app【packageName】$packageName');
- Log.d('package_info 插件获获取到的app【version】$version');
- Log.d('package_info 插件获获取到的app【buildNumber】$buildNumber');
- }
- // ServicesInjection 已经获取到了信息,直接赋值过来并打印当前的设备信息
- void getDeviceInfo() {
- if (DeviceUtils.isIOS) {
- iosDeviceInfo = ConfigServicesInjection.iosDeviceInfo;
- Log.d('device_info_plus插件获取到设备信息【iosDeviceInfo】$iosDeviceInfo');
- } else if (DeviceUtils.isAndroid) {
- androidDeviceInfo = ConfigServicesInjection.androidDeviceInfo;
- Log.d('device_info_plus插件获取到设备信息【androidDeviceInfo】$androidDeviceInfo');
- }
- }
- // 获取当前的languageCode
- void initLocale() {}
- // 更换当前的languageCode
- void onLocaleUpdate(Locale value) {
- locale = value;
- Get.updateLocale(value);
- Log.d('glabalService-app_config_service.dart-更换后的locale【locale】$locale');
- }
- // 切换主题模式 dark(暗黑) / light(明亮)
- Future<void> switchThemeModel() async {
- _isDarkModel.value = !_isDarkModel.value;
- // Get.changeTheme(
- // _isDarkModel.value == true ? AppTheme.dark : AppTheme.light,
- // );
- await Future.delayed(const Duration(seconds: 1));
- }
- // 获取media相关信息
- void getMediaInfo({BuildContext? context}) {
- //屏幕大小
- mSize = MediaQuery.of(context!).size;
- //密度
- mRatio = MediaQuery.of(context).devicePixelRatio;
- //设备像素 px
- width = mSize!.width * mRatio!;
- height = mSize!.height * mRatio!;
- whRatio = (height ?? 1) / (width ?? 1);
- isFullScreenDevice = whRatio > 1.9;
- // 上下边距 (主要用于 刘海 和 内置导航键)
- topPadding = MediaQuery.of(context).padding.top;
- bottomPadding = MediaQuery.of(context).padding.bottom;
- textScaleFactor = MediaQuery.of(context).textScaleFactor;
- platformBrightness = MediaQuery.of(context).platformBrightness;
- viewInsets = MediaQuery.of(context).viewInsets;
- padding = MediaQuery.of(context).padding;
- alwaysUse24HourFormat = MediaQuery.of(context).alwaysUse24HourFormat;
- accessibleNavigation = MediaQuery.of(context).accessibleNavigation;
- invertColors = MediaQuery.of(context).invertColors;
- disableAnimations = MediaQuery.of(context).disableAnimations;
- boldText = MediaQuery.of(context).boldText;
- orientation = MediaQuery.of(context).orientation;
- final screenDensity = MediaQuery.devicePixelRatioOf(context);
- //初始化Flustarts工具类中 ScreenUtils 工具,可以设置设计稿的大小,可以选择性的使用 getAdapterSizeCtx 适配大小屏不同尺寸的展示
- setDesignWHD(375, 667, density: screenDensity);
- Log.d('service-app_config_service.dart---设备信息【MediaQuery.of(context)】:${MediaQuery.of(context)}');
- Log.d('service-app_config_service.dart---设备像素比dpr(物理像素/逻辑像素)【mRatio】:$mRatio');
- Log.d('service-app_config_service.dart--- ScreenUtil像素比: $screenDensity');
- Log.d('service-app_config_service.dart---逻辑像素pt【mSize.width * mSize.height】:${mSize!.width} x ${mSize!.height}');
- Log.d('service-app_config_service.dart---屏幕分辨率px(物理像素)【mSize.width * mRatio】 X 【mSize.height * mRatio】:$width x $height '
- '屏幕的宽高比:$whRatio');
- Log.d('service-app_config_service.dart---上边刘海(状态栏)【MediaQuery.of(context).padding.top】:$topPadding');
- Log.d('service-app_config_service.dart---下边导航【MediaQuery.of(context).padding.bottom】:$bottomPadding');
- Log.d('service-app_config_service.dart---textScaleFactor【MediaQuery.of(context).textScaleFactor】: $textScaleFactor');
- Log.d('service-app_config_service.dart---platformBrightness【MediaQuery.of(context).platformBrightness】: $platformBrightness');
- Log.d('service-app_config_service.dart---viewInsets【MediaQuery.of(context).viewInsets】: $viewInsets');
- Log.d('service-app_config_service.dart---padding【MediaQuery.of(context).padding】: $padding');
- Log.d('service-app_config_service.dart---alwaysUse24HourFormat: $alwaysUse24HourFormat');
- Log.d('service-app_config_service.dart---accessibleNavigation: $accessibleNavigation');
- Log.d('service-app_config_service.dart---invertColors: $invertColors');
- Log.d('service-app_config_service.dart---disableAnimations: $disableAnimations');
- Log.d('service-app_config_service.dart---boldText: $boldText');
- Log.d('service-app_config_service.dart---orientation: $orientation');
- }
- double paddingSizeBottom({required BuildContext context}) {
- final MediaQueryData data = MediaQuery.of(context);
- EdgeInsets padding = data.padding;
- padding = padding.copyWith(bottom: data.viewPadding.bottom);
- Log.d("glabalService-config.dart该设备底部的安全距离为----${padding.bottom}");
- _safeAreaPaddingBottom = padding.bottom;
- return padding.bottom;
- }
- double paddingSizeTop({required BuildContext context}) {
- final MediaQueryData data = MediaQuery.of(context);
- EdgeInsets padding = data.padding;
- padding = padding.copyWith(top: data.viewPadding.top);
- Log.d("glabalService-config.dart该设备顶部的安全距离为----${padding.top}");
- _safeAreaPaddingTop = padding.top;
- return padding.top;
- }
- int compareVersionNumbers(String version1, String version2) {
- List<int> v1 = version1.split('.').map(int.parse).toList();
- List<int> v2 = version2.split('.').map(int.parse).toList();
- int minLength = v1.length < v2.length ? v1.length : v2.length;
- for (int i = 0; i < minLength; i++) {
- if (v1[i] < v2[i]) {
- return -1; // version1 < version2
- } else if (v1[i] > v2[i]) {
- return 1;
- }
- }
- if (v1.length < v2.length) {
- return -1;
- } else if (v1.length > v2.length) {
- return 1; // version1 > version2
- }
- return 0; //version1 = version2
- }
- }
|