Jisho-Study-Tool/lib/main.dart

185 lines
4.5 KiB
Dart
Raw Normal View History

2020-06-28 00:39:45 +02:00
import 'package:flutter/material.dart';
2021-03-03 12:47:35 +01:00
import 'package:mdi/mdi.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-08-03 22:02:42 +02:00
2021-12-01 23:09:53 +01:00
import 'bloc/database/database_bloc.dart';
import 'bloc/theme/theme_bloc.dart';
2021-08-08 23:16:54 +02:00
import 'models/themes/theme.dart';
2021-12-01 23:09:53 +01:00
import 'objectbox.g.dart';
import 'router.dart';
import 'view/components/common/splash.dart';
import 'view/screens/history.dart';
import 'view/screens/search/kanji_view.dart';
import 'view/screens/search/search_view.dart';
import 'view/screens/settings.dart';
2021-08-08 23:16:54 +02:00
2021-12-01 23:09:53 +01:00
void main() => runApp(const MyApp());
2020-06-28 00:39:45 +02:00
2021-07-17 16:11:17 +02:00
DatabaseBloc _databaseBloc = DatabaseBloc();
2021-08-03 22:02:42 +02:00
class MyApp extends StatefulWidget {
2021-12-01 23:09:53 +01:00
const MyApp({Key? key}) : super(key: key);
2020-06-28 00:39:45 +02:00
@override
2021-08-03 22:02:42 +02:00
_MyAppState createState() => _MyAppState();
2020-06-28 00:39:45 +02:00
}
2021-08-03 22:02:42 +02:00
class _MyAppState extends State<MyApp> {
2021-07-26 21:39:17 +02:00
late final Store _store;
2021-08-08 23:16:54 +02:00
bool dbConnected = false;
2021-07-17 16:11:17 +02:00
@override
void initState() {
super.initState();
2021-08-03 22:02:42 +02:00
getApplicationDocumentsDirectory().then((dir) {
_store = Store(
getObjectBoxModel(),
directory: join(dir.path, 'objectbox'),
);
2021-07-17 16:11:17 +02:00
2021-08-03 22:02:42 +02:00
_databaseBloc.add(ConnectedToDatabase(_store));
2021-08-08 23:16:54 +02:00
setState(() {
dbConnected = true;
});
2021-08-03 22:02:42 +02:00
});
2021-07-17 16:11:17 +02:00
}
@override
void dispose() {
_store.close();
2021-12-01 23:09:53 +01:00
_databaseBloc.add(const DisconnectedFromDatabase());
2021-07-17 16:11:17 +02:00
super.dispose();
}
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) => _databaseBloc),
BlocProvider(create: (context) => ThemeBloc()),
],
child: BlocBuilder<ThemeBloc, ThemeState>(
builder: (context, themeState) {
if (!(dbConnected && themeState.prefsAreLoaded))
2021-12-01 23:09:53 +01:00
return const SplashScreen();
2021-08-08 23:16:54 +02:00
return MaterialApp(
title: 'Jisho Study Tool',
theme: themeState.theme.getMaterialTheme(),
initialRoute: '/',
2021-12-01 23:09:53 +01:00
onGenerateRoute: generateRoute,
2021-08-08 23:16:54 +02:00
);
},
2021-08-03 22:02:42 +02:00
),
);
}
}
class Home extends StatefulWidget {
2021-12-01 23:09:53 +01:00
const Home({Key? key}) : super(key: key);
@override
State<StatefulWidget> createState() => _HomeState();
}
class _HomeState extends State<Home> {
int pageNum = 0;
2021-08-03 22:02:42 +02:00
@override
Widget build(BuildContext context) {
return BlocBuilder<ThemeBloc, ThemeState>(
builder: (context, themeState) {
return Scaffold(
appBar: AppBar(
title: pages[pageNum].titleBar,
centerTitle: true,
backgroundColor: AppTheme.jishoGreen.background,
foregroundColor: AppTheme.jishoGreen.foreground,
),
body: Stack(
children: [
Positioned(
right: 30,
left: 100,
bottom: 30,
2021-12-01 23:09:53 +01:00
child: Image.asset(
'assets/images/denshi_jisho_background_overlay.png',
),
2021-08-08 23:16:54 +02:00
),
pages[pageNum].content,
],
),
bottomNavigationBar: BottomNavigationBar(
fixedColor: AppTheme.jishoGreen.background,
currentIndex: pageNum,
2021-12-01 23:09:53 +01:00
onTap: (index) => setState(() {
pageNum = index;
}),
items: pages.map((p) => p.item).toList(),
showSelectedLabels: false,
showUnselectedLabels: false,
unselectedItemColor: themeState.theme.menuGreyDark.background,
),
2021-07-17 16:11:17 +02:00
);
},
2020-07-09 20:59:00 +02:00
);
}
2020-06-30 22:51:32 +02:00
}
2021-03-03 00:24:25 +01:00
class _Page {
2021-08-03 22:02:42 +02:00
final Widget content;
final Widget titleBar;
final BottomNavigationBarItem item;
2020-06-30 22:51:32 +02:00
2021-08-03 22:02:42 +02:00
const _Page({
2021-07-26 21:39:17 +02:00
required this.content,
required this.titleBar,
2021-08-03 22:02:42 +02:00
required this.item,
2020-07-14 14:33:21 +02:00
});
2020-06-30 22:51:32 +02:00
}
2021-03-03 00:24:25 +01:00
final List<_Page> pages = [
2021-12-01 23:09:53 +01:00
const _Page(
2021-03-05 22:39:28 +01:00
content: SearchView(),
titleBar: Text('Search'),
2021-08-03 22:02:42 +02:00
item: BottomNavigationBarItem(
label: 'Search',
icon: Icon(Icons.search),
),
2021-03-05 22:39:28 +01:00
),
2021-12-01 23:09:53 +01:00
const _Page(
2020-07-09 22:17:10 +02:00
content: KanjiView(),
titleBar: Text('Kanji'),
2021-08-03 22:02:42 +02:00
item: BottomNavigationBarItem(
2021-12-01 23:09:53 +01:00
label: 'Kanji',
icon: Icon(Mdi.ideogramCjk, size: 30),
),
2020-07-09 20:59:00 +02:00
),
2021-12-01 23:09:53 +01:00
const _Page(
2020-07-13 23:41:31 +02:00
content: HistoryView(),
2021-12-01 23:09:53 +01:00
titleBar: Text('History'),
2021-08-03 22:02:42 +02:00
item: BottomNavigationBarItem(
label: 'History',
icon: Icon(Icons.history),
),
2020-06-30 22:51:32 +02:00
),
2021-03-03 00:24:25 +01:00
_Page(
2020-07-09 20:59:00 +02:00
content: Container(),
2021-12-01 23:09:53 +01:00
titleBar: const Text('Saved'),
item: const BottomNavigationBarItem(
2021-08-03 22:02:42 +02:00
label: 'Saved',
icon: Icon(Icons.bookmark),
),
2020-06-30 22:51:32 +02:00
),
2021-12-01 23:09:53 +01:00
const _Page(
2021-08-03 22:04:29 +02:00
content: SettingsView(),
2021-12-01 23:09:53 +01:00
titleBar: Text('Settings'),
2021-08-03 22:02:42 +02:00
item: BottomNavigationBarItem(
label: 'Settings',
icon: Icon(Icons.settings),
),
2020-06-30 22:51:32 +02:00
),
2020-07-09 20:59:00 +02:00
];