calorimeter/lib/theme_bloc.dart
Marco b83f547f6b Implement food entry lookup on entering a food name.
Now, an on-the-fly food lookup is created from existing entries on startup. Those entries are used to make suggestions when the user is typing to enter new food entries.
2024-09-04 22:47:37 +02:00

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});
}