Update navigation_and_routing sample to GoRouter

pull/2067/head
John Ryan 2 years ago
parent 4211056a38
commit b2ed59c120

@ -48,7 +48,7 @@ class _BookstoreState extends State<Bookstore> {
initialLocation: '/books/popular',
redirect: (context, state) {
final signedIn = BookstoreAuth.of(context).signedIn;
if (state.location != '/sign-in' && !signedIn) {
if (state.uri.toString() != '/sign-in' && !signedIn) {
return '/sign-in';
}
return null;
@ -59,6 +59,12 @@ class _BookstoreState extends State<Bookstore> {
builder: (context, state, child) {
return BookstoreScaffold(
child: child,
selectedIndex: switch (state.uri.path) {
var p when p.startsWith('/books') => 0,
var p when p.startsWith('/authors') => 1,
var p when p.startsWith('/settings') => 2,
_ => 0,
},
);
},
routes: [
@ -70,20 +76,21 @@ class _BookstoreState extends State<Bookstore> {
// TODO (johnpryan): remove when https://github.com/flutter/flutter/issues/108177 lands
child: Builder(builder: (context) {
return BooksScreen(
child: child,
onTap: (idx) {
switch (idx) {
case 1:
GoRouter.of(context).go('/books/new');
break;
case 2:
GoRouter.of(context).go('/books/all');
break;
case 0:
default:
GoRouter.of(context).go('/books/popular');
}
GoRouter.of(context).go(switch (idx) {
0 => '/books/popular',
1 => '/books/new',
2 => '/books/all',
_ => '/books/popular',
});
},
selectedIndex: switch (state.uri.path) {
var p when p.startsWith('/books/popular') => 0,
var p when p.startsWith('/books/new') => 1,
var p when p.startsWith('/books/all') => 2,
_ => 0,
},
child: child,
);
}),
);
@ -116,7 +123,7 @@ class _BookstoreState extends State<Bookstore> {
builder: (context, state) {
return BookDetailsScreen(
book: libraryInstance
.getBook(state.params['bookId'] ?? ''),
.getBook(state.pathParameters['bookId'] ?? ''),
);
},
),
@ -149,7 +156,7 @@ class _BookstoreState extends State<Bookstore> {
builder: (context, state) {
return BookDetailsScreen(
book: libraryInstance
.getBook(state.params['bookId'] ?? ''),
.getBook(state.pathParameters['bookId'] ?? ''),
);
},
),
@ -182,7 +189,7 @@ class _BookstoreState extends State<Bookstore> {
builder: (context, state) {
return BookDetailsScreen(
book: libraryInstance
.getBook(state.params['bookId'] ?? ''),
.getBook(state.pathParameters['bookId'] ?? ''),
);
},
),
@ -212,7 +219,7 @@ class _BookstoreState extends State<Bookstore> {
final author = libraryInstance.allAuthors.firstWhere(
(author) =>
author.id ==
int.parse(state.params['authorId']!));
int.parse(state.pathParameters['authorId']!));
// Use a builder to get the correct BuildContext
// TODO (johnpryan): remove when https://github.com/flutter/flutter/issues/108177 lands
return Builder(builder: (context) {

@ -48,7 +48,7 @@ class BookDetailsScreen extends StatelessWidget {
MaterialPageRoute<void>(
builder: (context) => AuthorDetailsScreen(
author: book!.author,
onBookTapped: (Book book) {
onBookTapped: (book) {
GoRouter.of(context).go('/books/all/book/${book.id}');
},
),

@ -8,10 +8,12 @@ import 'package:go_router/go_router.dart';
class BooksScreen extends StatefulWidget {
final Widget child;
final ValueChanged<int> onTap;
final int selectedIndex;
const BooksScreen({
required this.child,
required this.onTap,
required this.selectedIndex,
super.key,
});
@ -38,7 +40,7 @@ class _BooksScreenState extends State<BooksScreen>
@override
Widget build(BuildContext context) {
_tabController.index = _getSelectedIndex(GoRouter.of(context).location);
_tabController.index = widget.selectedIndex;
return Scaffold(
appBar: AppBar(
title: const Text('Books'),
@ -65,15 +67,6 @@ class _BooksScreenState extends State<BooksScreen>
}
void _handleTabIndexChanged() {
if (_tabController.index != _getSelectedIndex(GoRouter.of(context).location)) {
widget.onTap(_tabController.index);
}
}
int _getSelectedIndex(String pathTemplate) {
if (pathTemplate.startsWith('/books/popular')) return 0;
if (pathTemplate.startsWith('/books/new')) return 1;
if (pathTemplate.startsWith('/books/all')) return 2;
return 0;
widget.onTap(_tabController.index);
}
}

@ -8,16 +8,17 @@ import 'package:go_router/go_router.dart';
class BookstoreScaffold extends StatelessWidget {
final Widget child;
final int selectedIndex;
const BookstoreScaffold({
required this.child,
required this.selectedIndex,
super.key,
});
@override
Widget build(BuildContext context) {
final goRouter = GoRouter.of(context);
final selectedIndex = _getSelectedIndex(goRouter.location);
return Scaffold(
body: AdaptiveNavigationScaffold(
@ -46,10 +47,4 @@ class BookstoreScaffold extends StatelessWidget {
);
}
int _getSelectedIndex(String pathTemplate) {
if (pathTemplate.startsWith('/books')) return 0;
if (pathTemplate == '/authors') return 1;
if (pathTemplate == '/settings') return 2;
return 0;
}
}

@ -4,15 +4,15 @@ publish_to: "none" # Remove this line if you wish to publish to pub.dev
version: 1.0.0+1
environment:
sdk: ">=2.17.0-0 <3.0.0"
sdk: ">=3.0.0 <4.0.0"
dependencies:
adaptive_navigation: ^0.0.3
collection: ^1.15.0
collection: ^1.17.0
cupertino_icons: ^1.0.2
flutter:
sdk: flutter
go_router: ^5.0.0
go_router: ^12.0.0
path_to_regexp: ^0.4.0
quiver: ^3.1.0
url_launcher: ^6.1.1
@ -26,7 +26,7 @@ dev_dependencies:
flutter_lints: ^2.0.1
flutter_test:
sdk: flutter
test: ^1.16.0
test: ^1.24.0
flutter:
uses-material-design: true
Loading…
Cancel
Save