معظم التطبيقات تحتوي على نماذج (تسجيل الدخول، التسجيل، إدخال البيانات) تتطلب معالجة الإدخال من المستخدم والتحقق من الصحة. يوفر Flutter Form و TextFormField والمدققات والمتحكمات لبناء التحقق من صحة النماذج — وهو أمر ضروري لأي تطبيق يتضمن إدخال المستخدم.
النموذج و 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'),
),
]),
)
