What’s the usage of the textEditingController
provided to fieldViewBuilder
in the Autocomplete
widget? What can it be used for? Can it be used to modify or clear the content of the TextField
?
Autocomplete<String>(
fieldViewBuilder: (
BuildContext context,
TextEditingController textEditingController,
FocusNode focusNode,
VoidCallback onFieldSubmitted,
) {
return TextField(
controller: textEditingController,
focusNode: focusNode,
onChanged: (String value) {
print('The text has changed to: $value');
},
);
},
2
Answers
The
textEditingController
property provided to thefieldViewBuilder
in theAutocomplete
widget is used to control the text field. It can be used to modify or clear the content of the text field.In this example, the
TextField
widget has a controller property that is initialized with a string value. TheonChanged
callback of theTextField
widget is used to print the current value of the text field to the console. TheonEditingComplete
callback is used to clear the text field’s value when the user presses the Enter key.