fnotes/lib/theme_bloc.dart

41 lines
1.0 KiB
Dart
Raw Normal View History

2024-03-17 13:15:50 +00:00
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
class ThemeBloc extends Bloc<ThemeEvent, ThemeState> {
ThemeBloc() : super(ThemeState.init()) {
on<ThemeEvent>(switchTheme);
}
void switchTheme(ThemeEvent event, Emitter<ThemeState> emit) {
if (state.theme.brightness == Brightness.light) {
emit(ThemeState.withBrightness(Brightness.dark));
} else {
emit(ThemeState.withBrightness(Brightness.light));
}
}
}
class ThemeEvent {}
class ThemeState {
final ThemeData theme;
static final ThemeData initTheme = ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.lightBlue, brightness: Brightness.light),
);
ThemeState({required this.theme});
factory ThemeState.init() {
return ThemeState(theme: initTheme);
}
factory ThemeState.withBrightness(Brightness brightness) {
return ThemeState(
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(
seedColor: Colors.lightBlue, brightness: brightness),
));
}
}