ReactiveDirectSelect<T, V> constructor

ReactiveDirectSelect<T, V>({
  1. Key? key,
  2. String? formControlName,
  3. FormControl<T>? formControl,
  4. Map<String, ValidationMessageFunction>? validationMessages,
  5. ControlValueAccessor<T, V>? valueAccessor,
  6. ShowErrorsFunction<T>? showErrors,
  7. InputDecoration decoration = const InputDecoration(),
  8. required List<V> items,
  9. required ItemBuilder<V> itemBuilder,
  10. required ItemBuilder<V> selectedItemBuilder,
  11. required double itemExtent,
  12. FocusNode? focusNode,
  13. DirectSelectMode mode = DirectSelectMode.drag,
  14. double itemMagnification = 1.15,
  15. Color backgroundColor = Colors.white,
  16. Color selectionColor = Colors.black12,
})

Creates a ReactiveDirectSelect that contains a DirectSelect.

Can optionally provide a formControl to bind this widget to a control.

Can optionally provide a formControlName to bind this ReactiveFormField to a FormControl.

Must provide one of the arguments formControl or a formControlName, but not both at the same time.

Can optionally provide a validationMessages argument to customize a message for different kinds of validation errors.

Can optionally provide a valueAccessor to set a custom value accessors. See ControlValueAccessor.

Can optionally provide a showErrors function to customize when to show validation messages. Reactive Widgets make validation messages visible when the control is INVALID and TOUCHED, this behavior can be customized in the showErrors function.

Example:

Binds a text field.

final form = fb.group({'email': Validators.required});

ReactiveDirectSelect(
  formControlName: 'email',
),

Binds a text field directly with a FormControl.

final form = fb.group({'email': Validators.required});

ReactiveDirectSelect(
  formControl: form.control('email'),
),

Customize validation messages

ReactiveDirectSelect(
  formControlName: 'email',
  validationMessages: {
    ValidationMessage.required: 'The email must not be empty',
    ValidationMessage.email: 'The email must be a valid email',
  }
),

Customize when to show up validation messages.

ReactiveDirectSelect(
  formControlName: 'email',
  showErrors: (control) => control.invalid && control.touched && control.dirty,
),

For documentation about the various parameters, see the DirectSelect class and DirectSelect, the constructor.

Implementation

ReactiveDirectSelect({
  super.key,
  super.formControlName,
  super.formControl,
  super.validationMessages,
  super.valueAccessor,
  super.showErrors,

  ////////////////////////////////////////////////////////////////////////////
  InputDecoration decoration = const InputDecoration(),
  required List<V> items,
  required ItemBuilder<V> itemBuilder,
  required ItemBuilder<V> selectedItemBuilder,
  required double itemExtent,
  FocusNode? focusNode,
  DirectSelectMode mode = DirectSelectMode.drag,
  double itemMagnification = 1.15,
  Color backgroundColor = Colors.white,
  Color selectionColor = Colors.black12,
}) : super(
        builder: (field) {
          final state = field as _ReactiveMacosTextFieldState<T, V>;

          final InputDecoration effectiveDecoration = decoration
              .applyDefaults(Theme.of(field.context).inputDecorationTheme);

          state._setFocusNode(focusNode);

          return DirectSelect(
            items: items.map((i) => itemBuilder(field.context, i)).toList(),
            onSelectedItemChanged: (i) => field.didChange(
              i != null ? items[i] : null,
            ),
            itemExtent: itemExtent,
            selectedIndex: items.indexWhere((e) => e == field.value),
            mode: mode,
            itemMagnification: itemMagnification,
            backgroundColor: backgroundColor,
            selectionColor: selectionColor,
            child: InputDecorator(
              decoration: effectiveDecoration.copyWith(
                errorText: field.errorText,
                enabled: field.control.enabled,
              ),
              child: selectedItemBuilder(
                field.context,
                field.value,
              ),
            ),
          );
        },
      );