text stringlengths 1 474 |
|---|
); |
}, |
); |
}, |
),<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]), |
), |
); |
}, |
); |
}, |
), |
); |
} |
} |
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> |
Alternatively, pass the arguments using RouteSettings |
Repeat the first two steps.<topic_end> |
<topic_start> |
Create a detail screen to extract the arguments |
Next, create a detail screen that extracts and displays the title and description from the Todo. To access the Todo, use the ModalRoute.of() method. This method returns the current route with the arguments. |
<code_start>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), |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.