/
ex09_on_navigate_signin.dart
211 lines (193 loc) · 6.11 KB
/
ex09_on_navigate_signin.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import 'package:flutter/material.dart';
import 'package:navigation_builder/navigation_builder.dart';
import 'package:states_rebuilder/scr/development_booster/development_booster.dart';
import 'package:states_rebuilder/scr/state_management/state_management.dart';
// Example of navigation guard that used the API `NavigationBuilder.onNavigate`
// to achive redirection when auth status is invaild.
void main() {
runApp(const MyApp());
}
final navigator = NavigationBuilder.create(
// CASE1: in WEB
// In web try enter '/user-info'. If the user is signed you will navigate
// to the '/user-info'. If the user is not signed, he will redirected to
// tge sign in page and after signing he will be navigated to the '/user-info'
// CASE2: In Other platform try uncomment the below line to see similar effect.
// initialLocation: '/user-info',
routes: {
'/': (data) => const HomePage(),
'/sign-in': (data) => const SignInScreen(),
'/user-info': (data) => const UserInfo(),
},
onNavigate: (data) {
final location = data.location;
if (!authBloc.isSignedIn && location != '/sign-in') {
// User is not signed and tries to enter the app without signing in
//
// Redirect the user to the sign in page
return data.redirectTo('/sign-in');
}
if (authBloc.isSignedIn && location == '/sign-in') {
// User is signed and tries to enter the sign in page
//
// Redirect the user to the home page
return data.redirectTo('/');
}
return null;
},
);
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp.router(
title: 'Books App',
routerConfig: navigator.routerConfig,
);
}
}
// Simple data class for the user
class User {
User(this.username, this.password);
final String username;
final String password;
}
@immutable
// The business logic for signing in and out
class AuthBloc {
final _user = RM.inject<User?>(
() => null,
sideEffects: SideEffects.onData((user) {
if (user != null) {
final toLocation = navigator.routeData.redirectedFrom?.location;
if (toLocation != null) {
// If we are redirected from a deep link to the sign in page, than
// continue to the deep link location after signing.
navigator.toDeeply(toLocation);
}
}
// execute onNavigate callback and navigate according the logic defined there
navigator.onNavigate();
}),
);
bool get isSignedIn => _user.state != null;
User? get user => _user.state;
Future<void> signOut() async {
await Future<void>.delayed(const Duration(milliseconds: 200));
_user.state = null;
}
Future<void> signIn(String username, String password) async {
await Future<void>.delayed(const Duration(milliseconds: 200));
_user.state = User(username, password);
}
}
final authBloc = AuthBloc();
class SignInScreen extends StatelessWidget {
const SignInScreen({
Key? key,
}) : super(key: key);
static final _form = RM.injectForm(
submit: () => authBloc.signIn(
username.text,
password.text,
),
);
static final username = RM.injectTextEditing(
validators: [
(value) {
return value!.isEmpty ? 'Required' : null;
}
],
);
static final password = RM.injectTextEditing(
validators: [
(value) {
return value!.isEmpty ? 'Required' : null;
}
],
);
@override
Widget build(BuildContext context) => Scaffold(
body: Center(
child: Card(
child: Container(
constraints: BoxConstraints.loose(const Size(600, 600)),
padding: const EdgeInsets.all(8),
child: OnFormBuilder(
listenTo: _form,
builder: () {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
Text('Sign in',
style: Theme.of(context).textTheme.headline4),
TextField(
decoration: InputDecoration(
labelText: 'Username',
errorText: username.error,
),
controller: username.controller,
focusNode: username.focusNode,
),
TextField(
decoration: InputDecoration(
labelText: 'Password',
errorText: password.error,
),
obscureText: true,
controller: password.controller,
focusNode: password.focusNode,
),
Padding(
padding: const EdgeInsets.all(16),
child: TextButton(
onPressed: () {
_form.submit();
},
child: const Text('Sign in'),
),
),
],
);
},
),
),
),
),
);
}
class HomePage extends StatelessWidget {
const HomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Home Page'),
),
body: Center(
child: ElevatedButton(
child: const Text('Go to User information'),
onPressed: () => navigator.to('/user-info'),
),
),
);
}
}
class UserInfo extends StatelessWidget {
const UserInfo({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('UserName: ${authBloc.user!.username}'),
),
body: Center(
child: ElevatedButton(
child: const Text('Sign out'),
onPressed: () => authBloc.signOut(),
),
),
);
}
}