use*_*684 1 widget dart flutter flutter-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);
});
}
});
Run Code Online (Sandbox Code Playgroud)
所以它看起来像
IconButton(icon: Icon(Icons.add), onPressed: () {
AddButtonAction().then((value) {
print(value);
if(value != null && value is item){
setState(() {
cart.add(value);
});
}
Run Code Online (Sandbox Code Playgroud)
我尝试过像 Future 这样的返回,但是在执行时得到了 null,该函数在我保存表单之前返回,可能是因为返回 AlertDialog
gen*_*ser 10
对于 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);
})
],
),
);
}
Run Code Online (Sandbox Code Playgroud)
使用await或then来得到结果:
result = await catLoversDialog();
Run Code Online (Sandbox Code Playgroud)
该Future说的ShowDialog返回包含你给什么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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1067 次 |
| 最近记录: |