Form class
A container widget that coordinates focus, validation, and resets for multiple FormField widgets.
Form can be used in two ways:
- Legacy Mode: By providing a flat list of form fields directly via the fields parameter.
- Declarative Mode: By passing a child widget tree containing nested form fields.
Under declarative mode, form fields can be nested inside columns, rows, padding, or custom widgets. They will dynamically locate and register their state with the Form using an inherited widget lookup.
Example (Declarative Mode)
Form(
child: Column([
TextFormField(
label: 'Username',
validator: (val) => (val == null || val.isEmpty) ? 'Required' : null,
),
ConfirmFormField(
label: 'Accept terms',
),
]),
)
- Inheritance
-
- Object
- Widget
- StatefulWidget
- Form
Constructors
Properties
- activeFieldIndex ↔ int
-
The index of the currently active form field.
getter/setter pair
- child → Widget?
-
The child subtree containing form fields. Used in Declarative Mode.
final
-
fields
→ List<
FormField> -
A flat list of form fields. Used in Legacy Mode.
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- key → Key?
-
The optional key for this widget.
finalinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
createElement(
) → Element -
Creates an Element to manage this widget's location in the tree.
inherited
-
createState(
) → State< StatefulWidget> -
Creates the mutable state for this widget at a given location in the tree.
override
-
getIntrinsicHeight(
int width) → int -
Computes the intrinsic height of this widget under the given
widthconstraint.inherited -
handleKeyEvent(
KeyEvent event) → void - Moves focus between fields using Tab/Shift-Tab, and routes other events to the active field.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
render(
Buffer buffer, Rect area) → void -
Renders the widget onto the provided
bufferwithin the specifiedarea.inherited -
toString(
) → String -
A string representation of this object.
inherited
-
validate(
) → bool - Validates all child fields, returning true if all are valid.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Methods
-
of(
BuildContext context) → FormState? - Retrieves the closest ancestor FormState in the tree.