rluv_client/lib/main.dart

196 lines
6.0 KiB
Dart
Raw Normal View History

2023-07-27 01:40:26 -06:00
import 'package:animated_splash_screen/animated_splash_screen.dart';
2023-07-15 17:23:27 -06:00
import 'package:flutter/material.dart';
2023-07-19 02:16:13 -06:00
import 'package:flutter_riverpod/flutter_riverpod.dart';
2023-07-22 21:29:32 -06:00
import 'package:helpers/helpers/misc_build/build_media.dart';
2023-07-27 01:40:26 -06:00
import 'package:rluv/features/account/account_create_screen.dart';
2023-07-19 02:16:13 -06:00
import 'package:rluv/features/budget/screens/budget_overview.dart';
2023-07-22 21:29:32 -06:00
import 'package:rluv/features/notes/screens/notes_screen.dart';
import 'package:rluv/features/settings/screens/settings_screen.dart';
2023-07-27 01:40:26 -06:00
import 'package:rluv/global/store.dart';
2023-07-19 02:16:13 -06:00
import 'package:rluv/global/styles.dart';
2023-07-22 21:29:32 -06:00
import 'package:rluv/global/utils.dart';
2023-07-27 01:40:26 -06:00
import 'package:shared_preferences/shared_preferences.dart';
2023-07-15 17:23:27 -06:00
2023-07-27 01:40:26 -06:00
import 'global/api.dart';
import 'global/widgets/ui_button.dart';
2023-07-22 21:29:32 -06:00
void main() async {
2023-07-27 01:40:26 -06:00
// await Api().loadToken();
runApp(const ProviderScope(child: MyApp()));
2023-07-15 17:23:27 -06:00
}
2023-07-27 01:40:26 -06:00
class MyApp extends ConsumerStatefulWidget {
2023-07-15 17:23:27 -06:00
const MyApp({super.key});
2023-07-27 01:40:26 -06:00
@override
ConsumerState<MyApp> createState() => _MyAppState();
}
class _MyAppState extends ConsumerState<MyApp> {
2023-07-15 17:23:27 -06:00
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
2023-07-27 01:40:26 -06:00
return MaterialApp(
scaffoldMessengerKey: ref.read(scaffoldMessengerKeyProvider),
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: AnimatedSplashScreen.withScreenFunction(
curve: Curves.easeInOutCubic,
splashTransition: SplashTransition.scaleTransition,
splashIconSize: 200,
splash: Image.asset('assets/splash_icon.png'),
backgroundColor: Styles.purpleNurple,
duration: 1,
screenFunction: () async {
final prefs = await SharedPreferences.getInstance();
ref.read(prefsProvider.notifier).state = prefs;
Widget nextScreen = const AccountCreateScreen();
if (ref.read(tokenProvider) != null) {
nextScreen = const Home();
}
return nextScreen;
},
2023-07-15 17:23:27 -06:00
),
);
}
}
2023-07-19 02:16:13 -06:00
class Home extends ConsumerStatefulWidget {
const Home({super.key});
2023-07-15 17:23:27 -06:00
@override
2023-07-19 02:16:13 -06:00
ConsumerState<Home> createState() => _HomeState();
2023-07-15 17:23:27 -06:00
}
2023-07-19 02:16:13 -06:00
class _HomeState extends ConsumerState<Home> {
2023-07-22 21:29:32 -06:00
Map<String, dynamic> initData = {};
final drawerColors = Styles.curatedColors;
@override
void initState() {
setDevicePortraitOrientation();
2023-07-27 01:40:26 -06:00
if (!ref.read(loadingStateProvider)) {
ref.read(dashboardProvider.notifier).fetchDashboard();
}
WidgetsBinding.instance.addPostFrameCallback(
(_) {
ref.read(currentHomePageProvider.notifier).state =
const BudgetOverviewScreen(initialData: {});
},
);
2023-07-22 21:29:32 -06:00
super.initState();
}
2023-07-15 17:23:27 -06:00
@override
Widget build(BuildContext context) {
2023-07-27 01:40:26 -06:00
if (ref.watch(tokenProvider) == null) {
WidgetsBinding.instance.addPostFrameCallback((_) {
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(builder: (ctx) => const AccountCreateScreen()),
(r) => false,
);
});
}
2023-07-22 21:29:32 -06:00
if (ref.watch(currentHomePageProvider).toString() ==
"BudgetOverviewScreen") {
initData = {};
}
2023-07-15 17:23:27 -06:00
return Scaffold(
2023-07-22 21:29:32 -06:00
key: ref.read(scaffoldKeyProvider),
2023-07-19 02:16:13 -06:00
resizeToAvoidBottomInset: false,
2023-07-22 21:29:32 -06:00
drawer: Drawer(
2023-07-19 02:16:13 -06:00
backgroundColor: Styles.purpleNurple,
2023-07-22 21:29:32 -06:00
child: SafeArea(
child: Column(
children: [
SizedBox(height: BuildMedia(context).height * 0.15),
2023-07-27 01:40:26 -06:00
UiButton(
2023-07-22 21:29:32 -06:00
text: 'Budget',
color: drawerColors[4],
onPressed: () {
if (ref.read(currentHomePageProvider).toString() !=
"BudgetOverviewScreen") {
ref.read(currentHomePageProvider.notifier).state =
BudgetOverviewScreen(initialData: initData);
}
toggleDrawer();
},
),
2023-07-27 01:40:26 -06:00
UiButton(
2023-07-22 21:29:32 -06:00
text: 'Notes',
color: drawerColors[5],
onPressed: () {
if (ref.read(currentHomePageProvider).toString() !=
"SharedNotesScreen") {
ref.read(currentHomePageProvider.notifier).state =
SharedNotesScreen(initialData: initData);
}
toggleDrawer();
},
),
const Spacer(),
2023-07-27 01:40:26 -06:00
UiButton(
2023-07-22 21:29:32 -06:00
text: 'Settings',
color: drawerColors[0],
onPressed: () {
if (ref.read(currentHomePageProvider).toString() !=
"SettingsScreen") {
ref.read(currentHomePageProvider.notifier).state =
const SettingsScreen();
}
toggleDrawer();
},
),
const SizedBox(height: 24),
],
),
),
2023-07-15 17:23:27 -06:00
),
2023-07-19 02:16:13 -06:00
appBar: AppBar(
backgroundColor: Styles.purpleNurple,
title: Text(
ref.watch(appBarTitleProvider),
2023-07-15 17:23:27 -06:00
),
),
2023-07-19 02:16:13 -06:00
body: ref.watch(currentHomePageProvider),
2023-07-15 17:23:27 -06:00
);
}
2023-07-22 21:29:32 -06:00
toggleDrawer() async {
final key = ref.read(scaffoldKeyProvider);
if (key.currentState != null && key.currentState!.isDrawerOpen) {
key.currentState!.openEndDrawer();
} else if (key.currentState != null) {
key.currentState!.openDrawer();
}
}
2023-07-15 17:23:27 -06:00
}
2023-07-19 02:16:13 -06:00
2023-07-22 21:29:32 -06:00
final scaffoldKeyProvider = Provider<GlobalKey<ScaffoldState>>(
(ref) => GlobalKey<ScaffoldState>(),
);
2023-07-19 02:16:13 -06:00
final currentHomePageProvider = StateProvider<Widget>(
2023-07-27 01:40:26 -06:00
(ref) => Container(),
2023-07-19 02:16:13 -06:00
);
final appBarTitleProvider = Provider<String>(
(ref) {
final currentPageName = ref.watch(currentHomePageProvider).toString();
switch (currentPageName) {
case 'BudgetOverviewScreen':
return 'Budget';
2023-07-22 21:29:32 -06:00
case 'SharedNotesScreen':
return 'Notes';
2023-07-19 02:16:13 -06:00
default:
return '';
}
},
);
2023-07-27 01:40:26 -06:00
final scaffoldMessengerKeyProvider =
Provider<GlobalKey<ScaffoldMessengerState>>(
(ref) => GlobalKey<ScaffoldMessengerState>());