1

I have a container that's wrapped within a gesture detector and within the container two text-fields which on clicked will show the time selector and date selector widgets consecutively. I added a clear button next to the date field and on tap it clears both the text-fields. The problem is on clicking the clear button it does not disappear even after both the text-fields have been cleared.

GestureDetector(
                onTap: () async {
                  await _pickDateTime();
                },
                child: Container(
                    margin: const EdgeInsets.only(top: 20.0),
                    width: double.infinity,
                    padding: const EdgeInsets.symmetric(
                        horizontal: 24.0, vertical: 15.0),
                    decoration: BoxDecoration(
                        color: tertiaryColor,
                        borderRadius: BorderRadius.circular(14.0)),
                    child: Column(
                      children: [
                        Row(
                          children: [
                            Flexible(
                              child: TextField(
                                enabled: false,
                                controller: _dateController,
                                onChanged: (String val) {
                                  _setDate = val;
                                },
                                decoration: InputDecoration(
                                    hintText: "Date",
                                    hintStyle: hintTextStyle,
                                    border: InputBorder.none),
                                style: todoScreenStyle,
                              ),
                            ),
                            _dateController.text.isNotEmpty &&
                                    _timeController.text.isNotEmpty
                                ? IconButton(
                                    onPressed: () {
                                      _dateController.clear();
                                      _timeController.clear();
                                    },
                                    icon: Icon(
                                      Icons.close,
                                      color: Colors.white,
                                    ))
                                : Container()
                          ],
                        ),
                        dividerStyle,
                        TextField(
                          onChanged: (String val) {
                            _setTime = val;
                          },
                          enabled: false,
                          controller: _timeController,
                          decoration: InputDecoration(
                              hintText: "Time",
                              hintStyle: hintTextStyle,
                              border: InputBorder.none),
                          style: todoScreenStyle,
                        )
                      ],
                    )),
              ),

I tried using setState but that did not work, also suffix Icon cannot be used as the whole container is wrapper within a gesture detector

Full Code on Github

For those who stumble upon this question: the answer is here: clear-button-not-changing-state

Rohith Nambiar
  • 2,957
  • 4
  • 17
  • 37

1 Answers1

1
@override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: Text("Test")),
        body: Column(
          children: [
            TextField(
              controller: textEditingController1,
              onChanged: (value) {
                 setState(() {});
              },
            ),
            TextField(
              controller: textEditingController2,
 onChanged: (value) {
                 setState(() {});
              },
            ),
            if (textEditingController1.text.isNotEmpty ||
                textEditingController2.text.isNotEmpty)
              {
                ElevatedButton(
                    onPressed: () {
                      textEditingController1.clear();
                      textEditingController2.clear();
                      setState(() {});
                    },
                    child: Text("Tap"))
              }.single
          ],
        ));
Fugipe
  • 386
  • 1
  • 8