We need to give the users the ability to enter information into forms, fields, validate it and show validation messages to the user if
The purpose of this chapter is to learn how get Flutter apps working with fields, forms and validations.
Flutter provides objects to help you with the process of building forms, validation and input fields. It provides a Form object, Form Field objects (indirectly) and all the input types below:
5. TextFormField / TextField
This is a Widget that is designed to wrap form Widgets and provides control over validation.
The Form object gives you the following constructor arguments:
‘autovalidate’ to enable or disable automatic validation.
‘onChanged’ callback fired when one of the fields are changed.
The Form object gives you the following methods:
‘reset’ to reset fields.
‘save’ to save fields.
‘validate’ to validate, returning a true if the form fields are valid, false if one or more are invalid.
The Form object stores input state data from child TextFormFields but not other field types like Checkboxes, DropdownButtons, Radios,
So, if you want your form to work with those other types of fields, you need to store the state of those items. If you take a look a look at the example you will see that these fields are stored as state in the Stateful Widget.
As mentioned earlier, the Form class has a ‘autovalidate’ constructor argument.
If this argument is set to true, the framework invokes validation as data is input.
If this argument is set to false, the framework will not invoke validation until the ‘validate’ method is invoked.
Form / Field Integration
The FormField is a Widget used as a base class by field Widgets (such as TextFormField) to integrate the field with the parent Form Widget
and provide services such as validation.
Example – ‘form_details’
This example attempts to use all the input field types: text, radio buttons, checkboxes, selection lists and dates.