Building complex forms with the Form/FormField widgets
Hopefully, the previous section demonstrated that using the vanilla TextField
widgets inherently comes with quite a bit of boilerplate since much of the state of each input field must be maintained in the parent widget.
Fortunately, there is a more declarative API that encapsulates most of that boilerplate and allows us to group input fields. Let’s explore how to build forms using the Form
and FormField
widgets.
The Form
widget allows you to group multiple input fields to perform operations such as saving, resetting, or validating each input field in unison. The container leverages FormField
widgets rather than regular Field
widgets, which are its descendants. It allows these operations to be performed by using Form.of
with a context whose ancestor is the form, or by using a GlobalKey
passed to the form’s constructor.
To start, wrap the scaffold in contact_edit_view.dart
with a Form
widget and a Builder
widget...