Navigate and pass data to the detail screen
import "package:flutter/material.dart";
void main()
{
runApp(const MyApp());
}
class MyApp extends StatelessWidget
{
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: "Navigate and pass data to the detail screen",
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.grey,
),
home: TodosScreen(
todos: List.generate(
20,
(index) {
return Todo("Todo $index",
"A description of what needs to be done for Todo $index");
},
),
),
);
}
}
class Todo
{
final String title;
final String description;
const Todo(this.title, this.description);
}
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"),
centerTitle: true,
backgroundColor: Colors.grey,
),
body: ListView.builder(
itemCount: todos.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(todos[index].title),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) {
return DetailScreen(
todo: todos[index],
);
},
),
);
},
);
},
),
);
}
}
class DetailScreen extends StatelessWidget
{
const DetailScreen({super.key, required this.todo});
final Todo todo;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Detail Screen"),
centerTitle: true,
backgroundColor: Colors.grey,
),
body: Center(
child: Text(todo.description,
style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 30.0),),
),
);
}
}
Comments
Post a Comment