calorimeter/lib/utils/theme_bloc.dart

38 lines
889 B
Dart
Raw Permalink Normal View History

2024-09-06 17:00:25 +00:00
import 'package:calorimeter/storage/storage.dart';
2024-06-12 12:42:29 +00:00
import 'package:flutter_bloc/flutter_bloc.dart';
class ThemeDataBloc extends Bloc<ThemeToggleEvent, ThemeState> {
final FoodStorage storage;
2024-06-12 12:42:29 +00:00
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});
}