Input widgets let users type or select information. This data helps apps know what the user wants or needs.
Why input widgets capture user data in Flutter
Introduction
Syntax
Flutter
TextField(
onChanged: (value) {
// Use the input value here
},
decoration: InputDecoration(
labelText: 'Enter text',
),
)The TextField widget is the basic input widget in Flutter.
The onChanged callback captures what the user types.
Examples
Flutter
TextField(
onChanged: (text) {
print('User typed: $text');
},
)Flutter
TextField(
decoration: InputDecoration(
labelText: 'Email',
hintText: 'example@mail.com',
),
)Flutter
TextField( obscureText: true, decoration: InputDecoration( labelText: 'Password', ), )
Sample App
This app shows a text field where the user can type. Below it, the app shows what the user typed in real time.
Flutter
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar(title: Text('Input Example')), body: InputExample(), ), ); } } class InputExample extends StatefulWidget { @override State<InputExample> createState() => _InputExampleState(); } class _InputExampleState extends State<InputExample> { String userInput = ''; @override Widget build(BuildContext context) { return Padding( padding: EdgeInsets.all(16), child: Column( children: [ TextField( decoration: InputDecoration(labelText: 'Type something'), onChanged: (text) { setState(() { userInput = text; }); }, ), SizedBox(height: 20), Text('You typed: $userInput'), ], ), ); } }
Important Notes
Input widgets are interactive and need to update the app state to show user data.
Use setState() in StatefulWidgets to refresh UI when input changes.
Always provide labels and hints for better user experience and accessibility.
Summary
Input widgets let users enter data that apps can use.
Use TextField with onChanged to capture input.
Show user input on screen by updating the app state.
Practice
1. Why do input widgets like
TextField capture user data in a Flutter app?easy
Solution
Step 1: Understand the purpose of input widgets
Input widgets likeTextFieldallow users to type text into the app.Step 2: Recognize why capturing input matters
Capturing user input lets the app use that data to update UI or perform actions.Final Answer:
To let the app know what the user types and respond accordingly -> Option CQuick Check:
Input widgets capture user data = To let the app know what the user types and respond accordingly [OK]
Hint: Input widgets capture what users type for app use [OK]
Common Mistakes:
- Thinking input widgets only display text
- Believing input widgets block typing
- Assuming input widgets change colors automatically
2. Which Flutter code snippet correctly captures user input from a
TextField?easy
Solution
Step 1: Identify the correct event for text input
TheonChangedcallback triggers whenever the user types or changes text.Step 2: Check the other options
onTaptriggers on tap,onPressedis not valid forTextField,onSubmitis not a valid property.Final Answer:
TextField(onChanged: (text) { print(text); }) -> Option DQuick Check:
onChangedcaptures input = TextField(onChanged: (text) { print(text); }) [OK]
Hint: Use onChanged to capture text input changes [OK]
Common Mistakes:
- Using onTap instead of onChanged
- Trying onPressed which is for buttons
- Confusing onSubmit with onChanged
3. What will be displayed on screen after this Flutter code runs and the user types "Hi"?
String userInput = '';
TextField(
onChanged: (text) {
userInput = text;
},
),
Text('You typed: $userInput')medium
Solution
Step 1: Understand variable update in Flutter widgets
The variableuserInputis updated insideonChanged, but the widget does not rebuild automatically.Step 2: Recognize that UI won't update without setState
SincesetStateis not called, theTextwidget still shows the initial empty string.Final Answer:
You typed: -> Option BQuick Check:
Without setState, UI won't update = You typed: [OK]
Hint: State must update UI after input changes [OK]
Common Mistakes:
- Assuming UI updates automatically without setState
- Thinking variable changes refresh UI instantly
- Confusing variable value with displayed text
4. Identify the error in this Flutter code that tries to show user input live:
String input = '';
TextField(
onChanged: (text) {
input = text;
},
),
Text('Input: $input')medium
Solution
Step 1: Check how state updates UI in Flutter
Changing a variable alone does not refresh the UI;setStatemust be called.Step 2: Verify other options
onSubmitis not required here,inputshould not be final to change, andTextcan display variables.Final Answer:
Missing call to setState to update UI -> Option AQuick Check:
State update needs setState = Missing call to setState to update UI [OK]
Hint: Use setState to refresh UI after input changes [OK]
Common Mistakes:
- Forgetting setState call after variable change
- Using onSubmit instead of onChanged incorrectly
- Declaring input as final and trying to change it
5. How can you modify this Flutter snippet to show live user input below the
TextField?
String input = '';
TextField(
onChanged: (text) {
input = text;
},
),
Text('Typed: $input')hard
Solution
Step 1: Recognize need for state management
To update UI live, the widget must be stateful and callsetStatewhen input changes.Step 2: Check other options for correctness
Making input final prevents changes, StatelessWidget can't update UI dynamically, and removing onChanged loses live updates.Final Answer:
Wrap in StatefulWidget and call setState inside onChanged -> Option AQuick Check:
StatefulWidget + setState for live input = Wrap in StatefulWidget and call setState inside onChanged [OK]
Hint: Use StatefulWidget and setState for live input display [OK]
Common Mistakes:
- Using StatelessWidget for dynamic input display
- Making input variable final and unchangeable
- Removing onChanged callback loses live updates
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
