DropdownButtonFormField2<T> constructor
DropdownButtonFormField2<T> ({
- Key? key,
- 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,
- 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 onMenuStateChange(
- 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,
),
);
}),
);
},
);