DropdownButtonFormField2<T> constructor
DropdownButtonFormField2<T> ({
- Key? key,
- Key? dropdownButtonKey,
- required List<
DropdownMenuItem< ? items,T> > - DropdownButtonBuilder? selectedItemBuilder,
- T? value,
- Widget? hint,
- Widget? disabledHint,
- ValueChanged<
T?> ? 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,
- Color? buttonSplashColor,
- Color? buttonHighlightColor,
- MaterialStateProperty<
Color?> ? buttonOverlayColor, - EdgeInsetsGeometry? itemPadding,
- Color? itemSplashColor,
- Color? itemHighlightColor,
- double? dropdownWidth,
- EdgeInsetsGeometry? dropdownPadding,
- EdgeInsetsGeometry? dropdownScrollPadding,
- BoxDecoration? dropdownDecoration,
- DropdownDirection dropdownDirection = DropdownDirection.textDirection,
- Color? selectedItemHighlightColor,
- Radius? scrollbarRadius,
- double? scrollbarThickness,
- bool? scrollbarAlwaysShow,
- Offset? offset,
- Widget? customButton,
- List<
double> ? customItemsHeights, - bool openWithLongPress = false,
- bool dropdownOverButton = false,
- bool dropdownFullScreen = false,
- bool barrierDismissible = true,
- Color? barrierColor,
- String? barrierLabel,
- TextEditingController? searchController,
- Widget? searchInnerWidget,
- double? searchInnerWidgetHeight,
- _SearchMatchFn? searchMatchFn,
- _OnMenuStateChangeFn? onMenuStateChange,
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({
super.key,
this.dropdownButtonKey,
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,
super.onSaved,
super.validator,
AutovalidateMode? autovalidateMode,
double? dropdownMaxHeight,
bool? enableFeedback,
AlignmentGeometry alignment = AlignmentDirectional.centerStart,
double? buttonHeight,
double? buttonWidth,
EdgeInsetsGeometry? buttonPadding,
BoxDecoration? buttonDecoration,
int? buttonElevation,
Color? buttonSplashColor,
Color? buttonHighlightColor,
MaterialStateProperty<Color?>? buttonOverlayColor,
EdgeInsetsGeometry? itemPadding,
Color? itemSplashColor,
Color? itemHighlightColor,
double? dropdownWidth,
EdgeInsetsGeometry? dropdownPadding,
EdgeInsetsGeometry? dropdownScrollPadding,
BoxDecoration? dropdownDecoration,
DropdownDirection dropdownDirection = DropdownDirection.textDirection,
Color? selectedItemHighlightColor,
Radius? scrollbarRadius,
double? scrollbarThickness,
bool? scrollbarAlwaysShow,
Offset? offset,
Widget? customButton,
List<double>? customItemsHeights,
bool openWithLongPress = false,
bool dropdownOverButton = false,
bool dropdownFullScreen = false,
bool barrierDismissible = true,
Color? barrierColor,
String? barrierLabel,
TextEditingController? searchController,
Widget? searchInnerWidget,
double? searchInnerWidgetHeight,
_SearchMatchFn? searchMatchFn,
_OnMenuStateChangeFn? 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(
initialValue: value,
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();
bool hasFocus = false;
// An unFocusable Focus widget so that this widget can detect if its
// descendants have focus or not.
return Focus(
canRequestFocus: false,
skipTraversal: true,
child: StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return InputDecorator(
decoration: effectiveDecoration.copyWith(
errorText: field.errorText),
isEmpty: isEmpty,
isFocused: hasFocus,
textAlignVertical: TextAlignVertical.bottom,
child: DropdownButtonHideUnderline(
child: DropdownButton2._formField(
key: dropdownButtonKey,
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,
buttonSplashColor: buttonSplashColor,
buttonHighlightColor: buttonHighlightColor,
buttonOverlayColor: buttonOverlayColor,
itemPadding: itemPadding,
itemSplashColor: itemSplashColor,
itemHighlightColor: itemHighlightColor,
dropdownWidth: dropdownWidth,
dropdownPadding: dropdownPadding,
dropdownScrollPadding: dropdownScrollPadding,
dropdownDecoration: dropdownDecoration,
dropdownDirection: dropdownDirection,
selectedItemHighlightColor: selectedItemHighlightColor,
scrollbarRadius: scrollbarRadius,
scrollbarThickness: scrollbarThickness,
scrollbarAlwaysShow: scrollbarAlwaysShow,
offset: offset,
customButton: customButton,
customItemsHeights: customItemsHeights,
openWithLongPress: openWithLongPress,
dropdownOverButton: dropdownOverButton,
dropdownFullScreen: dropdownFullScreen,
onMenuStateChange: onMenuStateChange,
barrierDismissible: barrierDismissible,
barrierColor: barrierColor,
barrierLabel: barrierLabel,
searchController: searchController,
searchInnerWidget: searchInnerWidget,
searchInnerWidgetHeight: searchInnerWidgetHeight,
searchMatchFn: searchMatchFn,
formFieldCallBack: (isOpen) {
hasFocus = isOpen;
setState(() {});
},
),
),
);
},
),
);
},
);