Challenge - 5 Problems
TextField Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What happens when you type in this TextField?
Consider this Flutter widget code snippet. What will be the value of the TextEditingController's text after typing "hello" in the TextField?
Flutter
final controller = TextEditingController(); TextField( controller: controller, decoration: InputDecoration(labelText: 'Enter text'), );
Attempts:
2 left
💡 Hint
The controller keeps the current text inside the TextField.
✗ Incorrect
The TextEditingController is linked to the TextField. When you type, the controller updates its text property to match the input.
❓ lifecycle
intermediate1:30remaining
When should you dispose a TextEditingController?
In a StatefulWidget, when is the correct time to call dispose() on a TextEditingController?
Attempts:
2 left
💡 Hint
Think about cleaning up resources when the widget is removed.
✗ Incorrect
TextEditingController holds resources and listeners. To avoid memory leaks, dispose it in the widget's dispose() method.
📝 Syntax
advanced1:30remaining
What is the output of this code snippet?
What will be printed when this Flutter code runs and the TextField is empty, then the button is pressed?
Flutter
final controller = TextEditingController(); ElevatedButton( onPressed: () { print(controller.text.isEmpty ? 'Empty' : controller.text); }, child: Text('Check'), );
Attempts:
2 left
💡 Hint
Check what controller.text returns when no input is given.
✗ Incorrect
controller.text returns an empty string "" if no text is entered, so isEmpty is true and 'Empty' is printed.
🔧 Debug
advanced2:00remaining
Why does this TextField not update the controller text?
Given this code, why does typing in the TextField not change the controller's text?
Flutter
final controller = TextEditingController(text: 'Start'); TextField( onChanged: (value) { controller.text = value; }, controller: controller, );
Attempts:
2 left
💡 Hint
Think about what happens when you set controller.text inside onChanged.
✗ Incorrect
Setting controller.text inside onChanged triggers another change event, causing a loop that prevents normal updates.
🧠 Conceptual
expert2:30remaining
How to programmatically move cursor to end of text in TextField?
You want to set the cursor position at the end of the current text in a TextField controlled by a TextEditingController. Which code snippet achieves this?
Attempts:
2 left
💡 Hint
Look for how to set selection using TextSelection and TextPosition.
✗ Incorrect
To move the cursor, set controller.selection to a TextSelection at the desired offset. The other options are invalid methods or properties.