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);
                  });
                }
              }); 

看起来像

IconButton(icon: Icon(Icons.add),onpressed: () {
             AddButtonAction().then((value) {
               print(value);
               if(value != null && value is item){
                 setState(() {
                   cart.add(value);
               });
             }

我尝试过像Future之类的返回,但是在返回时却为null,该函数在保存表单之前返回,可能是因为返回了AlertDialog

解决方法

对于 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)),true);
              })
        ],),);
  }

使用 awaitthen 得到结果:

result = await catLoversDialog();
,

showDialog返回的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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...