text stringlengths 1 474 |
|---|
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. |
final Todo todo; |
@override |
Widget build(BuildContext context) { |
// Use the Todo to create the UI. |
return Scaffold( |
appBar: AppBar( |
title: Text(todo.title), |
), |
body: Padding( |
padding: const EdgeInsets.all(16), |
child: Text(todo.description), |
), |
); |
} |
}<code_end> |
<topic_end> |
<topic_start> |
5. Navigate and pass data to the detail screen |
With a DetailScreen in place, |
you’re ready to perform the Navigation. |
In this example, navigate to the DetailScreen when a user |
taps a todo in the list. Pass the todo to the DetailScreen.To capture the user’s tap in the TodosScreen, write an onTap() |
callback for the ListTile widget. Within the onTap() callback, |
use the Navigator.push() method. |
<code_start>body: ListView.builder( |
itemCount: todos.length, |
itemBuilder: (context, index) { |
return ListTile( |
title: Text(todos[index].title), |
// When a user taps the ListTile, navigate to the DetailScreen. |
// Notice that you're not only creating a DetailScreen, you're |
// also passing the current todo through to it. |
onTap: () { |
Navigator.push( |
context, |
MaterialPageRoute( |
builder: (context) => DetailScreen(todo: todos[index]), |
), |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.