向新屏幕发送数据
如何向新路由传递数据。
通常,你不仅需要导航到新屏幕,还需要将数据传递给该屏幕。例如,你可能需要传递关于被点击条目的信息。
请记住:屏幕只是 Widget。在此示例中,创建一个待办事项列表。当点击某个待办事项时,导航到一个显示该待办事项信息的新屏幕(Widget)。本方案包含以下步骤:
- 定义一个待办事项类。
- 显示待办事项列表。
- 创建一个能够显示待办事项信息的详情屏幕。
- 导航并向详情屏幕传递数据。
1. 定义一个待办事项(todo)类
#首先,你需要一种简单的方式来表示待办事项。对于此示例,创建一个包含两部分数据的类:标题和描述。
class Todo {
final String title;
final String description;
const Todo(this.title, this.description);
}
2. 创建待办事项列表
#其次,显示待办事项列表。在此示例中,生成 20 个待办事项并使用 ListView 显示它们。有关处理列表的更多信息,请参阅使用列表方案。
生成待办事项列表
#final todos = List.generate(
20,
(i) => Todo(
'Todo $i',
'A description of what needs to be done for Todo $i',
),
);
使用 ListView 显示待办事项列表
#ListView.builder(
itemCount: todos.length,
itemBuilder: (context, index) {
return ListTile(title: Text(todos[index].title));
},
)
到目前为止,一切顺利。这生成了 20 个待办事项并将它们显示在 ListView 中。
3. 创建一个用于显示列表的待办事项屏幕
#为此,我们创建一个 StatelessWidget,将其命名为 TodosScreen。由于此页面的内容在运行时不会改变,我们需要在这个 Widget 的作用域内要求传入待办事项列表。
我们将 ListView.builder 作为我们返回给 build() 方法的 Widget 的 body 传入。这会将列表渲染到屏幕上,让你开始工作!
class TodosScreen extends StatelessWidget {
// Requiring the list of todos.
const TodosScreen({super.key, required this.todos});
final List<Todo> todos;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Todos')),
//passing in the ListView.builder
body: ListView.builder(
itemCount: todos.length,
itemBuilder: (context, index) {
return ListTile(title: Text(todos[index].title));
},
),
);
}
}
有了 Flutter 的默认样式,你无需为以后想做的事情而操心,直接使用即可!
4. 创建一个用于显示待办事项详细信息的详情屏幕
#现在,创建第二个屏幕。屏幕标题包含待办事项的标题,屏幕主体显示其描述。
由于详情屏幕是一个普通的 StatelessWidget,要求用户在 UI 中传入一个 Todo。然后,使用给定的待办事项构建 UI。
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),
),
);
}
}
5. 导航并向详情屏幕传递数据
#有了 DetailScreen,你就可以执行导航了。在此示例中,当用户点击列表中的待办事项时,导航到 DetailScreen。将该待办事项传递给 DetailScreen。
为了在 TodosScreen 中捕获用户的点击,为 ListTile Widget 编写一个 onTap() 回调。在 onTap() 回调内,使用 Navigator.push() 方法。
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<void>(
builder: (context) => DetailScreen(todo: todos[index]),
),
);
},
);
},
),
互动示例
#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<void>(
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),
),
);
}
}
或者,使用 RouteSettings 传递参数
#重复前两个步骤。
创建详情屏幕以提取参数
#接下来,创建一个从 Todo 中提取并显示标题和描述的详情屏幕。要访问 Todo,请使用 ModalRoute.of() 方法。此方法返回带有参数的当前路由。
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),
),
);
}
}
导航并将参数传递给详情屏幕
#最后,当用户点击 ListTile Widget 时,使用 Navigator.push() 导航到 DetailScreen。将参数作为 RouteSettings 的一部分进行传递。DetailScreen 将提取这些参数。
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<void>(
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]),
),
);
},
);
},
)
完整示例
#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<void>(
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),
),
);
}
}