Component - Why input widgets capture user data
This UI component shows how input widgets like TextField capture what a user types. It lets the app get and use the data entered by the user.
This UI component shows how input widgets like TextField capture what a user types. It lets the app get and use the data entered by the user.
Scaffold
├─ AppBar
│ └─ Text
└─ Padding
└─ Column
├─ TextField
└─ TextTextField capture user data in a Flutter app?TextField allow users to type text into the app.TextField?onChanged callback triggers whenever the user types or changes text.onTap triggers on tap, onPressed is not valid for TextField, onSubmit is not a valid property.onChanged captures input = TextField(onChanged: (text) { print(text); }) [OK]String userInput = '';
TextField(
onChanged: (text) {
userInput = text;
},
),
Text('You typed: $userInput')userInput is updated inside onChanged, but the widget does not rebuild automatically.setState is not called, the Text widget still shows the initial empty string.String input = '';
TextField(
onChanged: (text) {
input = text;
},
),
Text('Input: $input')setState must be called.onSubmit is not required here, input should not be final to change, and Text can display variables.TextField?
String input = '';
TextField(
onChanged: (text) {
input = text;
},
),
Text('Typed: $input')setState when input changes.Pass the test and this pattern moves unknown