DropdownButtonFormField2<T> constructor

DropdownButtonFormField2<T>({
  1. Key? key,
  2. required List<DropdownMenuItem<T>>? items,
  3. DropdownButtonBuilder? selectedItemBuilder,
  4. T? value,
  5. Widget? hint,
  6. Widget? disabledHint,
  7. ValueChanged<T?>? onChanged,
  8. int dropdownElevation = 8,
  9. TextStyle? style,
  10. Widget? icon,
  11. Widget? iconOnClick,
  12. Color? iconDisabledColor,
  13. Color? iconEnabledColor,
  14. double iconSize = 24.0,
  15. bool isDense = true,
  16. bool isExpanded = false,
  17. double itemHeight = kMinInteractiveDimension,
  18. Color? focusColor,
  19. FocusNode? focusNode,
  20. bool autofocus = false,
  21. InputDecoration? decoration,
  22. FormFieldSetter<T>? onSaved,
  23. FormFieldValidator<T>? validator,
  24. AutovalidateMode? autovalidateMode,
  25. double? dropdownMaxHeight,
  26. bool? enableFeedback,
  27. AlignmentGeometry alignment = AlignmentDirectional.centerStart,
  28. double? buttonHeight,
  29. double? buttonWidth,
  30. EdgeInsetsGeometry? buttonPadding,
  31. BoxDecoration? buttonDecoration,
  32. int? buttonElevation,
  33. EdgeInsetsGeometry? itemPadding,
  34. double? dropdownWidth,
  35. EdgeInsetsGeometry? dropdownPadding,
  36. BoxDecoration? dropdownDecoration,
  37. Color? selectedItemHighlightColor,
  38. Radius? scrollbarRadius,
  39. double? scrollbarThickness,
  40. bool? scrollbarAlwaysShow,
  41. Offset? offset,
  42. Widget? customButton,
  43. List<int>? customItemsIndexes,
  44. double? customItemsHeight,
  45. bool openWithLongPress = false,
  46. bool dropdownOverButton = false,
  47. bool dropdownFullScreen = false,
  48. void onMenuStateChange(
    1. bool isOpen
    )?,
})

Creates a DropdownButton2 widget that is a FormField, wrapped in an InputDecorator.

For a description of the onSaved, validator, or autovalidateMode parameters, see FormField. For the rest (other than decoration), see DropdownButton2.

The items, elevation, iconSize, isDense, isExpanded, autofocus, and decoration parameters must not be null.

Implementation

DropdownButtonFormField2({
  Key? key,
  required List<DropdownMenuItem<T>>? items,
  DropdownButtonBuilder? selectedItemBuilder,
  T? value,
  Widget? hint,
  Widget? disabledHint,
  this.onChanged,
  int dropdownElevation = 8,
  TextStyle? style,
  Widget? icon,
  Widget? iconOnClick,
  Color? iconDisabledColor,
  Color? iconEnabledColor,
  double iconSize = 24.0,
  bool isDense = true,
  bool isExpanded = false,
  double itemHeight = kMinInteractiveDimension,
  Color? focusColor,
  FocusNode? focusNode,
  bool autofocus = false,
  InputDecoration? decoration,
  FormFieldSetter<T>? onSaved,
  FormFieldValidator<T>? validator,
  AutovalidateMode? autovalidateMode,
  double? dropdownMaxHeight,
  bool? enableFeedback,
  AlignmentGeometry alignment = AlignmentDirectional.centerStart,
  double? buttonHeight,
  double? buttonWidth,
  EdgeInsetsGeometry? buttonPadding,
  BoxDecoration? buttonDecoration,
  int? buttonElevation,
  EdgeInsetsGeometry? itemPadding,
  double? dropdownWidth,
  EdgeInsetsGeometry? dropdownPadding,
  BoxDecoration? dropdownDecoration,
  Color? selectedItemHighlightColor,
  Radius? scrollbarRadius,
  double? scrollbarThickness,
  bool? scrollbarAlwaysShow,
  Offset? offset,
  Widget? customButton,
  List<int>? customItemsIndexes,
  double? customItemsHeight,
  bool openWithLongPress = false,
  bool dropdownOverButton = false,
  bool dropdownFullScreen = false,
  void Function(bool isOpen)? onMenuStateChange,
})  : assert(
items == null ||
    items.isEmpty ||
    value == null ||
    items.where((DropdownMenuItem<T> item) {
      return item.value == value;
    }).length ==
        1,
"There should be exactly one item with [DropdownButton]'s value: "
    '$value. \n'
    'Either zero or 2 or more [DropdownMenuItem]s were detected '
    'with the same value',
),
      decoration = decoration ?? InputDecoration(focusColor: focusColor),
      super(
      key: key,
      onSaved: onSaved,
      initialValue: value,
      validator: validator,
      autovalidateMode: autovalidateMode ?? AutovalidateMode.disabled,
      builder: (FormFieldState<T> field) {
        final _DropdownButtonFormFieldState<T> state =
        field as _DropdownButtonFormFieldState<T>;
        final InputDecoration decorationArg =
            decoration ?? InputDecoration(focusColor: focusColor);
        final InputDecoration effectiveDecoration =
        decorationArg.applyDefaults(
          Theme.of(field.context).inputDecorationTheme,
        );

        final bool showSelectedItem = items != null &&
            items
                .where(
                    (DropdownMenuItem<T> item) => item.value == state.value)
                .isNotEmpty;
        bool isHintOrDisabledHintAvailable() {
          final bool isDropdownDisabled =
              onChanged == null || (items == null || items.isEmpty);
          if (isDropdownDisabled) {
            return hint != null || disabledHint != null;
          } else {
            return hint != null;
          }
        }

        final bool isEmpty =
            !showSelectedItem && !isHintOrDisabledHintAvailable();

        // An unfocusable Focus widget so that this widget can detect if its
        // descendants have focus or not.
        return Focus(
          canRequestFocus: false,
          skipTraversal: true,
          child: Builder(builder: (BuildContext context) {
            return DropdownButtonHideUnderline(
              child: DropdownButton2._formField(
                items: items,
                selectedItemBuilder: selectedItemBuilder,
                value: state.value,
                hint: hint,
                disabledHint: disabledHint,
                onChanged: onChanged == null ? null : state.didChange,
                dropdownElevation: dropdownElevation,
                style: style,
                icon: icon,
                iconOnClick: iconOnClick,
                iconDisabledColor: iconDisabledColor,
                iconEnabledColor: iconEnabledColor,
                iconSize: iconSize,
                isDense: isDense,
                isExpanded: isExpanded,
                itemHeight: itemHeight,
                focusColor: focusColor,
                focusNode: focusNode,
                autofocus: autofocus,
                dropdownMaxHeight: dropdownMaxHeight,
                enableFeedback: enableFeedback,
                alignment: alignment,
                buttonHeight: buttonHeight,
                buttonWidth: buttonWidth,
                buttonPadding: buttonPadding,
                buttonDecoration: buttonDecoration,
                buttonElevation: buttonElevation,
                itemPadding: itemPadding,
                dropdownWidth: dropdownWidth,
                dropdownPadding: dropdownPadding,
                dropdownDecoration: dropdownDecoration,
                selectedItemHighlightColor: selectedItemHighlightColor,
                scrollbarRadius: scrollbarRadius,
                scrollbarThickness: scrollbarThickness,
                scrollbarAlwaysShow: scrollbarAlwaysShow,
                offset: offset,
                customButton: customButton,
                customItemsIndexes: customItemsIndexes,
                customItemsHeight: customItemsHeight,
                openWithLongPress: openWithLongPress,
                dropdownOverButton: dropdownOverButton,
                dropdownFullScreen: dropdownFullScreen,
                onMenuStateChange: onMenuStateChange,
                inputDecoration: effectiveDecoration.copyWith(
                  errorText: field.errorText,
                ),
                isEmpty: isEmpty,
              ),
            );
          }),
        );
      },
    );