Step 1 / 5
A Form groups fields
Form tracks every TextFormField inside it. The GlobalKey<FormState> gives you a handle to validate or reset them all at once.
Collect text input with controllers, validate fields, and submit only when everything is valid.
1final _formKey = GlobalKey<FormState>();2final _email = TextEditingController();34Form(5 key: _formKey,6 child: Column(children: [7 TextFormField(8 controller: _email,9 decoration: const InputDecoration(labelText: 'Email'),10 keyboardType: TextInputType.emailAddress,11 validator: (v) =>12 v != null && v.contains('@') ? null : 'Enter a valid email',13 ),14 FilledButton(15 onPressed: () {16 if (_formKey.currentState!.validate()) submit(_email.text);17 },18 child: const Text('Sign up'),19 ),20 ]),21)2223@override24void dispose() {25 _email.dispose();26 super.dispose();27}
Submit empty to see validator errors; a valid email + 8 chars signs in.
Form tracks every TextFormField inside it. The GlobalKey<FormState> gives you a handle to validate or reset them all at once.