8
8
9
9
<? code-excerpt path-base="cookbook/navigation/navigation_basics"?>
10
10
11
- Most apps contain several screens for displaying different types of
12
- information.
13
- For example, an app might have a screen that displays products.
14
- When the user taps the image of a product, a new screen displays
15
- details about the product.
11
+ Most apps contain several screens for displaying different
12
+ types of information. For example, an app might have a
13
+ screen that displays products. When the user taps the image
14
+ of a product, a new screen displays details about the
15
+ product.
16
16
17
17
:::note Terminology
18
18
In Flutter, _ screens_ and _ pages_ are called _ routes_ .
@@ -29,8 +29,8 @@ The next few sections show how to navigate between two routes,
29
29
using these steps:
30
30
31
31
1 . Create two routes.
32
- 2 . Navigate to the second route using Navigator.push().
33
- 3 . Return to the first route using Navigator.pop().
32
+ 2 . Navigate to the second route using ` Navigator.push() ` .
33
+ 3 . Return to the first route using ` Navigator.pop() ` .
34
34
35
35
## 1. Create two routes
36
36
@@ -41,6 +41,10 @@ second route returns to the first route.
41
41
42
42
First, set up the visual structure:
43
43
44
+ {% tabs "os-android" %}
45
+
46
+ {% tab "Android" %}
47
+
44
48
<? code-excerpt "lib/main_step1.dart (first-second-routes)"?>
45
49
``` dart
46
50
class FirstRoute extends StatelessWidget {
@@ -82,18 +86,72 @@ class SecondRoute extends StatelessWidget {
82
86
}
83
87
```
84
88
89
+ {% endtab %}
90
+
91
+ {% tab "iOS" %}
92
+
93
+ <? code-excerpt "lib/main_step1_cupertino.dart (first-second-routes)"?>
94
+ ``` dart
95
+ class FirstRoute extends StatelessWidget {
96
+ const FirstRoute({super.key});
97
+
98
+ @override
99
+ Widget build(BuildContext context) {
100
+ return CupertinoPageScaffold(
101
+ navigationBar: const CupertinoNavigationBar(middle: Text('First Route')),
102
+ child: Center(
103
+ child: CupertinoButton(
104
+ child: const Text('Open route'),
105
+ onPressed: () {
106
+ // Navigate to second route when tapped.
107
+ },
108
+ ),
109
+ ),
110
+ );
111
+ }
112
+ }
113
+
114
+ class SecondRoute extends StatelessWidget {
115
+ const SecondRoute({super.key});
116
+
117
+ @override
118
+ Widget build(BuildContext context) {
119
+ return CupertinoPageScaffold(
120
+ navigationBar: const CupertinoNavigationBar(middle: Text('Second Route')),
121
+ child: Center(
122
+ child: CupertinoButton(
123
+ onPressed: () {
124
+ // Navigate back to first route when tapped.
125
+ },
126
+ child: const Text('Go back!'),
127
+ ),
128
+ ),
129
+ );
130
+ }
131
+ }
132
+ ```
133
+
134
+ {% endtab %}
135
+
136
+ {% endtabs %}
137
+
85
138
## 2. Navigate to the second route using Navigator.push()
86
139
87
140
To switch to a new route, use the [ ` Navigator.push() ` ] [ ]
88
141
method. The ` push() ` method adds a ` Route ` to the stack of routes managed by
89
142
the ` Navigator ` . Where does the ` Route ` come from?
90
- You can create your own, or use a [ ` MaterialPageRoute ` ] [ ] ,
91
- which is useful because it transitions to the
92
- new route using a platform-specific animation.
143
+ You can create your own, or use a platform-specific route
144
+ such as [ ` MaterialPageRoute ` ] [ ] or [ ` CupertinoPageRoute ` ] [ ] .
145
+ A platform-specific route is useful because it transitions
146
+ to the new route using a platform-specific animation.
93
147
94
148
In the ` build() ` method of the ` FirstRoute ` widget,
95
149
update the ` onPressed() ` callback:
96
150
151
+ {% tabs "os-android" %}
152
+
153
+ {% tab "Android" %}
154
+
97
155
<? code-excerpt "lib/main_step2.dart (first-route-on-pressed)" replace="/^\},$/}/g"?>
98
156
``` dart
99
157
// Within the `FirstRoute` widget:
@@ -105,6 +163,25 @@ onPressed: () {
105
163
}
106
164
```
107
165
166
+ {% endtab %}
167
+
168
+ {% tab "iOS" %}
169
+
170
+ <? code-excerpt "lib/main_step2_cupertino.dart (first-route-on-pressed)" replace="/^\},$/}/g"?>
171
+ ``` dart
172
+ // Within the `FirstRoute` widget:
173
+ onPressed: () {
174
+ Navigator.push(
175
+ context,
176
+ CupertinoPageRoute(builder: (context) => const SecondRoute()),
177
+ );
178
+ }
179
+ ```
180
+
181
+ {% endtab %}
182
+
183
+ {% endtabs %}
184
+
108
185
## 3. Return to the first route using Navigator.pop()
109
186
110
187
How do you close the second route and return to the first?
@@ -125,6 +202,10 @@ onPressed: () {
125
202
126
203
## Interactive example
127
204
205
+ {% tabs "os-android" %}
206
+
207
+ {% tab "Android" %}
208
+
128
209
<? code-excerpt "lib/main.dart"?>
129
210
``` dartpad title="Flutter navigation hands-on example in DartPad" run="true"
130
211
import 'package:flutter/material.dart';
@@ -179,32 +260,9 @@ class SecondRoute extends StatelessWidget {
179
260
<img src =" /assets/images/docs/cookbook/navigation-basics.gif " alt =" Navigation Basics Demo " class =" site-mobile-screenshot " />
180
261
</noscript >
181
262
182
- ## Navigation with CupertinoPageRoute
263
+ {% endtab %}
183
264
184
- In the previous example you learned how to navigate between screens
185
- using the [ ` MaterialPageRoute ` ] [ ] from [ Material Components] [ ] .
186
- However, in Flutter you are not limited to Material design language,
187
- instead, you also have access to [ Cupertino] [ ] (iOS-style) widgets.
188
-
189
- Implementing navigation with Cupertino widgets follows the same steps
190
- as when using [ ` MaterialPageRoute ` ] [ ] ,
191
- but instead you use [ ` CupertinoPageRoute ` ] [ ]
192
- which provides an iOS-style transition animation.
193
-
194
- In the following example, these widgets have been replaced:
195
-
196
- - [ ` MaterialApp ` ] [ ] replaced by [ ` CupertinoApp ` ] .
197
- - [ ` Scaffold ` ] [ ] replaced by [ ` CupertinoPageScaffold ` ] [ ] .
198
- - [ ` ElevatedButton ` ] [ ] replaced by [ ` CupertinoButton ` ] [ ] .
199
-
200
- This way, the example follows the current iOS design language.
201
-
202
- ::: secondary
203
- You don't need to replace all Material widgets with Cupertino versions
204
- to use [ ` CupertinoPageRoute ` ] [ ]
205
- since Flutter allows you to mix and match Material and Cupertino widgets
206
- depending on your needs.
207
- :::
265
+ {% tab "iOS" %}
208
266
209
267
<? code-excerpt "lib/main_cupertino.dart"?>
210
268
``` dartpad title="Flutter Cupertino theme hands-on example in DartPad" run="true"
@@ -260,6 +318,10 @@ class SecondRoute extends StatelessWidget {
260
318
<img src =" /assets/images/docs/cookbook/navigation-basics-cupertino.gif " alt =" Navigation Basics Cupertino Demo " class =" site-mobile-screenshot " />
261
319
</noscript >
262
320
321
+ {% endtab %}
322
+
323
+ {% endtabs %}
324
+
263
325
## Additional navigation methods
264
326
265
327
The recipe in this topic shows you one way to navigate to a new screen and
0 commit comments