96 lines
2.3 KiB
Dart
96 lines
2.3 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:device_info/device_info.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:news_getx/config/storage.dart';
|
|
import 'package:news_getx/data/services/storage.dart';
|
|
import 'package:package_info_plus/package_info_plus.dart';
|
|
|
|
class ConfigService extends GetxService {
|
|
/// 静态变量
|
|
static ConfigService get to => Get.find();
|
|
|
|
// 发布渠道
|
|
static String channel = "xxx";
|
|
|
|
// 是否 ios
|
|
static bool isIOS = Platform.isIOS;
|
|
|
|
// android 设备信息
|
|
static AndroidDeviceInfo? androidDeviceInfo;
|
|
|
|
// ios 设备信息
|
|
static IosDeviceInfo? iosDeviceInfo;
|
|
|
|
/// 响应式成员变量
|
|
RxBool isGrayFilter = false.obs;
|
|
|
|
/// 成员变量
|
|
bool isFirstOpen = false;
|
|
PackageInfo? _platform;
|
|
|
|
Locale locale = Locale("zh", "CN");
|
|
|
|
List<Locale> languages = [
|
|
Locale('en', 'US'),
|
|
Locale('zh', 'CN'),
|
|
];
|
|
|
|
/// Getter
|
|
String get version => _platform?.version ?? "-";
|
|
|
|
bool get isRelease => bool.fromEnvironment("dart.vm.product");
|
|
|
|
@override
|
|
void onInit() async {
|
|
super.onInit();
|
|
isFirstOpen = StorageService.to.getBool(StorageDeviceFirstOpenKey);
|
|
|
|
// 加载设备信息
|
|
getPlatform();
|
|
|
|
// 读取设备信息
|
|
DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();
|
|
if (isIOS) {
|
|
iosDeviceInfo = await deviceInfoPlugin.iosInfo;
|
|
} else {
|
|
androidDeviceInfo = await deviceInfoPlugin.androidInfo;
|
|
}
|
|
}
|
|
|
|
void changeGrayTheme() {
|
|
isGrayFilter.value = !isGrayFilter.value;
|
|
}
|
|
|
|
Future<void> getPlatform() async {
|
|
// 获取包信息
|
|
_platform = await PackageInfo.fromPlatform();
|
|
}
|
|
|
|
// 标记用户已打开APP
|
|
Future<bool> saveAlreadyOpen() {
|
|
return StorageService.to.setBool(StorageDeviceFirstOpenKey, false);
|
|
}
|
|
|
|
// 初始化语言环境 暂未使用
|
|
void onInitLocale() {
|
|
String langCode = StorageService.to.getString(StorageLanguageCode);
|
|
if (langCode.isEmpty) return;
|
|
|
|
int index = languages.indexWhere((element) {
|
|
return element.languageCode == langCode;
|
|
});
|
|
if (index < 0) return;
|
|
locale = languages[index];
|
|
}
|
|
|
|
// 改变语言环境
|
|
void onLocaleUpdate(Locale value) {
|
|
locale = value;
|
|
// 更新app的语言
|
|
Get.updateLocale(value);
|
|
StorageService.to.setString(StorageLanguageCode, value.languageCode);
|
|
}
|
|
}
|