Merge pull request #23 from h7x4ABk3g/add-automatically-determine-theme-feature
Add automatically determine theme feature
This commit is contained in:
commit
b4a6b52e89
|
@ -1,9 +1,9 @@
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter/scheduler.dart';
|
||||||
import 'package:flutter_bloc/flutter_bloc.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 '../../models/themes/theme.dart';
|
||||||
|
import '../../settings.dart';
|
||||||
|
|
||||||
export 'package:flutter_bloc/flutter_bloc.dart';
|
export 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
export 'package:jisho_study_tool/models/themes/theme.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(
|
add(
|
||||||
SetTheme(
|
SetTheme(
|
||||||
themeIsDark: GetIt.instance
|
themeIsDark: autoThemeEnabled ? autoThemeIsDark : darkThemeEnabled,
|
||||||
.get<SharedPreferences>()
|
|
||||||
.getBool('darkThemeEnabled') ??
|
|
||||||
false,
|
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,7 @@ import 'package:shared_preferences/shared_preferences.dart';
|
||||||
|
|
||||||
import 'bloc/theme/theme_bloc.dart';
|
import 'bloc/theme/theme_bloc.dart';
|
||||||
import 'routing/router.dart';
|
import 'routing/router.dart';
|
||||||
|
import 'settings.dart';
|
||||||
|
|
||||||
Future<void> setupDatabase() async {
|
Future<void> setupDatabase() async {
|
||||||
final Directory appDocDir = await getApplicationDocumentsDirectory();
|
final Directory appDocDir = await getApplicationDocumentsDirectory();
|
||||||
|
@ -35,14 +36,43 @@ Future<void> main() async {
|
||||||
runApp(const MyApp());
|
runApp(const MyApp());
|
||||||
}
|
}
|
||||||
|
|
||||||
class MyApp extends StatelessWidget {
|
class MyApp extends StatefulWidget {
|
||||||
const MyApp({Key? key}) : super(key: key);
|
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
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return MultiBlocProvider(
|
return MultiBlocProvider(
|
||||||
providers: [
|
providers: [
|
||||||
BlocProvider(create: (context) => ThemeBloc()),
|
BlocProvider(create: (context) => themeBloc),
|
||||||
],
|
],
|
||||||
child: BlocBuilder<ThemeBloc, ThemeState>(
|
child: BlocBuilder<ThemeBloc, ThemeState>(
|
||||||
builder: (context, themeState) => MaterialApp(
|
builder: (context, themeState) => MaterialApp(
|
||||||
|
|
|
@ -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
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final TextStyle _titleTextStyle = TextStyle(
|
final TextStyle _titleTextStyle = TextStyle(
|
||||||
|
@ -56,11 +68,8 @@ class _SettingsViewState extends State<SettingsView> {
|
||||||
tiles: <SettingsTile>[
|
tiles: <SettingsTile>[
|
||||||
SettingsTile.switchTile(
|
SettingsTile.switchTile(
|
||||||
title: 'Automatically determine theme',
|
title: 'Automatically determine theme',
|
||||||
onToggle: (b) {
|
onToggle: toggleAutoTheme,
|
||||||
setState(() => autoThemeEnabled = b);
|
|
||||||
},
|
|
||||||
switchValue: autoThemeEnabled,
|
switchValue: autoThemeEnabled,
|
||||||
enabled: false,
|
|
||||||
switchActiveColor: AppTheme.jishoGreen.background,
|
switchActiveColor: AppTheme.jishoGreen.background,
|
||||||
),
|
),
|
||||||
SettingsTile.switchTile(
|
SettingsTile.switchTile(
|
||||||
|
|
Loading…
Reference in New Issue