hi-client/lib/app/themes/kr_theme_service.dart
2026-01-07 17:33:55 -08:00

169 lines
6.1 KiB
Dart
Executable File

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import '../utils/kr_secure_storage.dart'; // 确保导入路径正确
class KRThemeService extends GetxService {
// 单例模式实现
static final KRThemeService _instance = KRThemeService._internal();
factory KRThemeService() => _instance;
KRThemeService._internal();
final KRSecureStorage _storage = KRSecureStorage(); // 创建安全存储实例
final String _key = 'themeOption'; // 存储主题选项的键
late ThemeMode _currentThemeOption = ThemeMode.light; // 当前主题选项
// 🔧 P0修复: 添加初始化状态标记,防止未初始化就使用
bool _isInitialized = false;
/// 初始化时从存储中加载主题设置
Future<void> init() async {
try {
_currentThemeOption = await kr_loadThemeOptionFromStorage();
_isInitialized = true;
} catch (e) {
// 初始化失败时使用默认主题
_currentThemeOption = ThemeMode.light;
_isInitialized = true;
print('⚠️ 主题初始化失败,使用默认主题: $e');
}
}
/// 🔧 P0修复: 重置主题服务状态(用于热重载/应用恢复)
Future<void> reset() async {
_isInitialized = false;
await init();
}
/// 🔧 P0修复: 检查是否已初始化
bool get isInitialized => _isInitialized;
/// 获取当前主题模式
ThemeMode get kr_Theme {
switch (_currentThemeOption) {
case ThemeMode.light:
return ThemeMode.light;
case ThemeMode.dark:
return ThemeMode.dark;
case ThemeMode.system:
default:
return ThemeMode.system;
}
}
/// 从安全存储中加载主题选项
/// 返回 KRThemeOption 枚举值
Future<ThemeMode> kr_loadThemeOptionFromStorage() async {
String? themeOption = await _storage.kr_readData(key: _key);
switch (themeOption) {
case 'light':
return ThemeMode.light;
case 'dark':
return ThemeMode.dark;
case 'system':
return ThemeMode.system;
default:
return ThemeMode.light;
}
}
/// 将主题选项保存到安全存储中
/// 参数 [option] 为 KRThemeOption 枚举值
Future<void> kr_saveThemeOptionToStorage(ThemeMode option) async {
await _storage.kr_saveData(
key: _key, value: option.toString().split('.').last);
}
/// 切换主题模式
/// 循环切换亮色、暗色、跟随系统
Future<void> kr_switchTheme(ThemeMode option) async {
_currentThemeOption = option;
Get.changeThemeMode(kr_Theme);
await kr_saveThemeOptionToStorage(option);
}
/// 定义亮模式的主题数据
ThemeData kr_lightTheme() {
return ThemeData.light().copyWith(
primaryColor: const Color(0xFFADFF5B),
scaffoldBackgroundColor: Colors.transparent,
cardColor: Colors.white, // 卡片背景色
appBarTheme: const AppBarTheme(
systemOverlayStyle: SystemUiOverlayStyle.light, // 使用深色状态栏样式
),
hintColor: const Color(0xFFBFBFBF),
textTheme: TextTheme(
bodyMedium: TextStyle(color: const Color(0xFF333333)), // 标题颜色
bodySmall: TextStyle(color: const Color(0xFF777777)), // 子标题颜色
labelMedium: TextStyle(color: const Color(0xFF333333)), // 标题颜色
labelSmall: TextStyle(color: const Color(0xFF777777)), // 子标题颜色
),
elevatedButtonTheme: ElevatedButtonThemeData(
style:
ElevatedButton.styleFrom(backgroundColor: Colors.green), // 自定义的按钮颜色
),
switchTheme: SwitchThemeData(
thumbColor: WidgetStateProperty.all(Colors.white), // 开关按钮颜色
trackColor: WidgetStateProperty.resolveWith<Color>((states) {
if (states.contains(WidgetState.selected)) {
return Color.fromRGBO(23, 151, 255, 1); // 开启状态轨道颜色
}
return Color.fromRGBO(202, 202, 202, 1); // 关闭状态轨道颜色
}),
),
// 其他自定义颜色
);
}
// class KRColors {
// // 1. 底部导航栏背景色(稍深的蓝黑色)
// static const Color kr_bottomNavBackground = Color(0xFF161920);
// // 2. 列表整体背景色(深蓝黑色)
// static const Color kr_listBackground = Color(0xFF1A1D24);
// // 3. 列表项背景色(略浅于列表背景的蓝黑色)
// static const Color kr_listItemBackground = Color(0xFF1E2128);
// }
/// 定义暗模式的主题数据
ThemeData kr_darkTheme() {
return ThemeData.dark().copyWith(
primaryColor: Color.fromRGBO(18,22,32,1),
scaffoldBackgroundColor: Color.fromRGBO(21,25,35,1),
cardColor: Color.fromRGBO(27,31,41, 1),
appBarTheme: const AppBarTheme(
systemOverlayStyle: SystemUiOverlayStyle.light, // 使用浅色状态栏样式
),
hintColor: const Color(0xFFBBBBBB),
textTheme: TextTheme(
bodyMedium: TextStyle(color: const Color(0xFFFFFFFF)), // 标题颜色
bodySmall: TextStyle(color: const Color(0xFFBBBBBB)), // 子标题
labelMedium: TextStyle(color: const Color(0xFFFFFFFF)), // 标题颜色
labelSmall: TextStyle(color: const Color(0xFFBBBBBB)), // 子标题颜色
),
elevatedButtonTheme: ElevatedButtonThemeData(
style: ElevatedButton.styleFrom(
backgroundColor: Colors.purple), // 自定义的按钮颜色
),
switchTheme: SwitchThemeData(
thumbColor: WidgetStateProperty.all(Colors.white), // 开关按钮颜色
trackColor: WidgetStateProperty.resolveWith<Color>((states) {
if (states.contains(WidgetState.selected)) {
return Color.fromRGBO(23, 151, 255, 1); // 开启状态轨道颜色
}
return Color.fromRGBO(202, 202, 202, 1); // 关闭状态轨道颜色
}),
trackOutlineColor: WidgetStateProperty.resolveWith<Color>((states) {
if (!states.contains(WidgetState.selected)) {
return Colors.transparent; // 关闭状态的边框颜色
}
return Colors.transparent; // 打开时不显示边框
}),
),
// 其他自定义颜色
);
}
}