User Input in Flutter
30 March, 2023
2
2
0
Contributors
User inputs
In Flutter, you can use TextEditingController
objects to get and set the text of a TextField
widget. You can also use them to control the behaviour of the text field, such as clearing the text or moving the cursor.
To use a TextEditingController
, you first need to create one and assign it to a TextField
widget. Here's an example:
final TextEditingController _controller = TextEditingController();
TextField(
controller: _controller,
)
You can then use the **TextEditingController
**to get and set the text of the TextField
:
String text = _controller.text; // Get the text.
_controller.text = 'New text'; // Set the text.
You can also use the **TextEditingController
**to control the behaviour of the TextField
, such as clearing the text or moving the cursor:
_controller.clear(); // Clear the text.
_controller.selection = TextSelection.fromPosition(TextPosition(offset: 5)); // Move the cursor.
By using a TextEditingController
, you can easily get and set the text of a TextField
widget and control its behaviour.
More on forms and user inputs: