Add automatically determine theme feature

This commit is contained in:
Oystein Kristoffer Tveit 2022-01-19 16:06:03 +01:00
parent fd384a2ac9
commit 71fccb237f
3 changed files with 51 additions and 12 deletions

View File

@ -1,9 +1,9 @@
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:get_it/get_it.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../../models/themes/theme.dart';
import '../../settings.dart';
export 'package:flutter_bloc/flutter_bloc.dart';
export 'package:jisho_study_tool/models/themes/theme.dart';
@ -19,12 +19,12 @@ class ThemeBloc extends Bloc<ThemeEvent, ThemeState> {
),
);
final bool autoThemeIsDark =
SchedulerBinding.instance!.window.platformBrightness == Brightness.dark;
add(
SetTheme(
themeIsDark: GetIt.instance
.get<SharedPreferences>()
.getBool('darkThemeEnabled') ??
false,
themeIsDark: autoThemeEnabled ? autoThemeIsDark : darkThemeEnabled,
),
);
}

View File

@ -10,6 +10,7 @@ import 'package:shared_preferences/shared_preferences.dart';
import 'bloc/theme/theme_bloc.dart';
import 'routing/router.dart';
import 'settings.dart';
Future<void> setupDatabase() async {
final Directory appDocDir = await getApplicationDocumentsDirectory();
@ -35,14 +36,43 @@ Future<void> main() async {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
final ThemeBloc themeBloc = ThemeBloc();
@override
void initState() {
super.initState();
WidgetsBinding.instance?.addObserver(this);
}
@override
void dispose() {
WidgetsBinding.instance?.removeObserver(this);
super.dispose();
}
@override
void didChangePlatformBrightness() {
if (autoThemeEnabled) {
final themeIsDark =
WidgetsBinding.instance?.window.platformBrightness == Brightness.dark;
themeBloc.add(SetTheme(themeIsDark: themeIsDark));
}
super.didChangePlatformBrightness();
}
@override
Widget build(BuildContext context) {
return MultiBlocProvider(
providers: [
BlocProvider(create: (context) => ThemeBloc()),
BlocProvider(create: (context) => themeBloc),
],
child: BlocBuilder<ThemeBloc, ThemeState>(
builder: (context, themeState) => MaterialApp(

View File

@ -24,6 +24,18 @@ class _SettingsViewState extends State<SettingsView> {
}
}
// ignore: avoid_positional_boolean_parameters
void toggleAutoTheme(bool b) {
final bool newThemeIsDark = b
? WidgetsBinding.instance!.window.platformBrightness == Brightness.dark
: darkThemeEnabled;
BlocProvider.of<ThemeBloc>(context)
.add(SetTheme(themeIsDark: newThemeIsDark));
setState(() => autoThemeEnabled = b);
}
@override
Widget build(BuildContext context) {
final TextStyle _titleTextStyle = TextStyle(
@ -56,11 +68,8 @@ class _SettingsViewState extends State<SettingsView> {
tiles: <SettingsTile>[
SettingsTile.switchTile(
title: 'Automatically determine theme',
onToggle: (b) {
setState(() => autoThemeEnabled = b);
},
onToggle: toggleAutoTheme,
switchValue: autoThemeEnabled,
enabled: false,
switchActiveColor: AppTheme.jishoGreen.background,
),
SettingsTile.switchTile(