Jisho-Study-Tool/lib/bloc/theme/theme_bloc.dart

32 lines
848 B
Dart
Raw Normal View History

2021-12-01 23:09:53 +01:00
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
2021-12-01 23:09:53 +01:00
import 'package:flutter_bloc/flutter_bloc.dart';
2021-08-08 23:16:54 +02:00
2021-12-01 23:09:53 +01:00
import '../../models/themes/theme.dart';
import '../../settings.dart';
2021-12-01 23:09:53 +01:00
2021-08-08 23:16:54 +02:00
export 'package:flutter_bloc/flutter_bloc.dart';
export 'package:jisho_study_tool/models/themes/theme.dart';
part 'theme_event.dart';
part 'theme_state.dart';
class ThemeBloc extends Bloc<ThemeEvent, ThemeState> {
2021-12-01 23:09:53 +01:00
ThemeBloc() : super(const LightThemeState()) {
2021-12-06 20:26:52 +01:00
on<SetTheme>(
(event, emit) => emit(
event.themeIsDark ? const DarkThemeState() : const LightThemeState(),
),
);
2021-08-08 23:16:54 +02:00
final bool autoThemeIsDark =
SchedulerBinding.instance!.window.platformBrightness == Brightness.dark;
2021-12-06 20:26:52 +01:00
add(
SetTheme(
themeIsDark: autoThemeEnabled ? autoThemeIsDark : darkThemeEnabled,
2021-12-06 20:26:52 +01:00
),
);
2021-08-08 23:16:54 +02:00
}
}