finnow/flutter_app/lib/main.dart

65 lines
2.0 KiB
Dart

import 'package:finnow_app/api/main.dart';
import 'package:finnow_app/auth/model.dart';
import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
import 'package:go_router/go_router.dart';
import 'package:watch_it/watch_it.dart';
import 'app.dart';
import 'login_page.dart';
import 'profiles_page.dart';
import 'register_page.dart';
final getIt = GetIt.instance;
void main() {
setup();
runApp(const FinnowApp());
}
void setup() {
getIt.registerSingleton<FinnowApi>(FinnowApi());
getIt.registerSingleton<AuthenticationModel>(AuthenticationModel());
getIt.registerSingleton<GoRouter>(getRouterConfig());
}
GoRouter getRouterConfig() {
return GoRouter(routes: [
GoRoute(path: '/login', builder: (context, state) => const LoginPage()),
GoRoute(
path: '/register', builder: (context, state) => const RegisterPage()),
// Once a user has logged in, they're directed to a scaffold for the /profiles page.
ShellRoute(
builder: (context, state, child) => Scaffold(
body: child,
appBar: AppBar(
title: const Text('Finnow'),
backgroundColor: Colors.grey,
actions: [
TextButton(
onPressed: () => getIt<AuthenticationModel>().state =
Unauthenticated(),
child: const Text('Logout'))
],
),
),
redirect: (context, state) {
final bool authenticated =
getIt<AuthenticationModel>().state.authenticated();
return authenticated ? null : '/login';
},
routes: [
GoRoute(
path: '/',
redirect: (context, state) {
final bool authenticated =
getIt<AuthenticationModel>().state.authenticated();
return authenticated ? '/profiles' : '/login';
}),
GoRoute(
path: '/profiles',
builder: (context, state) => const ProfilesPage(),
)
]),
]);
}