text
stringlengths 1
372
|
---|
call your navigation routes using their names. |
name each route in the class passed to the runApp() function. |
the following example uses app: |
<code_start> |
// defines the route name as a constant |
// so that it's reusable. |
const detailsPageRouteName = '/details'; |
class app extends StatelessWidget { |
const app({ |
super.key, |
}); |
@override |
widget build(BuildContext context) { |
return CupertinoApp( |
home: const HomePage(), |
// the [routes] property defines the available named routes |
// and the widgets to build when navigating to those routes. |
routes: { |
detailsPageRouteName: (context) => const DetailsPage(), |
}, |
); |
} |
} |
<code_end> |
the following sample generates a list of persons using |
mockPersons(). tapping a person pushes the person’s detail page |
to the navigator using pushNamed(). |
<code_start> |
ListView.builder( |
itemCount: mockPersons.length, |
itemBuilder: (context, index) { |
final person = mockPersons.elementAt(index); |
final age = '${person.age} years old'; |
return ListTile( |
title: text(person.name), |
subtitle: text(age), |
trailing: const icon( |
icons.arrow_forward_ios, |
), |
onTap: () { |
// when a [listtile] that represents a person is |
// tapped, push the detailsPageRouteName route |
// to the navigator and pass the person's instance |
// to the route. |
Navigator.of(context).pushNamed( |
detailsPageRouteName, |
arguments: person, |
); |
}, |
); |
}, |
), |
<code_end> |
define the DetailsPage widget that displays the details of |
each person. in flutter, you can pass arguments into the |
widget when navigating to the new route. |
extract the arguments using ModalRoute.of(): |
<code_start> |
class DetailsPage extends StatelessWidget { |
const DetailsPage({super.key}); |
@override |
widget build(BuildContext context) { |
// read the person instance from the arguments. |
final person person = ModalRoute.of( |
context, |
)?.settings.arguments as person; |
// extract the age. |
final age = '${person.age} years old'; |
return scaffold( |
// display name and age. |
body: column(children: [text(person.name), text(age)]), |
); |
} |
} |
<code_end> |
to create more advanced navigation and routing requirements, |
use a routing package such as go_router. |
to learn more, check out navigation and routing. |
<topic_end> |
<topic_start> |
manually pop back |
in SwiftUI, you use the dismiss environment value to pop-back to |
the previous screen. |
in flutter, use the pop() function of the navigator class: |
<code_start> |
TextButton( |
onPressed: () { |
// this code allows the |
// view to pop back to its presenter. |
navigator.of(context).pop(); |
}, |
child: const Text('Pop back'), |
), |
<code_end> |
<topic_end> |
<topic_start> |
navigating to another app |
in SwiftUI, you use the openURL environment variable to open a |
URL to another application. |
in flutter, use the url_launcher plugin. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.