text
stringlengths
1
372
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]),
),
);
},
);
},
),
<code_end>
<topic_end>
<topic_start>
interactive example
<code_start>
import 'package:flutter/material.dart';
class todo {
final string title;
final string description;
const todo(this.title, this.description);
}
void main() {
runApp(
MaterialApp(
title: 'passing data',
home: TodosScreen(
todos: list.generate(
20,
(i) => todo(
'todo $i',
'a description of what needs to be done for todo $i',
),
),
),
),
);
}
class TodosScreen extends StatelessWidget {
const TodosScreen({super.key, required this.todos});
final List<Todo> todos;
@override
widget build(BuildContext context) {
return scaffold(
appBar: AppBar(
title: const Text('Todos'),
),
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]),
),
);
},
);
},