FormBuilderDropdown<T> constructor

FormBuilderDropdown<T>({
  1. Key? key,
  2. required String name,
  3. FormFieldValidator<T>? validator,
  4. T? initialValue,
  5. InputDecoration decoration = const InputDecoration(),
  6. ValueChanged<T?>? onChanged,
  7. ValueTransformer<T?>? valueTransformer,
  8. bool enabled = true,
  9. FormFieldSetter<T>? onSaved,
  10. AutovalidateMode autovalidateMode = AutovalidateMode.disabled,
  11. VoidCallback? onReset,
  12. FocusNode? focusNode,
  13. required List<DropdownMenuItem<T>> items,
  14. bool isExpanded = true,
  15. bool isDense = true,
  16. int elevation = 8,
  17. double iconSize = 24.0,
  18. Widget? hint,
  19. TextStyle? style,
  20. Widget? disabledHint,
  21. Widget? icon,
  22. Color? iconDisabledColor,
  23. Color? iconEnabledColor,
  24. bool allowClear = false,
  25. Widget clearIcon = const Icon(Icons.close),
  26. VoidCallback? onTap,
  27. bool autofocus = false,
  28. Color? dropdownColor,
  29. Color? focusColor,
  30. double itemHeight = kMinInteractiveDimension,
  31. DropdownButtonBuilder? selectedItemBuilder,
})

Creates field for Dropdown button

Implementation

FormBuilderDropdown({
  Key? key,
  //From Super
  required String name,
  FormFieldValidator<T>? validator,
  T? initialValue,
  InputDecoration decoration = const InputDecoration(),
  ValueChanged<T?>? onChanged,
  ValueTransformer<T?>? valueTransformer,
  bool enabled = true,
  FormFieldSetter<T>? onSaved,
  AutovalidateMode autovalidateMode = AutovalidateMode.disabled,
  VoidCallback? onReset,
  FocusNode? focusNode,
  required this.items,
  this.isExpanded = true,
  this.isDense = true,
  this.elevation = 8,
  this.iconSize = 24.0,
  this.hint,
  this.style,
  this.disabledHint,
  this.icon,
  this.iconDisabledColor,
  this.iconEnabledColor,
  this.allowClear = false,
  this.clearIcon = const Icon(Icons.close),
  this.onTap,
  this.autofocus = false,
  this.dropdownColor,
  this.focusColor,
  this.itemHeight = kMinInteractiveDimension,
  this.selectedItemBuilder,
}) : /*: assert(allowClear == true || clearIcon != null)*/ super(
        key: key,
        initialValue: initialValue,
        name: name,
        validator: validator,
        valueTransformer: valueTransformer,
        onChanged: onChanged,
        autovalidateMode: autovalidateMode,
        onSaved: onSaved,
        enabled: enabled,
        onReset: onReset,
        decoration: decoration,
        focusNode: focusNode,
        builder: (FormFieldState<T?> field) {
          final state = field as _FormBuilderDropdownState<T>;
          // DropdownButtonFormField
          // TextFormField

          void changeValue(T? value) {
            state.requestFocus();
            state.didChange(value);
          }

          return InputDecorator(
            decoration: state.decoration.copyWith(
                  floatingLabelBehavior: hint == null
                      ? decoration.floatingLabelBehavior
                      : FloatingLabelBehavior.always,
                ),
            isEmpty: state.value == null,
            child: Row(
              children: <Widget>[
                Expanded(
                  child: DropdownButtonHideUnderline(
                    child: DropdownButton<T>(
                      isExpanded: isExpanded,
                      hint: hint,
                      items: items,
                      value: field.value,
                      style: style,
                      isDense: isDense,
                      disabledHint: field.value != null
                          ? (items
                                  .firstWhereOrNull(
                                      (val) => val.value == field.value)
                                  ?.child ??
                              Text(field.value.toString()))
                          : disabledHint,
                      elevation: elevation,
                      iconSize: iconSize,
                      icon: icon,
                      iconDisabledColor: iconDisabledColor,
                      iconEnabledColor: iconEnabledColor,
                      onChanged: state.enabled
                          ? (value) => changeValue(value)
                          : null,
                      onTap: onTap,
                      focusNode: state.effectiveFocusNode,
                      autofocus: autofocus,
                      dropdownColor: dropdownColor,
                      focusColor: focusColor,
                      itemHeight: itemHeight,
                      selectedItemBuilder: selectedItemBuilder,
                    ),
                  ),
                ),
                if (allowClear && state.enabled && field.value != null) ...[
                  const VerticalDivider(),
                  InkWell(
                    onTap: () => changeValue(null),
                    child: clearIcon,
                  ),
                ]
              ],
            ),
          );
        },
      );