Challenge - 5 Problems
TextFormField Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What is the output of this Flutter code snippet?
Consider this Flutter widget code using
TextFormField. What will the UI show when the app runs?Flutter
TextFormField(
decoration: InputDecoration(
labelText: 'Enter your name',
border: OutlineInputBorder(),
),
initialValue: 'John',
enabled: false,
)Attempts:
2 left
💡 Hint
Check the
enabled and initialValue properties.✗ Incorrect
The
enabled: false disables input, so the field is read-only. The initialValue sets the text shown. The label and border appear as specified.❓ lifecycle
intermediate2:00remaining
What happens when you call
_formKey.currentState!.validate() in a Flutter form?Given a Flutter form with a
TextFormField that has a validator, what does calling _formKey.currentState!.validate() do?Attempts:
2 left
💡 Hint
Think about what validation means in forms.
✗ Incorrect
Calling
validate() runs each field's validator function and returns true only if all validators return null (no error).📝 Syntax
advanced2:00remaining
What error does this Flutter code cause?
Analyze this Flutter code snippet using
TextFormField. What error will it produce?Flutter
TextFormField(
validator: (value) {
if (value!.isEmpty) {
return 'Please enter text';
}
return null;
},
)Attempts:
2 left
💡 Hint
What does a validator return when input is valid?
✗ Incorrect
If the validator returns null, it means input is valid. The code returns a string only when empty, otherwise returns null implicitly.
advanced
2:00remaining
How to focus the next
TextFormField after pressing 'Next' on the keyboard?In Flutter, you want to move focus from one
TextFormField to the next when the user presses the 'Next' button on the keyboard. Which approach works best?Attempts:
2 left
💡 Hint
Think about how Flutter manages keyboard focus.
✗ Incorrect
Calling
FocusScope.of(context).nextFocus() moves the focus to the next focusable widget, which is the next text field.🔧 Debug
expert3:00remaining
Why does this
TextFormField not update its text when typing?This Flutter code uses a
TextFormField with a TextEditingController. The user types, but the text does not update on screen. Why?Flutter
final controller = TextEditingController(text: 'Hello'); TextFormField( controller: controller, onChanged: (value) { controller.text = value; }, )
Attempts:
2 left
💡 Hint
Think about what happens when you set controller.text inside onChanged.
✗ Incorrect
Setting controller.text inside onChanged resets the text and cursor position, causing input to not update as expected.