பெரும்பாலான apps களுக்கு forms (login, signup, data entry) தேவை என்பதால் user input handling மற்றும் validation தேவைப்படுகிறது. Flutter Form, TextFormField, validators, மற்றும் controllers ஐ வழங்குகிறது forms ஐ build மற்றும் validate செய்ய — இது user input உடன் கூடிய எந்த appக்கும் essential ஆகும்.
Form மற்றும் TextFormField
final _formKey = GlobalKey<FormState>(); // a key to access the form's state
Form(
key: _formKey,
child: Column(children: [
TextFormField(
decoration: InputDecoration(labelText: 'Email'),
validator: (value) { // VALIDATION logic
if (value == null || value.isEmpty) return 'Email required';
if (!value.contains('@')) return 'Invalid email';
return null; // null = valid
},
),
ElevatedButton(
onPressed: () {
if (_formKey.currentState!.validate()) { // run all validators
// all fields valid → submit
}
},
child: Text('Submit'),
),
]),
)
