diff --git a/navigation_and_routing/lib/src/app.dart b/navigation_and_routing/lib/src/app.dart index f930d152d..d5b02f8c6 100644 --- a/navigation_and_routing/lib/src/app.dart +++ b/navigation_and_routing/lib/src/app.dart @@ -48,7 +48,7 @@ class _BookstoreState extends State { 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 { 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 { // 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 { builder: (context, state) { return BookDetailsScreen( book: libraryInstance - .getBook(state.params['bookId'] ?? ''), + .getBook(state.pathParameters['bookId'] ?? ''), ); }, ), @@ -149,7 +156,7 @@ class _BookstoreState extends State { builder: (context, state) { return BookDetailsScreen( book: libraryInstance - .getBook(state.params['bookId'] ?? ''), + .getBook(state.pathParameters['bookId'] ?? ''), ); }, ), @@ -182,7 +189,7 @@ class _BookstoreState extends State { builder: (context, state) { return BookDetailsScreen( book: libraryInstance - .getBook(state.params['bookId'] ?? ''), + .getBook(state.pathParameters['bookId'] ?? ''), ); }, ), @@ -212,7 +219,7 @@ class _BookstoreState extends State { 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) { diff --git a/navigation_and_routing/lib/src/screens/book_details.dart b/navigation_and_routing/lib/src/screens/book_details.dart index 4f43d5872..4e8b97e94 100644 --- a/navigation_and_routing/lib/src/screens/book_details.dart +++ b/navigation_and_routing/lib/src/screens/book_details.dart @@ -48,7 +48,7 @@ class BookDetailsScreen extends StatelessWidget { MaterialPageRoute( builder: (context) => AuthorDetailsScreen( author: book!.author, - onBookTapped: (Book book) { + onBookTapped: (book) { GoRouter.of(context).go('/books/all/book/${book.id}'); }, ), diff --git a/navigation_and_routing/lib/src/screens/books.dart b/navigation_and_routing/lib/src/screens/books.dart index be89e1dca..823c5aa53 100644 --- a/navigation_and_routing/lib/src/screens/books.dart +++ b/navigation_and_routing/lib/src/screens/books.dart @@ -8,10 +8,12 @@ import 'package:go_router/go_router.dart'; class BooksScreen extends StatefulWidget { final Widget child; final ValueChanged 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 @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 } 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); } } diff --git a/navigation_and_routing/lib/src/screens/scaffold.dart b/navigation_and_routing/lib/src/screens/scaffold.dart index dffba6da1..7d9ff5109 100644 --- a/navigation_and_routing/lib/src/screens/scaffold.dart +++ b/navigation_and_routing/lib/src/screens/scaffold.dart @@ -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; - } } diff --git a/navigation_and_routing/pubspec.yaml b/navigation_and_routing/pubspec.yaml index ade4fdf98..2c91eb6b8 100644 --- a/navigation_and_routing/pubspec.yaml +++ b/navigation_and_routing/pubspec.yaml @@ -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 \ No newline at end of file