mirror of
https://github.com/flutter/samples.git
synced 2026-07-15 05:21:30 +00:00
Update navigation_and_routing sample to go_router (#2067)
This is a new PR to update the navigation_and_routing sample to the latest version of go_router. It is a based on https://github.com/flutter/samples/pull/1437 --------- Co-authored-by: Brett Morgan <brettmorgan@google.com>
This commit is contained in:
@@ -4,12 +4,15 @@
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../data.dart';
|
||||
import '../routing.dart';
|
||||
import '../widgets/book_list.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,
|
||||
});
|
||||
|
||||
@@ -28,20 +31,6 @@ class _BooksScreenState extends State<BooksScreen>
|
||||
..addListener(_handleTabIndexChanged);
|
||||
}
|
||||
|
||||
@override
|
||||
void didChangeDependencies() {
|
||||
super.didChangeDependencies();
|
||||
|
||||
final newPath = _routeState.route.pathTemplate;
|
||||
if (newPath.startsWith('/books/popular')) {
|
||||
_tabController.index = 0;
|
||||
} else if (newPath.startsWith('/books/new')) {
|
||||
_tabController.index = 1;
|
||||
} else if (newPath == '/books/all') {
|
||||
_tabController.index = 2;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_tabController.removeListener(_handleTabIndexChanged);
|
||||
@@ -49,63 +38,34 @@ class _BooksScreenState extends State<BooksScreen>
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Books'),
|
||||
elevation: 8,
|
||||
bottom: TabBar(
|
||||
controller: _tabController,
|
||||
tabs: const [
|
||||
Tab(
|
||||
text: 'Popular',
|
||||
icon: Icon(Icons.people),
|
||||
),
|
||||
Tab(
|
||||
text: 'New',
|
||||
icon: Icon(Icons.new_releases),
|
||||
),
|
||||
Tab(
|
||||
text: 'All',
|
||||
icon: Icon(Icons.list),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
body: TabBarView(
|
||||
Widget build(BuildContext context) {
|
||||
_tabController.index = widget.selectedIndex;
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Books'),
|
||||
bottom: TabBar(
|
||||
controller: _tabController,
|
||||
children: [
|
||||
BookList(
|
||||
books: libraryInstance.popularBooks,
|
||||
onTap: _handleBookTapped,
|
||||
tabs: const [
|
||||
Tab(
|
||||
text: 'Popular',
|
||||
icon: Icon(Icons.people),
|
||||
),
|
||||
BookList(
|
||||
books: libraryInstance.newBooks,
|
||||
onTap: _handleBookTapped,
|
||||
Tab(
|
||||
text: 'New',
|
||||
icon: Icon(Icons.new_releases),
|
||||
),
|
||||
BookList(
|
||||
books: libraryInstance.allBooks,
|
||||
onTap: _handleBookTapped,
|
||||
Tab(
|
||||
text: 'All',
|
||||
icon: Icon(Icons.list),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
RouteState get _routeState => RouteStateScope.of(context);
|
||||
|
||||
void _handleBookTapped(Book book) {
|
||||
_routeState.go('/book/${book.id}');
|
||||
),
|
||||
body: widget.child,
|
||||
);
|
||||
}
|
||||
|
||||
void _handleTabIndexChanged() {
|
||||
switch (_tabController.index) {
|
||||
case 1:
|
||||
_routeState.go('/books/new');
|
||||
case 2:
|
||||
_routeState.go('/books/all');
|
||||
case 0:
|
||||
default:
|
||||
_routeState.go('/books/popular');
|
||||
break;
|
||||
}
|
||||
widget.onTap(_tabController.index);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user