深色模式、首页设置页面和功能优化
This commit is contained in:
135
lib/models/night-mode/theme_model.dart
Normal file
135
lib/models/night-mode/theme_model.dart
Normal file
@@ -0,0 +1,135 @@
|
||||
// 时间: 2026-04-02
|
||||
// 功能: 深色模式主题模型
|
||||
// 介绍: 定义主题模式枚举和相关数据结构
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// 应用主题模式枚举
|
||||
enum AppThemeMode {
|
||||
/// 跟随系统
|
||||
system,
|
||||
/// 浅色模式
|
||||
light,
|
||||
/// 深色模式
|
||||
dark,
|
||||
}
|
||||
|
||||
/// 主题模式扩展方法
|
||||
extension AppThemeModeExtension on AppThemeMode {
|
||||
/// 获取主题模式对应的 ThemeMode
|
||||
ThemeMode get themeMode {
|
||||
switch (this) {
|
||||
case AppThemeMode.system:
|
||||
return ThemeMode.system;
|
||||
case AppThemeMode.light:
|
||||
return ThemeMode.light;
|
||||
case AppThemeMode.dark:
|
||||
return ThemeMode.dark;
|
||||
}
|
||||
}
|
||||
|
||||
/// 获取主题模式显示名称
|
||||
String get displayName {
|
||||
switch (this) {
|
||||
case AppThemeMode.system:
|
||||
return '跟随系统';
|
||||
case AppThemeMode.light:
|
||||
return '浅色模式';
|
||||
case AppThemeMode.dark:
|
||||
return '深色模式';
|
||||
}
|
||||
}
|
||||
|
||||
/// 获取主题模式图标
|
||||
IconData get icon {
|
||||
switch (this) {
|
||||
case AppThemeMode.system:
|
||||
return Icons.brightness_auto;
|
||||
case AppThemeMode.light:
|
||||
return Icons.brightness_high;
|
||||
case AppThemeMode.dark:
|
||||
return Icons.brightness_2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 主题配置数据类
|
||||
class ThemeConfig {
|
||||
/// 是否启用深色模式(简化开关,true=深色, false=跟随系统或浅色)
|
||||
final bool isDarkMode;
|
||||
|
||||
/// 主题模式
|
||||
final AppThemeMode themeMode;
|
||||
|
||||
/// 主题色索引
|
||||
final int themeColorIndex;
|
||||
|
||||
/// 强调色索引
|
||||
final int accentColorIndex;
|
||||
|
||||
/// 字体大小索引 (0: 小, 1: 中, 2: 大)
|
||||
final int fontSizeIndex;
|
||||
|
||||
/// 是否启用动画
|
||||
final bool enableAnimation;
|
||||
|
||||
/// 是否启用模糊效果
|
||||
final bool enableBlurEffect;
|
||||
|
||||
const ThemeConfig({
|
||||
this.isDarkMode = false,
|
||||
this.themeMode = AppThemeMode.system,
|
||||
this.themeColorIndex = 0,
|
||||
this.accentColorIndex = 0,
|
||||
this.fontSizeIndex = 1,
|
||||
this.enableAnimation = true,
|
||||
this.enableBlurEffect = true,
|
||||
});
|
||||
|
||||
/// 从 JSON 创建
|
||||
factory ThemeConfig.fromJson(Map<String, dynamic> json) {
|
||||
return ThemeConfig(
|
||||
isDarkMode: json['isDarkMode'] ?? false,
|
||||
themeMode: AppThemeMode.values[json['themeMode'] ?? 0],
|
||||
themeColorIndex: json['themeColorIndex'] ?? 0,
|
||||
accentColorIndex: json['accentColorIndex'] ?? 0,
|
||||
fontSizeIndex: json['fontSizeIndex'] ?? 1,
|
||||
enableAnimation: json['enableAnimation'] ?? true,
|
||||
enableBlurEffect: json['enableBlurEffect'] ?? true,
|
||||
);
|
||||
}
|
||||
|
||||
/// 转换为 JSON
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'isDarkMode': isDarkMode,
|
||||
'themeMode': themeMode.index,
|
||||
'themeColorIndex': themeColorIndex,
|
||||
'accentColorIndex': accentColorIndex,
|
||||
'fontSizeIndex': fontSizeIndex,
|
||||
'enableAnimation': enableAnimation,
|
||||
'enableBlurEffect': enableBlurEffect,
|
||||
};
|
||||
}
|
||||
|
||||
/// 复制并修改
|
||||
ThemeConfig copyWith({
|
||||
bool? isDarkMode,
|
||||
AppThemeMode? themeMode,
|
||||
int? themeColorIndex,
|
||||
int? accentColorIndex,
|
||||
int? fontSizeIndex,
|
||||
bool? enableAnimation,
|
||||
bool? enableBlurEffect,
|
||||
}) {
|
||||
return ThemeConfig(
|
||||
isDarkMode: isDarkMode ?? this.isDarkMode,
|
||||
themeMode: themeMode ?? this.themeMode,
|
||||
themeColorIndex: themeColorIndex ?? this.themeColorIndex,
|
||||
accentColorIndex: accentColorIndex ?? this.accentColorIndex,
|
||||
fontSizeIndex: fontSizeIndex ?? this.fontSizeIndex,
|
||||
enableAnimation: enableAnimation ?? this.enableAnimation,
|
||||
enableBlurEffect: enableBlurEffect ?? this.enableBlurEffect,
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user