feat(flutter/theme): themeModeProvider with secure-storage persistence
AsyncNotifier wrapping flutter_secure_storage's "theme_mode" key. Returns AppThemeMode.system as the default. set(mode) writes the value + updates state. Used by MinstrelApp (next commit) to drive MaterialApp.themeMode and by the Settings appearance section to persist the operator's pick. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
import '../auth/auth_provider.dart';
|
||||
|
||||
const _kThemeModeKey = 'theme_mode';
|
||||
|
||||
enum AppThemeMode { system, dark, light }
|
||||
|
||||
extension AppThemeModeMaterial on AppThemeMode {
|
||||
ThemeMode get materialMode => switch (this) {
|
||||
AppThemeMode.system => ThemeMode.system,
|
||||
AppThemeMode.dark => ThemeMode.dark,
|
||||
AppThemeMode.light => ThemeMode.light,
|
||||
};
|
||||
}
|
||||
|
||||
class ThemeModeController extends AsyncNotifier<AppThemeMode> {
|
||||
@override
|
||||
Future<AppThemeMode> build() async {
|
||||
final storage = ref.watch(secureStorageProvider);
|
||||
final raw = await storage.read(key: _kThemeModeKey);
|
||||
return switch (raw) {
|
||||
'dark' => AppThemeMode.dark,
|
||||
'light' => AppThemeMode.light,
|
||||
_ => AppThemeMode.system,
|
||||
};
|
||||
}
|
||||
|
||||
Future<void> set(AppThemeMode mode) async {
|
||||
final storage = ref.read(secureStorageProvider);
|
||||
await storage.write(key: _kThemeModeKey, value: mode.name);
|
||||
state = AsyncData(mode);
|
||||
}
|
||||
}
|
||||
|
||||
final themeModeProvider =
|
||||
AsyncNotifierProvider<ThemeModeController, AppThemeMode>(
|
||||
ThemeModeController.new,
|
||||
);
|
||||
Reference in New Issue
Block a user