Skip to content

Commit

Permalink
ReactiveDropdownMenuField
Browse files Browse the repository at this point in the history
  • Loading branch information
BenjiFarquhar committed Sep 19, 2024
1 parent fe8b030 commit e25f27b
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions lib/src/widgets/reactive_dropdown_menu_field.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:reactive_forms/reactive_forms.dart';

/// A reactive widget that wraps a [DropdownMenu].
class ReactiveDropdownMenuField<T> extends ReactiveFocusableFormField<T, T> {
ReactiveDropdownMenuField({
required List<DropdownMenuEntry<T>> dropdownMenuEntries,
super.key,
super.formControlName,
super.formControl,
super.focusNode,
super.validationMessages,
super.showErrors,
bool readOnly = false,
bool enabled = true,
Widget? label,
double? width,
double? menuHeight,
Widget? leadingIcon,
Widget? trailingIcon,
String? hintText,
String? helperText,
String? errorText,
Widget? selectedTrailingIcon,
bool enableFilter = false,
bool enableSearch = true,
TextStyle? textStyle,
TextAlign textAlign = TextAlign.start,
InputDecorationTheme? inputDecorationTheme,
MenuStyle? menuStyle,
TextEditingController? controller,
ValueChanged<FormControl<T?>>? onSelected,
bool? requestFocusOnTap,
EdgeInsets? expandedInsets,
FilterCallback<T>? filterCallback,
SearchCallback<T>? searchCallback,
List<TextInputFormatter>? inputFormatters,
}) : super(
builder: (field) {
var effectiveValue = field.value;
if (effectiveValue != null && !dropdownMenuEntries.any((item) => item.value == effectiveValue)) {
effectiveValue = null;
}

return DropdownMenu<T>(
enabled: enabled,
width: width,
menuHeight: menuHeight,
leadingIcon: leadingIcon,
trailingIcon: trailingIcon,
label: label,
hintText: hintText,
helperText: helperText,
errorText: errorText,
selectedTrailingIcon: selectedTrailingIcon,
enableFilter: enableFilter,
enableSearch: enableSearch,
textStyle: textStyle,
textAlign: textAlign,
inputDecorationTheme: inputDecorationTheme,
menuStyle: menuStyle,
controller: controller,
initialSelection: effectiveValue,
onSelected: readOnly || field.control.disabled
? null
: (value) {
field.didChange(value);
onSelected?.call(field.control);
},
focusNode: field.focusNode,
requestFocusOnTap: requestFocusOnTap,
expandedInsets: expandedInsets,
filterCallback: filterCallback,
searchCallback: searchCallback,
dropdownMenuEntries: dropdownMenuEntries,
inputFormatters: inputFormatters,
);
},
);
}

0 comments on commit e25f27b

Please sign in to comment.