Flutter - 从 AlertDialog 返回内容
Flutter - returning content from AlertDialog
代码的想法是,当用户按下添加时,他可以键入条形码或简单地退出警报屏幕。验证条形码后,会从该条形码生成一个对象,并将其添加到实际的市场购物车中。此代码已经有效,但我正试图找到一种方法以某种方式将它隔离为一个函数。
IconButton(icon: Icon(Icons.add), onPressed: () {
TextEditingController barcodeController = TextEditingController();
final _formBarcode = GlobalKey<FormState>();
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
content: Stack(
overflow: Overflow.visible,
children: <Widget>[
Form(
key: _formBarcode,
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Padding(
padding: EdgeInsets.all(2.0),
child: TextFormField(
validator: (String value) {
if (BarcodeController.text.isEmpty) {
return "please enter the product barcode";
}
return null;
},
onSaved: (String value) {
},
controller: barcodeController,
style: TextStyle(
color: Colors.black,
fontSize: 10.0,
fontWeight: FontWeight.w700,
),
decoration: InputDecoration(
labelText: "barcode:",
labelStyle: new TextStyle(
fontSize: 12,
),
suffixIcon: IconButton(icon: Icon(Icons.camera_alt), onPressed: () async {}),
),
),
),
Padding(
padding: EdgeInsets.all(12),
),
Padding(
padding: const EdgeInsets.all(2.0),
child: RaisedButton(
color: Colors.black,
child: Text(
"Confirmar",
style: TextStyle(color: Colors.white),
),
onPressed: () {
if (_formBarcode.currentState.validate()) {
_formBarcode.currentState.save();
Navigator.pop(context, item("11111", BarcodeController.text));
}
},
),
)
],
),
),
],
),
);
})
.then((value) {
print(value);
if(value != null && value is item){
setState(() {
cart.add(value);
});
}
});
看起来像
IconButton(icon: Icon(Icons.add), onPressed: () {
AddButtonAction().then((value) {
print(value);
if(value != null && value is item){
setState(() {
cart.add(value);
});
}
我已经尝试过 returns 像 Future 但是当它执行时得到了 null,函数 returned 在我保存表单之前,可能是因为 return AlertDialog
showDialog returns 的 Future
包含您给 Navigator.pop()
的内容
var result = await showDialog(
//Your Dialog Code that does Navigator.pop(context, result) when necessary
);
print(result); // this is the result that the dialog sent over
对于 bolian 结果,您可以转义使用的对话框 Navigator.pop(context, true)
:
Future<bool> catLoversDialog() async {
return await showDialog(
context: context,
builder: (context) => AlertDialog(
content: Text("Do you love cats?"),
actions: [
TextButton(
child: Text("no", style: TextStyle(color: Colors.grey)),
onPressed: () {
Navigator.pop(context, false);
}),
TextButton(
child: Text("yes!", style: TextStyle(color: Colors.blue)),
onPressed: () {
Navigator.pop(context, true);
})
],
),
);
}
使用await
或then
得到结果:
result = await catLoversDialog();
代码的想法是,当用户按下添加时,他可以键入条形码或简单地退出警报屏幕。验证条形码后,会从该条形码生成一个对象,并将其添加到实际的市场购物车中。此代码已经有效,但我正试图找到一种方法以某种方式将它隔离为一个函数。
IconButton(icon: Icon(Icons.add), onPressed: () {
TextEditingController barcodeController = TextEditingController();
final _formBarcode = GlobalKey<FormState>();
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
content: Stack(
overflow: Overflow.visible,
children: <Widget>[
Form(
key: _formBarcode,
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Padding(
padding: EdgeInsets.all(2.0),
child: TextFormField(
validator: (String value) {
if (BarcodeController.text.isEmpty) {
return "please enter the product barcode";
}
return null;
},
onSaved: (String value) {
},
controller: barcodeController,
style: TextStyle(
color: Colors.black,
fontSize: 10.0,
fontWeight: FontWeight.w700,
),
decoration: InputDecoration(
labelText: "barcode:",
labelStyle: new TextStyle(
fontSize: 12,
),
suffixIcon: IconButton(icon: Icon(Icons.camera_alt), onPressed: () async {}),
),
),
),
Padding(
padding: EdgeInsets.all(12),
),
Padding(
padding: const EdgeInsets.all(2.0),
child: RaisedButton(
color: Colors.black,
child: Text(
"Confirmar",
style: TextStyle(color: Colors.white),
),
onPressed: () {
if (_formBarcode.currentState.validate()) {
_formBarcode.currentState.save();
Navigator.pop(context, item("11111", BarcodeController.text));
}
},
),
)
],
),
),
],
),
);
})
.then((value) {
print(value);
if(value != null && value is item){
setState(() {
cart.add(value);
});
}
});
看起来像
IconButton(icon: Icon(Icons.add), onPressed: () {
AddButtonAction().then((value) {
print(value);
if(value != null && value is item){
setState(() {
cart.add(value);
});
}
我已经尝试过 returns 像 Future 但是当它执行时得到了 null,函数 returned 在我保存表单之前,可能是因为 return AlertDialog
showDialog returns 的 Future
包含您给 Navigator.pop()
var result = await showDialog(
//Your Dialog Code that does Navigator.pop(context, result) when necessary
);
print(result); // this is the result that the dialog sent over
对于 bolian 结果,您可以转义使用的对话框 Navigator.pop(context, true)
:
Future<bool> catLoversDialog() async {
return await showDialog(
context: context,
builder: (context) => AlertDialog(
content: Text("Do you love cats?"),
actions: [
TextButton(
child: Text("no", style: TextStyle(color: Colors.grey)),
onPressed: () {
Navigator.pop(context, false);
}),
TextButton(
child: Text("yes!", style: TextStyle(color: Colors.blue)),
onPressed: () {
Navigator.pop(context, true);
})
],
),
);
}
使用await
或then
得到结果:
result = await catLoversDialog();