text
stringlengths
1
372
),
);
}
}
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),
),
);
}
}
<code_end>
<topic_end>
<topic_start>
navigate and pass the arguments to the detail screen
finally, navigate to the DetailScreen when a user taps
a ListTile widget using navigator.push().
pass the arguments as part of the RouteSettings.
the DetailScreen extracts these arguments.
<code_start>
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],
),
),
);
},
);
},
)
<code_end>
<topic_end>
<topic_start>
complete 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(