feat: 保存配置
This commit is contained in:
@@ -15,11 +15,31 @@ class KRThemeService extends GetxService {
|
||||
final String _key = 'themeOption'; // 存储主题选项的键
|
||||
late ThemeMode _currentThemeOption = ThemeMode.light; // 当前主题选项
|
||||
|
||||
// 🔧 P0修复: 添加初始化状态标记,防止未初始化就使用
|
||||
bool _isInitialized = false;
|
||||
|
||||
/// 初始化时从存储中加载主题设置
|
||||
Future<void> init() async {
|
||||
_currentThemeOption = await kr_loadThemeOptionFromStorage();
|
||||
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) {
|
||||
@@ -82,7 +102,7 @@ class KRThemeService extends GetxService {
|
||||
),
|
||||
elevatedButtonTheme: ElevatedButtonThemeData(
|
||||
style:
|
||||
ElevatedButton.styleFrom(backgroundColor: Colors.green), // 自定义的按钮颜色
|
||||
ElevatedButton.styleFrom(backgroundColor: Colors.green), // 自定义的按钮颜色
|
||||
),
|
||||
switchTheme: SwitchThemeData(
|
||||
thumbColor: WidgetStateProperty.all(Colors.white), // 开关按钮颜色
|
||||
@@ -100,10 +120,10 @@ class KRThemeService extends GetxService {
|
||||
// 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);
|
||||
// }
|
||||
@@ -142,18 +162,6 @@ class KRThemeService extends GetxService {
|
||||
return Colors.transparent; // 打开时不显示边框
|
||||
}),
|
||||
),
|
||||
snackBarTheme: SnackBarThemeData(
|
||||
backgroundColor: Colors.black, // 黑色背景
|
||||
contentTextStyle: TextStyle(
|
||||
color: Colors.white, // 白色文字
|
||||
fontSize: 14,
|
||||
),
|
||||
actionTextColor: Colors.white, // 操作按钮文字颜色
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(8), // 圆角
|
||||
),
|
||||
behavior: SnackBarBehavior.floating, // 浮动样式
|
||||
),
|
||||
// 其他自定义颜色
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user