FormField<T> class abstract

Abstract base class for all form field widgets in termui.

Form fields are widgets designed to collect user input, manage validation rules, track focused status, and represent state within an ancestor Form.

When using dynamic forms (with Form(child: ...)), form fields walk up the BuildContext to locate the ancestor Form and register their state dynamically.

Subclassing FormField

To create a custom FormField:

  1. Extend FormField specifying the data type T (e.g. class MyField extends FormField<int>).
  2. Implement createState to return a state class extending FormFieldState.
  3. Provide layout requirements by overriding getPreferredHeight.
  4. Implement event processing by overriding handleKeyEvent.

Core API Reference

Property Type Description
label String Header title of the input field.
description String Auxiliary description shown below the label.
initialValue T? The default value when initialized or reset.
validator String? Function(T?)? Callback to execute during validation.
focused bool Current keyboard focus status.
Inheritance
Implementers

Constructors

FormField({required String label, String description = '', T? initialValue, String? validator(T?)?, bool focused = false})
Creates a FormField with common properties.

Properties

description String
Optional secondary description text for helper information.
final
errorText String?
The validation error text if validation fails; otherwise, null.
getter/setter pair
focused bool
Whether this field currently has input focus.
getter/setter pair
hasError bool
Returns whether this field currently contains a validation error.
no setter
hashCode int
The hash code for this object.
no setterinherited
initialValue → T?
The initial value of the field when created or reset.
final
key Key?
The optional key for this widget.
finalinherited
label String
The title label displayed for this field.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
touched bool
Whether this field has been focused at least once.
getter/setter pair
validator String? Function(T?)?
A validation function called by FormState.validate. Returns an error message string if invalid, or null if valid.
final
value ↔ T?
The current value of the form field.
getter/setter pair

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.
inherited
getIntrinsicHeight(int width) int
Computes the intrinsic height of this widget under the given width constraint.
inherited
getPreferredHeight() int
Calculates preferred layout height needed to render this field cleanly.
handleKeyEvent(KeyEvent event) → void
Handles user key inputs to update the internal value.
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 buffer within the specified area.
inherited
toString() String
A string representation of this object.
inherited
validate() bool
Runs the validator callback against the current field value. Updates errorText and returns whether the field is valid.

Operators

operator ==(Object other) bool
The equality operator.
inherited