How onTap clear TextField in below example?
Note TextField is positioned inside trailing in ListView
trailing: FittedBox(
fit: BoxFit.fill,
child: SizedBox(
height:40, width:100,
child: TextField(
controller: TextEditingController(text: _docData[index].quantity.toString()),
decoration: InputDecoration(
suffix: InkWell(
onTap: () {
setState(() {
});
},
child: Icon(
Icons.clear,
),
)
),
),
),
),
3
Answers
When we call the function:
The whole widget rebuilds hence your
TextEditingController
is initialized again so the text inside is reset to default which is emptyTry this
Create a list of
TextEditingController
Then inside
ListView.builder
, createTextEditingController
one by one and add it to controllers list.Then assign to
TextField
and clear like this