DropdownMenuFormField<T> constructor

DropdownMenuFormField<T>({
  1. Key? key,
  2. bool enabled = true,
  3. double? width,
  4. double? menuHeight,
  5. Widget? leadingIcon,
  6. Widget? trailingIcon,
  7. Widget? label,
  8. String? hintText,
  9. String? helperText,
  10. Widget? selectedTrailingIcon,
  11. bool enableFilter = false,
  12. bool enableSearch = true,
  13. TextInputType? keyboardType,
  14. TextStyle? textStyle,
  15. TextAlign textAlign = TextAlign.start,
  16. Object? inputDecorationTheme,
  17. DropdownMenuDecorationBuilder? decorationBuilder,
  18. MenuStyle? menuStyle,
  19. TextEditingController? controller,
  20. T? initialSelection,
  21. ValueChanged<T?>? onSelected,
  22. FocusNode? focusNode,
  23. bool? requestFocusOnTap,
  24. EdgeInsetsGeometry? expandedInsets,
  25. Offset? alignmentOffset,
  26. FilterCallback<T>? filterCallback,
  27. SearchCallback<T>? searchCallback,
  28. required List<DropdownMenuEntry<T>> dropdownMenuEntries,
  29. List<TextInputFormatter>? inputFormatters,
  30. DropdownMenuCloseBehavior closeBehavior = DropdownMenuCloseBehavior.all,
  31. int maxLines = 1,
  32. TextInputAction? textInputAction,
  33. String? restorationId,
  34. FormFieldSetter<T>? onSaved,
  35. AutovalidateMode autovalidateMode = AutovalidateMode.disabled,
  36. FormFieldValidator<T>? validator,
  37. String? forceErrorText,
  38. FormFieldErrorBuilder? errorBuilder,
})

Creates a DropdownMenu widget that is a FormField.

For a description of the onSaved, validator, or autovalidateMode parameters, see FormField. For the rest, see DropdownMenu.

Implementation

DropdownMenuFormField({
  super.key,
  bool enabled = true,
  double? width,
  double? menuHeight,
  Widget? leadingIcon,
  Widget? trailingIcon,
  Widget? label,
  String? hintText,
  String? helperText,
  Widget? selectedTrailingIcon,
  bool enableFilter = false,
  bool enableSearch = true,
  TextInputType? keyboardType,
  TextStyle? textStyle,
  TextAlign textAlign = TextAlign.start,
  // TODO(bleroux): Clean this up once `InputDecorationTheme` is fully normalized.
  Object? inputDecorationTheme,
  DropdownMenuDecorationBuilder? decorationBuilder,
  MenuStyle? menuStyle,
  this.controller,
  T? initialSelection,
  this.onSelected,
  FocusNode? focusNode,
  bool? requestFocusOnTap,
  EdgeInsetsGeometry? expandedInsets,
  Offset? alignmentOffset,
  FilterCallback<T>? filterCallback,
  SearchCallback<T>? searchCallback,
  required this.dropdownMenuEntries,
  List<TextInputFormatter>? inputFormatters,
  DropdownMenuCloseBehavior closeBehavior = DropdownMenuCloseBehavior.all,
  int maxLines = 1,
  TextInputAction? textInputAction,
  super.restorationId,
  super.onSaved,
  AutovalidateMode autovalidateMode = AutovalidateMode.disabled,
  super.validator,
  super.forceErrorText,
  super.errorBuilder,
}) : super(
       initialValue: initialSelection,
       autovalidateMode: autovalidateMode,
       builder: (FormFieldState<T> field) {
         final state = field as _DropdownMenuFormFieldState<T>;

         InputDecoration effectiveDecorationBuilder(
           BuildContext context,
           MenuController menuController,
         ) {
           final InputDecoration decoration =
               decorationBuilder?.call(context, menuController) ?? const InputDecoration();
           final InputDecoration decorationWithLabels = decoration.copyWith(
             label: label,
             hintText: hintText,
             helperText: helperText,
           );

           final String? errorText = state.errorText;
           if (errorText == null) {
             return decorationWithLabels;
           }

           return errorBuilder != null
               ? decorationWithLabels.copyWith(error: errorBuilder(state.context, errorText))
               : decorationWithLabels.copyWith(errorText: errorText);
         }

         return UnmanagedRestorationScope(
           bucket: field.bucket,
           child: DropdownMenu<T>(
             restorationId: restorationId,
             enabled: enabled,
             width: width,
             menuHeight: menuHeight,
             leadingIcon: leadingIcon,
             trailingIcon: trailingIcon,
             selectedTrailingIcon: selectedTrailingIcon,
             enableFilter: enableFilter,
             enableSearch: enableSearch,
             keyboardType: keyboardType,
             textStyle: textStyle,
             textAlign: textAlign,
             inputDecorationTheme: inputDecorationTheme,
             decorationBuilder: effectiveDecorationBuilder,
             menuStyle: menuStyle,
             controller: state.textFieldController,
             initialSelection: state.value,
             onSelected: field.didChange,
             focusNode: focusNode,
             requestFocusOnTap: requestFocusOnTap,
             expandedInsets: expandedInsets,
             alignmentOffset: alignmentOffset,
             filterCallback: filterCallback,
             searchCallback: searchCallback,
             inputFormatters: inputFormatters,
             closeBehavior: closeBehavior,
             dropdownMenuEntries: dropdownMenuEntries,
             maxLines: maxLines,
             textInputAction: textInputAction,
           ),
         );
       },
     );