text stringlengths 1 474 |
|---|
} |
}<code_end> |
For more information on input validation, see the |
Building a form with validation recipe. |
<topic_end> |
<topic_start>Retrieve the value of a text field |
In this recipe, |
learn how to retrieve the text a user has entered into a text field |
using the following steps:<topic_end> |
<topic_start> |
1. Create a TextEditingController |
To retrieve the text a user has entered into a text field, |
create a TextEditingController |
and supply it to a TextField or TextFormField.Important: Call dispose of the TextEditingController when |
you’ve finished using it. This ensures that you discard any resources |
used by the object. |
<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 the data related to the Form. |
class _MyCustomFormState extends State<MyCustomForm> { |
// Create a text controller and use it to retrieve the current value |
// of the TextField. |
final myController = TextEditingController(); |
@override |
void dispose() { |
// Clean up the controller when the widget is disposed. |
myController.dispose(); |
super.dispose(); |
} |
@override |
Widget build(BuildContext context) { |
// Fill this out in the next step. |
} |
}<code_end> |
<topic_end> |
<topic_start> |
2. Supply the TextEditingController to a TextField |
Now that you have a TextEditingController, wire it up |
to a text field using the controller property: |
<code_start>return TextField( |
controller: myController, |
);<code_end> |
<topic_end> |
<topic_start> |
3. Display the current value of the text field |
After supplying the TextEditingController to the text field, |
begin reading values. Use the text() |
method provided by the TextEditingController to retrieve the |
String that the user has entered into the text field.The following code displays an alert dialog with the current |
value of the text field when the user taps a floating action button. |
<code_start>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 that the 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> |
Interactive example |
<code_start>import 'package:flutter/material.dart'; |
void main() => runApp(const MyApp()); |
class MyApp extends StatelessWidget { |
const MyApp({super.key}); |
@override |
Widget build(BuildContext context) { |
return const MaterialApp( |
title: 'Retrieve Text Input', |
home: MyCustomForm(), |
); |
} |
} |
// 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 the data related to the Form. |
class _MyCustomFormState extends State<MyCustomForm> { |
// Create a text controller and use it to retrieve the current value |
// of the TextField. |
final myController = TextEditingController(); |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.