Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReactiveDropdownMenuField #465

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 124 additions & 0 deletions lib/src/widgets/reactive_dropdown_menu_field.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import 'package:collection/collection.dart';
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> {
final List<DropdownMenuEntry<T>> _dropdownMenuEntries;

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,
Widget? selectedTrailingIcon,
bool enableFilter = false,
bool enableSearch = true,
TextStyle? textStyle,
TextAlign textAlign = TextAlign.start,
InputDecorationTheme? inputDecorationTheme,
MenuStyle? menuStyle,
ValueChanged<FormControl<T?>>? onSelected,
bool? requestFocusOnTap,
EdgeInsets? expandedInsets,
FilterCallback<T>? filterCallback,
SearchCallback<T>? searchCallback,
List<TextInputFormatter>? inputFormatters,
}) : _dropdownMenuEntries = dropdownMenuEntries,
super(
builder: (state) {
final field = state as ReactiveDropdownMenuFieldState<ReactiveDropdownMenuField<T>, T, T>;

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: field.errorText,
selectedTrailingIcon: selectedTrailingIcon,
enableFilter: enableFilter,
enableSearch: enableSearch,
textStyle: textStyle,
textAlign: textAlign,
inputDecorationTheme: inputDecorationTheme,
menuStyle: menuStyle,
controller: field._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,
);
},
);

@override
ReactiveDropdownMenuFieldState<ReactiveDropdownMenuField<T>, T, T> createState() =>
ReactiveDropdownMenuFieldState<ReactiveDropdownMenuField<T>, T, T>();
}

class ReactiveDropdownMenuFieldState<W extends ReactiveDropdownMenuField<T>, T, V>
extends ReactiveFocusableFormFieldState<T, T> {
final TextEditingController _controller = TextEditingController();

W get _reactiveWidget => widget as W;

@override
void dispose() {
_controller.removeListener(_ensureValueIsInEntries);
_controller.dispose();

super.dispose();
}

@override
void initState() {
super.initState();

_controller.addListener(_ensureValueIsInEntries);
}

void _ensureValueIsInEntries() {
final match = _reactiveWidget._dropdownMenuEntries
.firstWhereOrNull((item) => item.label.toLowerCase() == _controller.text.toLowerCase());

if (match != null && match.value != value) {
didChange(match.value);
}

if (match == null && value != null) {
didChange(null);
}
}
}