0
0
Fluttermobile~20 mins

TextFormField in Flutter - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
TextFormField Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2: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,
)
AAn enabled text field showing 'John' with no border.
BAn enabled empty text field with label 'Enter your name'.
CA disabled empty text field with no label.
DA disabled text field showing 'John' with a label 'Enter your name'.
Attempts:
2 left
💡 Hint
Check the enabled and initialValue properties.
lifecycle
intermediate
2: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?
AIt runs all validators and returns true if all fields are valid, false otherwise.
BIt clears all the text fields in the form.
CIt submits the form data to the server automatically.
DIt disables all text fields in the form.
Attempts:
2 left
💡 Hint
Think about what validation means in forms.
📝 Syntax
advanced
2: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;
  },
)
AMissing return statement in validator causes a warning but runs fine.
BThe code causes a syntax error due to missing else branch.
CThe validator returns null implicitly if input is not empty, so no error occurs.
DThe validator must always return a String, so this causes a runtime error.
Attempts:
2 left
💡 Hint
What does a validator return when input is valid?
navigation
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?
AUse <code>FocusScope.of(context).nextFocus()</code> inside <code>onFieldSubmitted</code> callback.
BCall <code>setState()</code> to rebuild the widget tree on submit.
CUse <code>TextEditingController.clear()</code> to clear the current field on submit.
DWrap the fields in a <code>GestureDetector</code> and detect taps to move focus.
Attempts:
2 left
💡 Hint
Think about how Flutter manages keyboard focus.
🔧 Debug
expert
3: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;
  },
)
AThe controller must be disposed inside onChanged to update text.
BSetting controller.text inside onChanged causes the cursor to jump and text not to update properly.
CThe initial text must be set inside onChanged, not in controller constructor.
DThe TextFormField needs a validator to update text.
Attempts:
2 left
💡 Hint
Think about what happens when you set controller.text inside onChanged.