Jisho-Study-Tool/lib/main.dart

88 lines
2.3 KiB
Dart
Raw Normal View History

2021-12-04 05:13:13 +01:00
import 'dart:io';
2020-06-28 00:39:45 +02:00
import 'package:flutter/material.dart';
2021-12-04 05:13:13 +01:00
import 'package:get_it/get_it.dart';
2021-07-17 16:11:17 +02:00
import 'package:path/path.dart';
2021-12-01 23:09:53 +01:00
import 'package:path_provider/path_provider.dart';
2021-12-04 05:13:13 +01:00
import 'package:sembast/sembast.dart';
import 'package:sembast/sembast_io.dart';
2021-12-06 20:26:52 +01:00
import 'package:shared_preferences/shared_preferences.dart';
2021-08-03 22:02:42 +02:00
2021-12-01 23:09:53 +01:00
import 'bloc/theme/theme_bloc.dart';
2022-01-19 02:10:05 +01:00
import 'routing/router.dart';
import 'settings.dart';
2021-08-08 23:16:54 +02:00
2021-12-06 20:26:52 +01:00
Future<void> setupDatabase() async {
2021-12-04 05:13:13 +01:00
final Directory appDocDir = await getApplicationDocumentsDirectory();
2021-12-06 20:26:52 +01:00
if (!appDocDir.existsSync()) appDocDir.createSync(recursive: true);
final Database database =
await databaseFactoryIo.openDatabase(join(appDocDir.path, 'sembast.db'));
GetIt.instance.registerSingleton<Database>(database);
}
2020-06-28 00:39:45 +02:00
2021-12-06 20:26:52 +01:00
Future<void> setupSharedPreferences() async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
GetIt.instance.registerSingleton<SharedPreferences>(prefs);
}
2021-07-17 16:11:17 +02:00
2021-12-06 20:26:52 +01:00
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
2021-12-01 23:09:53 +01:00
2021-12-06 20:26:52 +01:00
await Future.wait([
setupDatabase(),
setupSharedPreferences(),
]);
2020-06-28 00:39:45 +02:00
2021-12-04 05:13:13 +01:00
runApp(const MyApp());
}
2021-07-17 16:11:17 +02:00
class MyApp extends StatefulWidget {
2021-12-06 20:26:52 +01:00
const MyApp({Key? key}) : super(key: key);
2021-07-17 16:11:17 +02:00
@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();
}
2020-06-28 00:39:45 +02:00
@override
2020-07-09 20:59:00 +02:00
Widget build(BuildContext context) {
2021-08-08 23:16:54 +02:00
return MultiBlocProvider(
providers: [
BlocProvider(create: (context) => themeBloc),
2021-08-08 23:16:54 +02:00
],
child: BlocBuilder<ThemeBloc, ThemeState>(
2021-12-06 20:26:52 +01:00
builder: (context, themeState) => MaterialApp(
title: 'Jisho Study Tool',
theme: themeState.theme.getMaterialTheme(),
initialRoute: '/',
onGenerateRoute: generateRoute,
),
2021-08-03 22:02:42 +02:00
),
);
}
}