update theme selection

This commit is contained in:
Phuoc Nguyen
2025-12-01 11:31:26 +07:00
parent 4ecb236532
commit 250c453413
18 changed files with 1351 additions and 304 deletions

View File

@@ -0,0 +1,88 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:worker/core/database/app_settings_box.dart';
import 'package:worker/core/theme/colors.dart';
part 'theme_provider.g.dart';
/// Theme settings state
class ThemeSettings {
const ThemeSettings({
required this.seedColorId,
required this.themeMode,
});
final String seedColorId;
final ThemeMode themeMode;
/// Get the actual Color from the seed color ID
Color get seedColor => AppColors.getSeedColorById(seedColorId);
/// Get the SeedColorOption from the ID
SeedColorOption get seedColorOption => AppColors.seedColorOptions.firstWhere(
(option) => option.id == seedColorId,
orElse: () => AppColors.seedColorOptions.first,
);
ThemeSettings copyWith({
String? seedColorId,
ThemeMode? themeMode,
}) {
return ThemeSettings(
seedColorId: seedColorId ?? this.seedColorId,
themeMode: themeMode ?? this.themeMode,
);
}
}
/// Provider for managing theme settings with Hive persistence
/// Uses AppSettingsBox for storage
@Riverpod(keepAlive: true)
class ThemeSettingsNotifier extends _$ThemeSettingsNotifier {
@override
ThemeSettings build() {
return _loadFromSettings();
}
ThemeSettings _loadFromSettings() {
return ThemeSettings(
seedColorId: AppSettingsBox.getSeedColorId(),
themeMode: ThemeMode.values[AppSettingsBox.getThemeModeIndex()],
);
}
/// Update seed color
Future<void> setSeedColor(String colorId) async {
await AppSettingsBox.setSeedColorId(colorId);
state = state.copyWith(seedColorId: colorId);
}
/// Update theme mode (light/dark/system)
Future<void> setThemeMode(ThemeMode mode) async {
await AppSettingsBox.setThemeModeIndex(mode.index);
state = state.copyWith(themeMode: mode);
}
/// Toggle between light and dark mode
Future<void> toggleThemeMode() async {
final newMode =
state.themeMode == ThemeMode.light ? ThemeMode.dark : ThemeMode.light;
await setThemeMode(newMode);
}
}
/// Provider for the current seed color (convenience provider)
@riverpod
Color currentSeedColor(Ref ref) {
return ref.watch(
themeSettingsProvider.select((settings) => settings.seedColor),
);
}
/// Provider for available seed color options
@riverpod
List<SeedColorOption> seedColorOptions(Ref ref) {
return AppColors.seedColorOptions;
}