import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; class ThemeBloc extends Bloc { ThemeBloc() : super(ThemeState.init()) { on(switchTheme); } void switchTheme(ThemeEvent event, Emitter 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), )); } }