简体   繁体   English

清除 controller 文本字段 flutter

[英]Clear controller textfield flutter

I'm facing something strange.我正面临着一些奇怪的事情。 I have textfield in my app that can be cleared without problem, and once cleared, the delete icon disappears.我的应用程序中有文本字段,可以毫无问题地清除,一旦清除,删除图标就会消失。 But, when i want to clear a textfield that is in a AlertDialog, the text is cleared, but the icon to delete stays on.但是,当我想清除 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'),
                ),
              ],
            ),
          );
        });
  }

在此处输入图像描述

You need to use StatefulBuilder() to use setState() inside AlertBox() .您需要使用StatefulBuilder() ) 在AlertBox()中使用setState() ) 。 Using StatefulBuilder() it build the UI of only your AlertBox() .使用StatefulBuilder()它只构建你的AlertBox()的 UI。 If you don't use StatefulBuilder() the UI of your AlertBox() wont update if you check your _controller.text==0 and change your Icon.如果您不使用StatefulBuilder()如果您检查_controller.text==0并更改您的图标,则AlertBox() ) 的 UI 不会更新。 You can use like this.你可以这样使用。

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

You can try here你可以在这里试试

That's because your TextField holds the Focus you need to unfocus that in order to get what you want.那是因为您的TextField拥有Focus ,您需要unfocus焦点才能获得所需的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM