ReactiveDropdownField<T> constructor

ReactiveDropdownField<T>({
  1. Key? key,
  2. String? formControlName,
  3. FormControl<T>? formControl,
  4. required List<DropdownMenuItem<T>> items,
  5. ValidationMessagesFunction<T>? validationMessages,
  6. ShowErrorsFunction? showErrors,
  7. DropdownButtonBuilder? selectedItemBuilder,
  8. Widget? hint,
  9. VoidCallback? onTap,
  10. InputDecoration decoration = const InputDecoration(),
  11. Widget? disabledHint,
  12. int elevation = 8,
  13. TextStyle? style,
  14. Widget? icon,
  15. Color? iconDisabledColor,
  16. Color? iconEnabledColor,
  17. double iconSize = 24.0,
  18. bool isDense = true,
  19. bool isExpanded = false,
  20. bool readOnly = false,
  21. double? itemHeight,
  22. ValueChanged<T?>? onChanged,
  23. Color? dropdownColor,
  24. Color? focusColor,
  25. Widget? underline,
  26. bool autofocus = false,
  27. double? menuMaxHeight,
  28. bool? enableFeedback,
  29. AlignmentGeometry alignment = AlignmentDirectional.centerStart,
  30. BorderRadius? borderRadius,
})

Creates a DropdownButton widget wrapped in an InputDecorator.

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.

If readOnly is true, the button will be disabled, the down arrow will be grayed out, and the disabledHint will be shown (if provided).

The DropdownButton items parameters must not be null.

Implementation

ReactiveDropdownField({
  Key? key,
  String? formControlName,
  FormControl<T>? formControl,
  required List<DropdownMenuItem<T>> items,
  ValidationMessagesFunction<T>? validationMessages,
  ShowErrorsFunction? showErrors,
  DropdownButtonBuilder? selectedItemBuilder,
  Widget? hint,
  VoidCallback? onTap,
  InputDecoration decoration = const InputDecoration(),
  Widget? disabledHint,
  int elevation = 8,
  TextStyle? style,
  Widget? icon,
  Color? iconDisabledColor,
  Color? iconEnabledColor,
  double iconSize = 24.0,
  bool isDense = true,
  bool isExpanded = false,
  bool readOnly = false,
  double? itemHeight,
  ValueChanged<T?>? onChanged,
  Color? dropdownColor,
  Color? focusColor,
  Widget? underline,
  bool autofocus = false,
  double? menuMaxHeight,
  bool? enableFeedback,
  AlignmentGeometry alignment = AlignmentDirectional.centerStart,
  BorderRadius? borderRadius,
})  : assert(itemHeight == null || itemHeight > 0),
      super(
        key: key,
        formControl: formControl,
        formControlName: formControlName,
        validationMessages: validationMessages,
        showErrors: showErrors,
        builder: (ReactiveFormFieldState<T, T> field) {
          final state = field as _ReactiveDropdownFieldState<T>;

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

          var effectiveValue = field.value;
          if (effectiveValue != null &&
              !items.any((item) => item.value == effectiveValue)) {
            effectiveValue = null;
          }

          final isDisabled = readOnly || field.control.disabled;
          var effectiveDisabledHint = disabledHint;
          if (isDisabled && disabledHint == null) {
            final selectedItemIndex =
                items.indexWhere((item) => item.value == effectiveValue);
            if (selectedItemIndex > -1) {
              effectiveDisabledHint = selectedItemBuilder != null
                  ? selectedItemBuilder(field.context)
                      .elementAt(selectedItemIndex)
                  : items.elementAt(selectedItemIndex).child;
            }
          }

          return InputDecorator(
            decoration: effectiveDecoration.copyWith(
              errorText: field.errorText,
              enabled: !isDisabled,
            ),
            isEmpty: effectiveValue == null,
            child: DropdownButtonHideUnderline(
              child: DropdownButton<T>(
                value: effectiveValue,
                items: items,
                selectedItemBuilder: selectedItemBuilder,
                hint: hint,
                onChanged: isDisabled
                    ? null
                    : (T? value) => state._onChanged(value, onChanged),
                onTap: onTap,
                disabledHint: effectiveDisabledHint,
                elevation: elevation,
                style: style,
                icon: icon,
                iconDisabledColor: iconDisabledColor,
                iconEnabledColor: iconEnabledColor,
                iconSize: iconSize,
                isDense: isDense,
                isExpanded: isExpanded,
                itemHeight: itemHeight,
                focusNode: state._focusController.focusNode,
                dropdownColor: dropdownColor,
                focusColor: focusColor,
                underline: underline,
                autofocus: autofocus,
                menuMaxHeight: menuMaxHeight,
                enableFeedback: enableFeedback,
                alignment: alignment,
                borderRadius: borderRadius,
              ),
            ),
          );
        },
      );