This repository has been archived by the owner on Feb 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 96
/
main.dart
158 lines (143 loc) · 4.54 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright 2021, the Flutter project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'src/auth.dart';
import 'src/data/library.dart';
import 'src/screens/author_details.dart';
import 'src/screens/authors.dart';
import 'src/screens/book_details.dart';
import 'src/screens/books.dart';
import 'src/screens/scaffold.dart';
import 'src/screens/settings.dart';
import 'src/screens/sign_in.dart';
void main() => runApp(Bookstore());
class Bookstore extends StatelessWidget {
Bookstore({Key? key}) : super(key: key);
final _scaffoldKey = const ValueKey<String>('App scaffold');
@override
Widget build(BuildContext context) => BookstoreAuthScope(
notifier: _auth,
child: MaterialApp.router(
routerDelegate: _router.routerDelegate,
routeInformationParser: _router.routeInformationParser,
),
);
final _auth = BookstoreAuth();
late final _router = GoRouter(
routes: [
GoRoute(
path: '/',
redirect: (_) => '/books',
),
GoRoute(
path: '/signin',
pageBuilder: (context, state) => FadeTransitionPage(
key: state.pageKey,
child: SignInScreen(
onSignIn: (credentials) {
BookstoreAuthScope.of(context)
.signIn(credentials.username, credentials.password);
},
),
),
),
GoRoute(
path: '/books',
redirect: (_) => '/books/popular',
),
GoRoute(
path: '/book/:bookId',
redirect: (state) => '/books/all/${state.params['bookId']}',
),
GoRoute(
path: '/books/:kind(new|all|popular)',
pageBuilder: (context, state) => FadeTransitionPage(
key: _scaffoldKey,
child: BookstoreScaffold(
selectedTab: ScaffoldTab.books,
child: BooksScreen(state.params['kind']!),
),
),
routes: [
GoRoute(
path: ':bookId',
builder: (context, state) {
final bookId = state.params['bookId']!;
final selectedBook = libraryInstance.allBooks
.firstWhereOrNull((b) => b.id.toString() == bookId);
return BookDetailsScreen(book: selectedBook);
},
),
],
),
GoRoute(
path: '/author/:authorId',
redirect: (state) => '/authors/${state.params['authorId']}',
),
GoRoute(
path: '/authors',
pageBuilder: (context, state) => FadeTransitionPage(
key: _scaffoldKey,
child: const BookstoreScaffold(
selectedTab: ScaffoldTab.authors,
child: AuthorsScreen(),
),
),
routes: [
GoRoute(
path: ':authorId',
builder: (context, state) {
final authorId = int.parse(state.params['authorId']!);
final selectedAuthor = libraryInstance.allAuthors
.firstWhereOrNull((a) => a.id == authorId);
return AuthorDetailsScreen(author: selectedAuthor);
},
),
],
),
GoRoute(
path: '/settings',
pageBuilder: (context, state) => FadeTransitionPage(
key: _scaffoldKey,
child: const BookstoreScaffold(
selectedTab: ScaffoldTab.settings,
child: SettingsScreen(),
),
),
),
],
redirect: _guard,
refreshListenable: _auth,
debugLogDiagnostics: true,
);
String? _guard(GoRouterState state) {
final signedIn = _auth.signedIn;
final signingIn = state.subloc == '/signin';
// Go to /signin if the user is not signed in
if (!signedIn && !signingIn) {
return '/signin';
}
// Go to /books if the user is signed in and tries to go to /signin.
else if (signedIn && signingIn) {
return '/books';
}
// no redirect
return null;
}
}
class FadeTransitionPage extends CustomTransitionPage<void> {
FadeTransitionPage({
required LocalKey key,
required Widget child,
}) : super(
key: key,
transitionsBuilder: (c, animation, a2, child) => FadeTransition(
opacity: animation.drive(_curveTween),
child: child,
),
child: child);
static final _curveTween = CurveTween(curve: Curves.easeIn);
}