text
stringlengths
1
372
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) => const DetailScreen(),
// pass the arguments as part of the RouteSettings. the
// DetailScreen reads the arguments from these settings.
settings: RouteSettings(
arguments: todos[index],
),
),
);
},
);
},
),
);
}
}
class DetailScreen extends StatelessWidget {
const DetailScreen({super.key});
@override
widget build(BuildContext context) {
final todo = ModalRoute.of(context)!.settings.arguments as todo;
// 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>
return data from a screen
in some cases, you might want to return data from a new screen.
for example, say you push a new screen that presents two options to a user.
when the user taps an option, you want to inform the first screen
of the user’s selection so that it can act on that information.
you can do this with the navigator.pop()
method using the following steps:
<topic_end>
<topic_start>
1. define the home screen
the home screen displays a button. when tapped,
it launches the selection screen.
<code_start>
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
@override
widget build(BuildContext context) {
return scaffold(
appBar: AppBar(
title: const Text('Returning data demo'),
),
// create the SelectionButton widget in the next step.
body: const center(
child: SelectionButton(),
),
);
}
}
<code_end>
<topic_end>
<topic_start>
2. add a button that launches the selection screen
now, create the SelectionButton, which does the following:
<code_start>