【问题标题】:Flutter - returning content from AlertDialogFlutter - 从 AlertDialog 返回内容
【发布时间】:2020-12-09 04:26:15
【问题描述】:

代码的想法是当用户按下添加时,他可以键入条形码或简单地退出警报屏幕。验证条形码后,会从该条形码生成一个对象,并将其添加到实际的市场购物车中。这段代码已经可以工作了,但我正在尝试找到一种方法以某种方式将其隔离到一个函数中。

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

【问题讨论】:

    标签: flutter dart widget flutter-alertdialog


    【解决方案1】:

    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
    
    

    【讨论】:

      【解决方案2】:

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

      使用awaitthen 得到结果:

      result = await catLoversDialog();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-10
        • 2019-07-07
        相关资源
        最近更新 更多