从屏幕返回数据
如何从新页面返回数据。
在某些情况下,您可能需要从新页面返回数据。例如,假设您跳转到一个为用户提供两个选项的新页面。当用户点击某个选项时,您希望将用户的选择通知给第一个页面,以便其根据该信息执行后续操作。
您可以通过以下步骤使用 Navigator.pop() 方法来实现这一点:
- 定义主屏幕
- 添加一个跳转到选择屏幕的按钮
- 展示带有两个按钮的选择屏幕
- 点击按钮时,关闭选择屏幕
- 在主屏幕上显示包含选择结果的 Snackbar
1. 定义主屏幕
#主屏幕显示一个按钮。点击该按钮后,它会跳转到选择屏幕。
dart
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()),
);
}
}
2. 添加一个跳转到选择屏幕的按钮
#现在,创建 SelectionButton,它执行以下操作:
- 点击时跳转到 SelectionScreen。
- 等待 SelectionScreen 返回结果。
dart
class SelectionButton extends StatefulWidget {
const SelectionButton({super.key});
@override
State<SelectionButton> createState() => _SelectionButtonState();
}
class _SelectionButtonState extends State<SelectionButton> {
@override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: () {
_navigateAndDisplaySelection(context);
},
child: const Text('Pick an option, any option!'),
);
}
Future<void> _navigateAndDisplaySelection(BuildContext context) async {
// Navigator.push returns a Future that completes after calling
// Navigator.pop on the Selection Screen.
final result = await Navigator.push(
context,
// Create the SelectionScreen in the next step.
MaterialPageRoute<String>(builder: (context) => const SelectionScreen()),
);
}
}
3. 展示带有两个按钮的选择屏幕
#现在,构建一个包含两个按钮的选择屏幕。当用户点击按钮时,应用会关闭选择屏幕并通知主屏幕点击了哪个按钮。
此步骤定义 UI。下一步将添加返回数据的代码。
dart
class SelectionScreen extends StatelessWidget {
const SelectionScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Pick an option')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.all(8),
child: ElevatedButton(
onPressed: () {
// Pop here with "Yep"...
},
child: const Text('Yep!'),
),
),
Padding(
padding: const EdgeInsets.all(8),
child: ElevatedButton(
onPressed: () {
// Pop here with "Nope"...
},
child: const Text('Nope.'),
),
),
],
),
),
);
}
}
4. 点击按钮时,关闭选择屏幕
#现在,更新两个按钮的 onPressed() 回调。要将数据返回到第一个页面,请使用 Navigator.pop() 方法,该方法接受一个名为 result 的可选第二个参数。任何结果都会返回给 SelectionButton 中的 Future。
“是”按钮
#
dart
ElevatedButton(
onPressed: () {
// Close the screen and return "Yep!" as the result.
Navigator.pop(context, 'Yep!');
},
child: const Text('Yep!'),
)
“否”按钮
#
dart
ElevatedButton(
onPressed: () {
// Close the screen and return "Nope." as the result.
Navigator.pop(context, 'Nope.');
},
child: const Text('Nope.'),
)
5. 在主屏幕上显示包含选择结果的 Snackbar
#既然您已经跳转到选择屏幕并正在等待结果,接下来就需要处理返回的信息。
在本例中,请通过 SelectionButton 中的 _navigateAndDisplaySelection() 方法显示一个包含结果的 Snackbar。
dart
// A method that launches the SelectionScreen and awaits the result from
// Navigator.pop.
Future<void> _navigateAndDisplaySelection(BuildContext context) async {
// Navigator.push returns a Future that completes after calling
// Navigator.pop on the Selection Screen.
final result = await Navigator.push(
context,
MaterialPageRoute<String>(builder: (context) => const SelectionScreen()),
);
// When a BuildContext is used from a StatefulWidget, the mounted property
// must be checked after an asynchronous gap.
if (!context.mounted) return;
// After the Selection Screen returns a result, hide any previous snackbars
// and show the new result.
ScaffoldMessenger.of(context)
..removeCurrentSnackBar()
..showSnackBar(SnackBar(content: Text('$result')));
}
互动示例
#import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(title: 'Returning Data', home: HomeScreen()));
}
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Returning Data Demo')),
body: const Center(child: SelectionButton()),
);
}
}
class SelectionButton extends StatefulWidget {
const SelectionButton({super.key});
@override
State<SelectionButton> createState() => _SelectionButtonState();
}
class _SelectionButtonState extends State<SelectionButton> {
@override
Widget build(BuildContext context) {
return ElevatedButton(
onPressed: () {
_navigateAndDisplaySelection(context);
},
child: const Text('Pick an option, any option!'),
);
}
// A method that launches the SelectionScreen and awaits the result from
// Navigator.pop.
Future<void> _navigateAndDisplaySelection(BuildContext context) async {
// Navigator.push returns a Future that completes after calling
// Navigator.pop on the Selection Screen.
final result = await Navigator.push(
context,
MaterialPageRoute<String>(builder: (context) => const SelectionScreen()),
);
// When a BuildContext is used from a StatefulWidget, the mounted property
// must be checked after an asynchronous gap.
if (!context.mounted) return;
// After the Selection Screen returns a result, hide any previous snackbars
// and show the new result.
ScaffoldMessenger.of(context)
..removeCurrentSnackBar()
..showSnackBar(SnackBar(content: Text('$result')));
}
}
class SelectionScreen extends StatelessWidget {
const SelectionScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Pick an option')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8),
child: ElevatedButton(
onPressed: () {
// Close the screen and return "Yep!" as the result.
Navigator.pop(context, 'Yep!');
},
child: const Text('Yep!'),
),
),
Padding(
padding: const EdgeInsets.all(8),
child: ElevatedButton(
onPressed: () {
// Close the screen and return "Nope." as the result.
Navigator.pop(context, 'Nope.');
},
child: const Text('Nope.'),
),
),
],
),
),
);
}
}