颤动警报对话框,用于更新值

时间:2019-09-24 06:45:28

标签: flutter

我想在警报对话框中更新值,我正在使用以下内容:

Future showAlert() async {
    await showDialog(
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
            title: Text('Alert in Dialog'),
            content: Container(
              height: 40.0,
              width: 60.0,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  _itemCount != 1
                      ? new IconButton(
                          icon: new Icon(Icons.remove),
                          onPressed: () => setState(() => _itemCount--),
                        )
                      : new Container(),
                  new Text(_itemCount.toString()),
                  new IconButton(
                      icon: new Icon(Icons.add),
                      onPressed: () => setState(() => _itemCount++))
                ],
              ),
            ),
            actions: <Widget>[
              new FlatButton(
                child: new Text('CANCEL'),
                onPressed: () {
                  Navigator.of(context).pop();
                },
              )
            ],
          );
        });
  }

我在Listview Builder中调用此函数

  GestureDetector(
                                  child: Align(
                                    alignment: Alignment.topRight,
                                    child: Padding(
                                      padding: const EdgeInsets.all(5.0),
                                      child: Icon(Icons.add_box,
                                          color: Color(0xFFfccd01)),
                                    ),
                                  ),
                                  onTap: () {
                                    showAlert();
                                  /*  FutureBuilder<String>(
                                        future: Add_to_cart(USER_ID,
                                            snapshot.data[index].id, "1"),
                                        builder: (context, snapshot) {
                                          print(snapshot.data);
                                        });*/
                                  },
                                ),

但是在+单击关闭并重新打开后,我的警报对话框的值将不会更新,它将更新,但是我想在点击图标按钮时进行更新。

2 个答案:

答案 0 :(得分:0)

您必须将AlertDialog插入Statefulll小部件

请参见以下示例:

class ShowDialog extends StatefulWidget {
  @override
  _ShowDialogState createState() => _ShowDialogState();
}

class _ShowDialogState extends State<ShowDialog> {
  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      //your container
    );
  }
}

并将ShowDialog调用到showDialog()

答案 1 :(得分:0)

您可以使用StreamBuilder在Dialog或该屏幕上通过流在单个事件上进行更新