TextField(
controller: _controller,
decoration: InputDecoration(
hintText: "Enter a message",
suffixIcon: IconButton(
onPressed: () => _controller.clear(),
icon: Icon(Icons.clear),
),
),
)
final TextEditingController _textController = new TextEditingController();
TextField(
controller: _textController,
decoration: InputDecoration(
hintText: "Username",
suffixIcon: IconButton(
onPressed: () {
setState(() {
_textController.clear();
});
},
)),
)
//To clear a textField, use a TextEditing Controller
final _controller = TextEditingController();
Column(
children: [
TextField(
decoration: InputDecoration(labelText: 'Title'),
controller: _controller,
onChanged: (value) {
titleInput = value;
}),
ElevatedButton(
onPressed: () => _controller.clear(),
child: Text('Add Transaction!'),
),
],
),
//Initialize a controller inside your State class
TextEditingController _controller = TextEditingController();
//Set the _controller on you TextField
TextField(
controller: _controller,
//Rest of your code
)
//Clear the controller after pushing the new page
onPressed: () {
Navigator.push(context, new MaterialPageRoute(
builder: (context) => SearchPage(searchText: this.search.text)
)).then((value) {
//This makes sure the textfield is cleared after page is pushed.
_controller.clear();
});
}
The issue is that you are persisting objects inside a StatelessWidget.
Opening the keyboard tends to rebuild the whole screen (which is fine). But in
my case rebuilding cause your TextEditingController to be GCed because i
stored them inside a StatelessWidget