news_getx/lib/data/services/config.dart

91 lines
2.2 KiB
Dart

import 'dart:io';
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';
import 'package:device_info/device_info.dart';
class ConfigService extends GetxService {
static ConfigService get to => Get.find();
bool isFirstOpen = false;
RxBool isGrayFilter = false.obs;
PackageInfo? _platform;
/// 发布渠道
static String channel = "xxx";
/// 是否 ios
static bool isIOS = Platform.isIOS;
/// android 设备信息
static AndroidDeviceInfo? androidDeviceInfo;
/// ios 设备信息
static IosDeviceInfo? iosDeviceInfo;
String get version => _platform?.version ?? "-";
bool get isRelease => bool.fromEnvironment("dart.vm.product");
// Locale locale = Locale("en", "US");
Locale locale = Locale("zh", "CN");
List<Locale> languages = [
Locale('en', 'US'),
Locale('zh', 'CN'),
];
@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);
}
}