text
stringlengths
1
474
),
body: Center(
child: ElevatedButton(
onPressed: () {
Navigator.pop(context);
},
child: const Text('Go back!'),
),
),
);
}
}<code_end>
<topic_end>
<topic_start>
Navigation with CupertinoPageRoute
In the previous example you learned how to navigate between screens
using the MaterialPageRoute from Material Components.
However, in Flutter you are not limited to Material design language,
instead, you also have access to Cupertino (iOS-style) widgets.Implementing navigation with Cupertino widgets follows the same steps
as when using MaterialPageRoute,
but instead you use CupertinoPageRoute
which provides an iOS-style transition animation.In the following example, these widgets have been replaced:This way, the example follows the current iOS design language.You don’t need to replace all Material widgets with Cupertino versions
to use CupertinoPageRoute
since Flutter allows you to mix and match Material and Cupertino widgets
depending on your needs.
<code_start>import 'package:flutter/cupertino.dart';
void main() {
runApp(const CupertinoApp(
title: 'Navigation Basics',
home: FirstRoute(),
));
}
class FirstRoute extends StatelessWidget {
const FirstRoute({super.key});
@override
Widget build(BuildContext context) {
return CupertinoPageScaffold(
navigationBar: const CupertinoNavigationBar(
middle: Text('First Route'),
),
child: Center(
child: CupertinoButton(
child: const Text('Open route'),
onPressed: () {
Navigator.push(
context,
CupertinoPageRoute(builder: (context) => const SecondRoute()),
);
},
),
),
);
}
}
class SecondRoute extends StatelessWidget {
const SecondRoute({super.key});
@override
Widget build(BuildContext context) {
return CupertinoPageScaffold(
navigationBar: const CupertinoNavigationBar(
middle: Text('Second Route'),
),
child: Center(
child: CupertinoButton(
onPressed: () {
Navigator.pop(context);
},
child: const Text('Go back!'),
),
),
);
}
}<code_end>
<topic_end>
<topic_start>Send data to a new screen
Often, you not only want to navigate to a new screen,
but also pass data to the screen as well.
For example, you might want to pass information about
the item that’s been tapped.Remember: Screens are just widgets.
In this example, create a list of todos.
When a todo is tapped, navigate to a new screen (widget) that
displays information about the todo.
This recipe uses the following steps:<topic_end>
<topic_start>
1. Define a todo class
First, you need a simple way to represent todos. For this example,
create a class that contains two pieces of data: the title and description.
<code_start>class Todo {
final String title;
final String description;
const Todo(this.title, this.description);
}<code_end>
<topic_end>
<topic_start>
2. Create a list of todos
Second, display a list of todos. In this example, generate
20 todos and show them using a ListView.
For more information on working with lists,
see the Use lists recipe.<topic_end>
<topic_start>