Published on

flutter AlertDialog 状态刷新

Authors
  showDialog(
        context: context,
        builder: (context) {
          bool warnningVisable = false;

          return StatefulBuilder(builder: (context, setState) {
            //AlerDialog需要套在StatefulBuilder里面才能更新状态
            return AlertDialog(
                title: Text('Enter Coin Symbol'),
                content: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Visibility(
                      maintainAnimation: true,
                      maintainSize: true,
                      maintainState: true,
                      visible: warnningVisable,
                      child: SizedBox(
                        child: ListTile(
                          horizontalTitleGap: 1.0,
                          contentPadding: EdgeInsets.all(1.0),
                          leading: Icon(
                            Icons.warning,
                            color: Colors.red[400],
                          ),
                          titleTextStyle:
                              Theme.of(context).textTheme.titleMedium,
                          title: Text("Alreadly has this Coin"),
                        ),
                      ),
                    ),
                    SizedBox(
                      height: 100,
                      width: 400,
                      child: TextField(
                        style: Theme.of(context).textTheme.headlineLarge,
                        textAlign: TextAlign.center,
                        controller: _cryptoItemTextEdit,
                      ),
                    ),
                    // )
                  ],
                ),
                actionsAlignment: MainAxisAlignment.spaceBetween,
                actions: <Widget>[
                  MaterialButton(
                      child: Icon(Icons.cancel),
                      onPressed: () {
                        Navigator.of(context).pop();
                        setState(() {
                          warnningVisable = false;
                        });
                      }),
                  ElevatedButton(
                      child: Text("Add"),
                      onPressed: () {
                        var s = _cryptoItemTextEdit.text.toUpperCase();

                        if (!_items.contains(s)) {
                          debugPrint("$_items");
                          setState(() {
                            _items.add(s);
                          });
                          _saveCryptoItems();
                          Navigator.of(context).pop();
                        } else {
                          debugPrint("$warnningVisable");
                          setState(() {
                            warnningVisable = true;
                          });
                        }
                      })
                ]);
          });
        });