Add a TextEditingController
to your TextField
and call controller.clear()
only after pushing the other page.
This can be done either by using await
inside the onPressed
function or you can use the .then()
callback if you want you avoid making your onPressed' function
async`.
Example -
//Initialize a controller inside your State class
TextEditingController _controller1 = TextEditingController();
TextEditingController _controller2 = TextEditingController();
//Set the _controller on your both TextFields
TextField(
controller: _controller1,
//Rest of your code
)
TextField(
controller: _controller2,
//Rest of your code
)
//Values Remain Same after pushing the new page with this
onPressed: () {
Navigator.push(context, new MaterialPageRoute(
builder: (context) => NextPage()
))
}
//Clear the controller after pushing the new page with this
onPressed: () {
Navigator.push(context, new MaterialPageRoute(
builder: (context) => NextPage()
)).then((value) {
//This makes sure the textfield is cleared after page is pushed.
_controller1.clear();
_controller2.clear();
});
}