Files
nj/app/lib/features/profile/bloc/settings_bloc.dart
iven 860e9e5d22 feat(app): BLoC 集成 Repository + SettingsBloc 主题切换
全局依赖注入:
- app.dart 注入 JournalRepository + ClassRepository + SettingsBloc
- ApiClient token 自动注入(监听 AuthBloc 状态)

BLoC 重构 (占位数据 → Repository):
- CalendarBloc: 通过 JournalRepository 加载月度日记
- ClassBloc: 通过 ClassRepository + JournalRepository 加载班级数据
- 新增 ClassJoin 事件支持班级码加入
- HomeBloc: 加载最近日记 + 心情概览 + 连续天数 + 今日是否已写

设置系统:
- SettingsBloc: ThemeMode 切换 (system/light/dark)
- app.dart 通过 ListenableBuilder 响应主题变化
- HomeBloc 支持下拉刷新

首页增强:
- 连续天数徽章 + 今日已写标记 + 最常用心情高亮
- RefreshIndicator 下拉刷新
- 日记列表卡片显示日期

验证: flutter analyze 0 error
2026-06-01 10:32:20 +08:00

61 lines
1.4 KiB
Dart

// 设置 BLoC — 主题切换 + 应用设置管理
import 'package:flutter/material.dart';
// ===== Events =====
sealed class SettingsEvent {
const SettingsEvent();
}
final class SettingsThemeChanged extends SettingsEvent {
final ThemeMode themeMode;
const SettingsThemeChanged(this.themeMode);
}
final class SettingsLoad extends SettingsEvent {
const SettingsLoad();
}
// ===== State =====
class SettingsState {
final ThemeMode themeMode;
final bool isLoading;
const SettingsState({
this.themeMode = ThemeMode.system,
this.isLoading = false,
});
SettingsState copyWith({ThemeMode? themeMode, bool? isLoading}) =>
SettingsState(
themeMode: themeMode ?? this.themeMode,
isLoading: isLoading ?? this.isLoading,
);
}
// ===== BLoC =====
class SettingsBloc extends ChangeNotifier {
SettingsState _state = const SettingsState();
SettingsState get state => _state;
/// 切换主题模式
void changeTheme(ThemeMode mode) {
_state = _state.copyWith(themeMode: mode);
notifyListeners();
// TODO: 持久化到 SharedPreferences/Isar
}
/// 循环切换: system → light → dark → system
void cycleTheme() {
final next = switch (_state.themeMode) {
ThemeMode.system => ThemeMode.light,
ThemeMode.light => ThemeMode.dark,
ThemeMode.dark => ThemeMode.system,
};
changeTheme(next);
}
}