從螢幕返回資料
在某些情況下,你可能希望從新螢幕返回資料。例如,假設你推送了一個新螢幕,它向用戶顯示兩個選項。當用戶輕觸一個選項時,你希望將使用者的選擇告知第一個螢幕,以便它能夠根據這些資訊採取行動。
你可以使用 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.'),
),
),
],
),
),
);
}
}