38 lines
887 B
Dart
38 lines
887 B
Dart
import 'package:calodiary/storage/storage.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
|
|
class ThemeDataBloc extends Bloc<ThemeToggleEvent, ThemeState> {
|
|
final FoodStorage storage;
|
|
|
|
ThemeDataBloc(super.initialState, {required this.storage}) {
|
|
on<ThemeToggleEvent>(switchTheme);
|
|
}
|
|
|
|
void switchTheme(ThemeToggleEvent event, Emitter<ThemeState> emit) async {
|
|
String newBrightness = 'light';
|
|
if (state.brightness == 'light') newBrightness = 'dark';
|
|
|
|
persistTheme(newBrightness);
|
|
|
|
emit(ThemeState(brightness: newBrightness));
|
|
}
|
|
|
|
void persistTheme(String brightness) async {
|
|
storage.writeBrightness(brightness);
|
|
}
|
|
|
|
Future<String> getPersistedTheme() async {
|
|
return storage.readBrightness();
|
|
}
|
|
}
|
|
|
|
class ThemeToggleEvent {
|
|
ThemeToggleEvent();
|
|
}
|
|
|
|
class ThemeState {
|
|
final String brightness;
|
|
|
|
ThemeState({required this.brightness});
|
|
}
|