text
stringlengths
1
372
<topic_end>
<topic_start>
handle changes to a text field
in some cases, it’s useful to run a callback function every time the text
in a text field changes. for example, you might want to build a search
screen with autocomplete functionality where you want to update the
results as the user types.
how do you run a callback function every time the text changes?
with flutter, you have two options:
<topic_end>
<topic_start>
1. supply an onChanged() callback to a TextField or a TextFormField
the simplest approach is to supply an onChanged() callback to a
TextField or a TextFormField.
whenever the text changes, the callback is invoked.
in this example, print the current value and length of the text field
to the console every time the text changes.
it’s important to use characters when dealing with user input,
as text may contain complex characters.
this ensures that every character is counted correctly
as they appear to the user.
<code_start>
TextField(
onChanged: (text) {
print('First text field: $text (${text.characters.length})');
},
),
<code_end>
<topic_end>
<topic_start>
2. use a TextEditingController
a more powerful, but more elaborate approach, is to supply a
TextEditingController as the controller
property of the TextField or a TextFormField.
to be notified when the text changes, listen to the controller
using the addListener() method using the following steps:
<topic_end>
<topic_start>
create a TextEditingController
create a TextEditingController:
<code_start>
// define a custom form widget.
class MyCustomForm extends StatefulWidget {
const MyCustomForm({super.key});
@override
State<MyCustomForm> createState() => _MyCustomFormState();
}
// define a corresponding state class.
// this class holds data related to the form.
class _MyCustomFormState extends State<MyCustomForm> {
// create a text controller. later, use it to retrieve the
// current value of the TextField.
final myController = TextEditingController();
@override
void dispose() {
// clean up the controller when the widget is removed from the
// widget tree.
myController.dispose();
super.dispose();
}
@override
widget build(BuildContext context) {
// fill this out in the next step.
}
}
<code_end>
info note
remember to dispose of the TextEditingController when it’s no
longer needed. this ensures that you discard any resources used
by the object.
<topic_end>
<topic_start>
connect the TextEditingController to a text field
supply the TextEditingController to either a TextField
or a TextFormField. once you wire these two classes together,
you can begin listening for changes to the text field.
<code_start>
TextField(
controller: myController,
),
<code_end>
<topic_end>
<topic_start>
create a function to print the latest value
you need a function to run every time the text changes.
create a method in the _MyCustomFormState class that prints
out the current value of the text field.
<code_start>
void _printLatestValue() {
final text = myController.text;
print('Second text field: $text (${text.characters.length})');
}
<code_end>
<topic_end>
<topic_start>
listen to the controller for changes
finally, listen to the TextEditingController and call the
_printLatestValue() method when the text changes. use the
addListener() method for this purpose.
begin listening for changes when the