import 'package:flutter/material.dart'; import 'package:riverpod_annotation/riverpod_annotation.dart'; import 'package:shared_preferences/shared_preferences.dart'; part 'theme_provider.g.dart'; /// Key for storing theme preference in SharedPreferences const String _themeModeKey = 'theme_mode'; /// Provider for theme mode state management @riverpod class ThemeNotifier extends _$ThemeNotifier { @override ThemeMode build() { _loadThemeMode(); return ThemeMode.system; // Default value until loaded } /// Load theme mode from SharedPreferences Future _loadThemeMode() async { final prefs = await SharedPreferences.getInstance(); final themeModeString = prefs.getString(_themeModeKey); if (themeModeString != null) { final themeMode = ThemeMode.values.firstWhere( (mode) => mode.name == themeModeString, orElse: () => ThemeMode.system, ); state = themeMode; } } /// Set theme mode and persist to SharedPreferences Future setThemeMode(ThemeMode mode) async { state = mode; final prefs = await SharedPreferences.getInstance(); await prefs.setString(_themeModeKey, mode.name); } }