text
stringlengths
1
372
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>
generate the list of todos
<code_start>
final todos = list.generate(
20,
(i) => todo(
'todo $i',
'a description of what needs to be done for todo $i',
),
);
<code_end>
<topic_end>
<topic_start>
display the list of todos using a ListView
<code_start>
ListView.builder(
itemCount: todos.length,
itemBuilder: (context, index) {
return ListTile(
title: text(todos[index].title),
);
},
),
<code_end>
so far, so good.
this generates 20 todos and displays them in a ListView.
<topic_end>
<topic_start>
3. create a todo screen to display the list
for this, we create a StatelessWidget. we call it TodosScreen.
since the contents of this page won’t change during runtime,
we’ll have to require the list
of todos within the scope of this widget.
we pass in our ListView.builder as body of the widget we’re returning to build().
this’ll render the list on to the screen for you to get going!
<code_start>
class TodosScreen extends StatelessWidget {
// requiring the list of todos.
const TodosScreen({super.key, required this.todos});
final List<Todo> todos;
@override
widget build(BuildContext context) {
return scaffold(
appBar: AppBar(
title: const Text('Todos'),
),
//passing in the ListView.builder
body: ListView.builder(
itemCount: todos.length,
itemBuilder: (context, index) {
return ListTile(
title: text(todos[index].title),
);
},
),
);
}
}
<code_end>
with flutter’s default styling, you’re good to go without sweating about
things that you’d like to do later on!
<topic_end>
<topic_start>
4. create a detail screen to display information about a todo
now, create the second screen. the title of the screen contains the
title of the todo, and the body of the screen shows the description.
since the detail screen is a normal StatelessWidget,
require the user to enter a todo in the UI.
then, build the UI using the given todo.
<code_start>
class DetailScreen extends StatelessWidget {
// in the constructor, require a todo.
const DetailScreen({super.key, required this.todo});
// declare a field that holds the todo.