清除控制器文本字段颤动

Clear controller textfield flutter

我遇到了一些奇怪的事情。我的应用程序中有文本字段,可以毫无问题地清除,一旦清除,删除图标就会消失。 但是,当我想清除 AlertDialog 中的文本字段时,文本被清除,但要删除的图标仍然存在。

final TextEditingController _namespaceController = TextEditingController();

  void clearNamespaceController() {
    _namespaceController.clear();
    setState(() {});
  }
Widget _displayDialogForEdition(result, index, context) {
    return IconButton(
        icon: Icon(Icons.edit),
        onPressed: () {
          setState(() {
            _namespaceController.text = result[index].name;
          });

          showDialog<String>(
            context: context,
            builder: (BuildContext context) => AlertDialog(
              title: const Text("Modification d'une configuration"),
              content: Container(
                // padding: EdgeInsets.all(16),
                child: TextField(
                  controller: _namespaceController,
                  decoration: InputDecoration(
                      prefixIcon: Icon(Icons.search, color: Theme.of(context).primaryColor),
                      border: OutlineInputBorder(),
                      labelText: 'Exclure le Namespace',
                      suffixIcon: _namespaceController.text.length == 0
                          ? null
                          : IconButton(
                              icon: Icon(Icons.clear),
                              onPressed: clearNamespaceController,
                            ),
                      labelStyle: Theme.of(context).textTheme.headline6),
                ),
              ),
              actions: <Widget>[
                TextButton(
                  onPressed: () => Navigator.pop(context, 'Cancel'),
                  child: const Text('Cancel'),
                ),
                TextButton(
                  onPressed: () => Navigator.pop(context, 'OK'),
                  child: const Text('OK'),
                ),
              ],
            ),
          );
        });
  }

您需要使用StatefulBuilder()才能在AlertBox()中使用setState()。使用 StatefulBuilder() 它只构建你的 AlertBox() 的 UI。如果您不使用 StatefulBuilder(),您的 AlertBox() 的 UI 如果您检查 _controller.text==0 并更改图标,则不会更新。 你可以这样使用。

showDialog(
              context: context,
              builder: (ctx) {
                bool isTextClear = true;
                return StatefulBuilder(builder: (ctx, setState) {
                  return AlertDialog(
                    title: Text("Add name"),
                    content: Container(
                      child: TextField(
                        controller: _controller,
                        onChanged: (val) {
                          setState(
                            () {
                              if (_controller.text.isEmpty) {
                                isTextClear = true;
                              } else {
                                isTextClear = false;
                              }
                            },
                          );
                        },
                        decoration: InputDecoration(
                          labelText: "Name",
                          prefixIcon: Icon(Icons.search),
                          suffixIcon: isTextClear
                              ? null
                              : IconButton(
                                  onPressed: () {
                                    setState(() {
                                      isTextClear = true;
                                      _controller.clear();
                                    });
                                  },
                                  icon: Icon(
                                    Icons.clear,
                                  ),
                                ),
                        ),
                      ),
                    ),
                  );
                });
              },
            );

你可以试试here

那是因为您的 TextField 拥有 Focus 您需要 unfocus 才能获得您想要的东西。