-
-
Notifications
You must be signed in to change notification settings - Fork 410
/
nested_navigation.router.dart
81 lines (72 loc) · 1.81 KB
/
nested_navigation.router.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import 'package:auto_route/auto_route.dart';
import 'package:example/nested-navigation/nested_navigation.router.gr.dart';
import 'package:flutter/material.dart';
void main() {
runApp(NestedNavigationApp());
}
class NestedNavigationApp extends StatelessWidget {
NestedNavigationApp({super.key});
final nestedRouter = NestedRouter();
@override
Widget build(BuildContext context) {
return MaterialApp.router(
routerConfig: nestedRouter.config(),
);
}
}
@AutoRouterConfig(generateForDir: ['lib/nested-navigation'])
class NestedRouter extends RootStackRouter {
@override
List<AutoRoute> get routes => [
AutoRoute(
initial: true,
page: HostRoute.page,
children: [
AutoRoute(page: FirstRoute.page, initial: true),
AutoRoute(page: SecondRoute.page),
],
),
];
}
@RoutePage()
class HostScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Host Screen'),
/// This will automatically display a back button if the nested router can pop
leading: AutoLeadingButton(),
),
body: AutoRouter(),
);
}
}
@RoutePage()
class FirstScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ElevatedButton(
onPressed: () => context.pushRoute(SecondRoute()),
child: Text('Go to second screen'),
),
),
);
}
}
@RoutePage()
class SecondScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ElevatedButton(
onPressed: () => context.maybePop(),
child: Text('Go Back'),
),
),
);
}
}