Clear controller textfield flutter

Issue

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.

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

enter image description here

Solution

You need to use StatefulBuilder() to use setState() inside AlertBox(). Using StatefulBuilder() it build the UI of only your AlertBox(). If you don’t use StatefulBuilder() the UI of your AlertBox() wont update if you check your _controller.text==0 and change your Icon.
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

Answered By – Chirag Bargoojar

Answer Checked By – Gilberto Lyons (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *