EasyCustomFormField<T, C extends ValueNotifier> class
An extensible base class for wrapping form fields.
It is used as a base class for creating fields of custom types, as well as for creating a custom field on the fly in the widget tree.
For a custom field type, you must specify two general types:
the first T
is the field value type, and the second C
is
the controller type.
When a controller is specified, its Controller.value
defines the initialValue. If this EasyCustomFormField is part of a scrolling
container that lazily constructs its children, like a ListView or a
CustomScrollView, then a controller should be specified.
The controller's lifetime should be managed by a stateful widget ancestor
of the scrolling container.
If a controller is not specified, initialValue can be used to give the automatically generated controller an initial value.
Remember to call Controller.dispose
of the Controller
when it is no longer needed. This will ensure we discard any resources used
by the object.
Creates a EasyCustomFormField with an custom field and controller.
EasyCustomFormField<Color, ColorFieldController>(
name: 'color',
initialValue: Colors.teal,
controllerBuilder: (value) => ColorFieldController(value),
builder: (fieldState, onChangedHandler) => ColorField(
controller: fieldState.controller,
onChange: onChangedHandler,
),
)
Creates a TextField on the fly.
EasyCustomFormField<String, TextEditingController>(
name: 'first_name',
initialValue: 'some text',
controllerBuilder: (value) => TextEditingController(text: value),
controllerRebuilder: (oldController) => TextEditingController.fromValue(oldController.value),
valueGet: (controller) => controller.text,
valueSet: (controller, newText) => controller.text = newText,
builder: (fieldState, onChangedHandler) => TextField(
controller: fieldState.controller,
focusNode: fieldState.focusNode,
decoration: InputDecoration(
errorText: fieldState.errorText,
),
onChanged: onChangedHandler,
),
validator: (value) => value.isEmpty ? 'Field is required.' : null,
),
See also:
- material.io/design/components/text-fields.html
- TextField, which is the underlying text field without the Form integration.
- Learn how to use a
Controller
in one of our cookbook recipes.
- Inheritance
-
- Object
- DiagnosticableTree
- Widget
- StatefulWidget
- EasyFormField<
T?> - EasyCustomFormField
- Implementers
Constructors
-
EasyCustomFormField({Key? key, required String name, C? controller, FocusNode? focusNode, T? initialValue, required CreateControllerCallback<
C, T?> controllerBuilder, RecreateControllerCallback<C> ? controllerRebuilder, ValueOfGetter<T?, C> ? valueGet, ValueOfSetter<T?, C> ? valueSet, required EasyFormCustomFieldBuilder<T, C> builder, ValueChanged<T> ? onChanged, FormFieldSetter<T> ? onSaved, EasyFormFieldValidator<T?> ? validator, bool enabled = true, EasyAutovalidateMode autovalidateMode = EasyAutovalidateMode.disabled}) - Creates a EasyCustomFormField that contains a custom field.
Properties
- autovalidateMode → EasyAutovalidateMode
-
Used to enable/disable this form field auto validation and update its
error text.
finalinherited
-
builder
→ EasyFormFieldBuilder<
T?> -
Function that returns the widget representing this form field. It is
passed the form field state as input, containing the current value and
validation state of this field.
finalinherited
- controller → C?
-
Controls the value being edited.
final
-
controllerBuilder
→ CreateControllerCallback<
C, T?> -
A builder that instantiates a controller of type
C
for a value of typeT
.final -
controllerRebuilder
→ RecreateControllerCallback<
C> ? -
A builder that recreates a controller from an old controller,
if not specified, the
controllerBuilder
builder will be used.final - enabled → bool
-
Whether the form is able to receive user input.
finalinherited
- focusNode → FocusNode?
-
Focus node.
finalinherited
- hashCode → int
-
The hash code for this object.
no setterinherited
- initialValue → T?
-
An optional value to initialize the form field to, or null otherwise.
finalinherited
- key → Key?
-
Controls how one widget replaces another widget in the tree.
finalinherited
- name → String
-
Name of the field.
finalinherited
-
onSaved
→ EasyFormFieldSetter<
T?> ? -
An optional method to call with the final value when the form is saved via
EasyFormState.save.
finalinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
-
validator
→ EasyFormFieldValidator<
T?> ? -
An optional method that validates an input. Returns an error string to
display if the input is invalid, or null otherwise.
finalinherited
-
valueGet
→ ValueOfGetter<
T?, C> ? -
Callback to get the value from the controller, if not set
then the controller's
value
property is used.final -
valueSet
→ ValueOfSetter<
T?, C> ? -
Callback to set controller value, if not set - controller's
value
property is used.final
Methods
-
createController(
T value) → C -
A builder that instantiates a controller of type
C
for a value of typeT
. -
createElement(
) → StatefulElement -
Creates a StatefulElement to manage this widget's location in the tree.
inherited
-
createState(
) → _EasyCustomFormFieldState< T, C> -
Creates the mutable state for this widget at a given location in the tree.
override
-
debugDescribeChildren(
) → List< DiagnosticsNode> -
Returns a list of DiagnosticsNode objects describing this node's
children.
inherited
-
debugFillProperties(
DiagnosticPropertiesBuilder properties) → void -
Add additional properties associated with the node.
inherited
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
recreateController(
C oldController) → C - A builder that recreates a controller from an old controller.
-
setValue(
C controller, T? value) → dynamic - Setter, to set the controller value.
-
toDiagnosticsNode(
{String? name, DiagnosticsTreeStyle? style}) → DiagnosticsNode -
Returns a debug representation of the object that is used by debugging
tools and by DiagnosticsNode.toStringDeep.
inherited
-
toString(
{DiagnosticLevel minLevel = DiagnosticLevel.info}) → String -
A string representation of this object.
inherited
-
toStringDeep(
{String prefixLineOne = '', String? prefixOtherLines, DiagnosticLevel minLevel = DiagnosticLevel.debug}) → String -
Returns a string representation of this node and its descendants.
inherited
-
toStringShallow(
{String joiner = ', ', DiagnosticLevel minLevel = DiagnosticLevel.debug}) → String -
Returns a one-line detailed description of the object.
inherited
-
toStringShort(
) → String -
A short, textual description of this widget.
inherited
-
valueOf(
C controller) → T? - Getter, to get the value from the controller.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited