Skip to content

Commit adb6bee

Browse files
firebase-get-to-know-flutter: add go_router (flutter#1210)
1 parent ef866a3 commit adb6bee

16 files changed

Lines changed: 353 additions & 275 deletions

File tree

firebase-get-to-know-flutter/step_02/lib/src/authentication.dart

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// found in the LICENSE file.
44

55
import 'package:flutter/material.dart';
6+
import 'package:go_router/go_router.dart';
67

78
import 'widgets.dart';
89

@@ -24,9 +25,7 @@ class AuthFunc extends StatelessWidget {
2425
padding: const EdgeInsets.only(left: 24, bottom: 8),
2526
child: StyledButton(
2627
onPressed: () {
27-
!loggedIn
28-
? Navigator.of(context).pushNamed('/sign-in')
29-
: signOut();
28+
!loggedIn ? context.push('/sign-in') : signOut();
3029
},
3130
child: !loggedIn ? const Text('RSVP') : const Text('Logout')),
3231
),
@@ -36,7 +35,7 @@ class AuthFunc extends StatelessWidget {
3635
padding: const EdgeInsets.only(left: 24, bottom: 8),
3736
child: StyledButton(
3837
onPressed: () {
39-
Navigator.of(context).pushNamed('/profile');
38+
context.push('/profile');
4039
},
4140
child: const Text('Profile')),
4241
))

firebase-get-to-know-flutter/step_02/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ dependencies:
3737
# Use with the CupertinoIcons class for iOS style icons.
3838
cupertino_icons: ^1.0.2
3939
google_fonts: ^3.0.1
40+
go_router: ^5.2.1
4041

4142
dev_dependencies:
4243
flutter_test:

firebase-get-to-know-flutter/step_04/lib/src/authentication.dart

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// found in the LICENSE file.
44

55
import 'package:flutter/material.dart';
6+
import 'package:go_router/go_router.dart';
67

78
import 'widgets.dart';
89

@@ -24,9 +25,7 @@ class AuthFunc extends StatelessWidget {
2425
padding: const EdgeInsets.only(left: 24, bottom: 8),
2526
child: StyledButton(
2627
onPressed: () {
27-
!loggedIn
28-
? Navigator.of(context).pushNamed('/sign-in')
29-
: signOut();
28+
!loggedIn ? context.push('/sign-in') : signOut();
3029
},
3130
child: !loggedIn ? const Text('RSVP') : const Text('Logout')),
3231
),
@@ -36,7 +35,7 @@ class AuthFunc extends StatelessWidget {
3635
padding: const EdgeInsets.only(left: 24, bottom: 8),
3736
child: StyledButton(
3837
onPressed: () {
39-
Navigator.of(context).pushNamed('/profile');
38+
context.push('/profile');
4039
},
4140
child: const Text('Profile')),
4241
))

firebase-get-to-know-flutter/step_04/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ dependencies:
3737
# Use with the CupertinoIcons class for iOS style icons.
3838
cupertino_icons: ^1.0.2
3939
google_fonts: ^3.0.1
40+
go_router: ^5.2.1
4041
cloud_firestore: ^4.1.0
4142
firebase_auth: ^4.1.4
4243
firebase_core: ^2.3.0

firebase-get-to-know-flutter/step_05/lib/main.dart

Lines changed: 78 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'package:firebase_ui_auth/firebase_ui_auth.dart';
66
import 'package:flutter/material.dart';
7+
import 'package:go_router/go_router.dart';
78
import 'package:google_fonts/google_fonts.dart';
89
import 'package:provider/provider.dart';
910

@@ -19,70 +20,87 @@ void main() {
1920
));
2021
}
2122

23+
final _router = GoRouter(
24+
routes: [
25+
GoRoute(
26+
path: '/',
27+
builder: (context, state) => const HomePage(),
28+
routes: [
29+
GoRoute(
30+
path: 'sign-in',
31+
builder: (context, state) {
32+
return SignInScreen(
33+
actions: [
34+
ForgotPasswordAction(((context, email) {
35+
final uri = Uri(
36+
path: '/sign-in/forgot-password',
37+
queryParameters: <String, String?>{
38+
'email': email,
39+
},
40+
);
41+
context.push(uri.toString());
42+
})),
43+
AuthStateChangeAction(((context, state) {
44+
if (state is SignedIn || state is UserCreated) {
45+
var user = (state is SignedIn)
46+
? state.user
47+
: (state as UserCreated).credential.user;
48+
if (user == null) {
49+
return;
50+
}
51+
if (state is UserCreated) {
52+
user.updateDisplayName(user.email!.split('@')[0]);
53+
}
54+
if (!user.emailVerified) {
55+
user.sendEmailVerification();
56+
const snackBar = SnackBar(
57+
content: Text(
58+
'Please check your email to verify your email address'));
59+
ScaffoldMessenger.of(context).showSnackBar(snackBar);
60+
}
61+
context.replace('/');
62+
}
63+
})),
64+
],
65+
);
66+
},
67+
routes: [
68+
GoRoute(
69+
path: 'forgot-password',
70+
builder: (context, state) {
71+
final arguments = state.queryParams;
72+
return ForgotPasswordScreen(
73+
email: arguments['email'],
74+
headerMaxExtent: 200,
75+
);
76+
},
77+
),
78+
],
79+
),
80+
GoRoute(
81+
path: 'profile',
82+
builder: (context, state) {
83+
return ProfileScreen(
84+
providers: const [],
85+
actions: [
86+
SignedOutAction((context) {
87+
context.replace('/');
88+
}),
89+
],
90+
);
91+
},
92+
),
93+
],
94+
),
95+
],
96+
);
97+
2298
class App extends StatelessWidget {
2399
const App({super.key});
24100

25101
@override
26102
Widget build(BuildContext context) {
27-
return MaterialApp(
28-
initialRoute: '/home',
29-
routes: {
30-
'/home': (context) {
31-
return const HomePage();
32-
},
33-
'/sign-in': ((context) {
34-
return SignInScreen(
35-
actions: [
36-
ForgotPasswordAction(((context, email) {
37-
Navigator.of(context)
38-
.pushNamed('/forgot-password', arguments: {'email': email});
39-
})),
40-
AuthStateChangeAction(((context, state) {
41-
if (state is SignedIn || state is UserCreated) {
42-
var user = (state is SignedIn)
43-
? state.user
44-
: (state as UserCreated).credential.user;
45-
if (user == null) {
46-
return;
47-
}
48-
if (state is UserCreated) {
49-
user.updateDisplayName(user.email!.split('@')[0]);
50-
}
51-
if (!user.emailVerified) {
52-
user.sendEmailVerification();
53-
const snackBar = SnackBar(
54-
content: Text(
55-
'Please check your email to verify your email address'));
56-
ScaffoldMessenger.of(context).showSnackBar(snackBar);
57-
}
58-
Navigator.of(context).popUntil(ModalRoute.withName('/home'));
59-
}
60-
})),
61-
],
62-
);
63-
}),
64-
'/forgot-password': ((context) {
65-
final arguments = ModalRoute.of(context)?.settings.arguments
66-
as Map<String, dynamic>?;
67-
68-
return ForgotPasswordScreen(
69-
email: arguments?['email'] as String,
70-
headerMaxExtent: 200,
71-
);
72-
}),
73-
'/profile': ((context) {
74-
return ProfileScreen(
75-
providers: const [],
76-
actions: [
77-
SignedOutAction(
78-
((context) {
79-
Navigator.of(context).popUntil(ModalRoute.withName('/home'));
80-
}),
81-
),
82-
],
83-
);
84-
})
85-
},
103+
return MaterialApp.router(
86104
title: 'Firebase Meetup',
87105
theme: ThemeData(
88106
buttonTheme: Theme.of(context).buttonTheme.copyWith(
@@ -94,6 +112,7 @@ class App extends StatelessWidget {
94112
),
95113
visualDensity: VisualDensity.adaptivePlatformDensity,
96114
),
115+
routerConfig: _router,
97116
);
98117
}
99118
}

firebase-get-to-know-flutter/step_05/lib/src/authentication.dart

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// found in the LICENSE file.
44

55
import 'package:flutter/material.dart';
6+
import 'package:go_router/go_router.dart';
67

78
import 'widgets.dart';
89

@@ -24,9 +25,7 @@ class AuthFunc extends StatelessWidget {
2425
padding: const EdgeInsets.only(left: 24, bottom: 8),
2526
child: StyledButton(
2627
onPressed: () {
27-
!loggedIn
28-
? Navigator.of(context).pushNamed('/sign-in')
29-
: signOut();
28+
!loggedIn ? context.push('/sign-in') : signOut();
3029
},
3130
child: !loggedIn ? const Text('RSVP') : const Text('Logout')),
3231
),
@@ -36,7 +35,7 @@ class AuthFunc extends StatelessWidget {
3635
padding: const EdgeInsets.only(left: 24, bottom: 8),
3736
child: StyledButton(
3837
onPressed: () {
39-
Navigator.of(context).pushNamed('/profile');
38+
context.push('/profile');
4039
},
4140
child: const Text('Profile')),
4241
))

firebase-get-to-know-flutter/step_05/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ dependencies:
3737
# Use with the CupertinoIcons class for iOS style icons.
3838
cupertino_icons: ^1.0.2
3939
google_fonts: ^3.0.1
40+
go_router: ^5.2.1
4041
cloud_firestore: ^4.1.0
4142
firebase_auth: ^4.1.4
4243
firebase_core: ^2.3.0

0 commit comments

Comments
 (0)