mirror of
https://github.com/flutter/samples.git
synced 2026-07-15 13:32:35 +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,28 +4,30 @@
|
||||
|
||||
import 'package:adaptive_navigation/adaptive_navigation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../routing.dart';
|
||||
import 'scaffold_body.dart';
|
||||
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 routeState = RouteStateScope.of(context);
|
||||
final selectedIndex = _getSelectedIndex(routeState.route.pathTemplate);
|
||||
final goRouter = GoRouter.of(context);
|
||||
|
||||
return Scaffold(
|
||||
body: AdaptiveNavigationScaffold(
|
||||
selectedIndex: selectedIndex,
|
||||
body: const BookstoreScaffoldBody(),
|
||||
body: child,
|
||||
onDestinationSelected: (idx) {
|
||||
if (idx == 0) routeState.go('/books/popular');
|
||||
if (idx == 1) routeState.go('/authors');
|
||||
if (idx == 2) routeState.go('/settings');
|
||||
if (idx == 0) goRouter.go('/books/popular');
|
||||
if (idx == 1) goRouter.go('/authors');
|
||||
if (idx == 2) goRouter.go('/settings');
|
||||
},
|
||||
destinations: const [
|
||||
AdaptiveScaffoldDestination(
|
||||
@@ -44,11 +46,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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user