text stringlengths 1 474 |
|---|
@override |
void dispose() { |
// Clean up the controller when the widget is disposed. |
myController.dispose(); |
super.dispose(); |
} |
@override |
Widget build(BuildContext context) { |
return Scaffold( |
appBar: AppBar( |
title: const Text('Retrieve Text Input'), |
), |
body: Padding( |
padding: const EdgeInsets.all(16), |
child: TextField( |
controller: myController, |
), |
), |
floatingActionButton: FloatingActionButton( |
// When the user presses the button, show an alert dialog containing |
// the text that the user has entered into the text field. |
onPressed: () { |
showDialog( |
context: context, |
builder: (context) { |
return AlertDialog( |
// Retrieve the text the that user has entered by using the |
// TextEditingController. |
content: Text(myController.text), |
); |
}, |
); |
}, |
tooltip: 'Show me the value!', |
child: const Icon(Icons.text_fields), |
), |
); |
} |
}<code_end> |
<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 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.