import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'theme_event.dart'; import 'theme_state.dart'; class ThemeBloc extends Bloc { ThemeBloc() : super(const ThemeState()) { on(_onThemeChanged); on(_onThemeLoadRequested); } Future _onThemeChanged( ThemeChanged event, Emitter emit, ) async { emit(state.copyWith(themeMode: event.themeMode)); // Sauvegarder la préférence final prefs = await SharedPreferences.getInstance(); await prefs.setString('themeMode', event.themeMode.toString()); } Future _onThemeLoadRequested( ThemeLoadRequested event, Emitter emit, ) async { final prefs = await SharedPreferences.getInstance(); final themeModeString = prefs.getString('themeMode'); if (themeModeString != null) { ThemeMode themeMode; switch (themeModeString) { case 'ThemeMode.light': themeMode = ThemeMode.light; break; case 'ThemeMode.dark': themeMode = ThemeMode.dark; break; default: themeMode = ThemeMode.system; } emit(state.copyWith(themeMode: themeMode)); } } }