How to add clear button to TextField Widget

后端 未结 12 1445
南笙
南笙 2020-12-22 22:11

Is there a proper way to add a clear button to the TextField?

Just like this picture from Material design guidelines:

What I found is

12条回答
  •  臣服心动
    2020-12-22 22:51

    Output:

    Create a variable

    var _controller = TextEditingController();
    

    And your TextField:

    TextField(
      controller: _controller,
      decoration: InputDecoration(
        hintText: "Enter a message",
        suffixIcon: IconButton(
          onPressed: () => _controller.clear(),
          icon: Icon(Icons.clear),
        ),
      ),
    )
    

提交回复
热议问题