Skip to content

Commit 1ea0726

Browse files
fix: improve dropdown field
- Fix assert error - Replace dropdown widget
1 parent c48671f commit 1ea0726

File tree

1 file changed

+41
-41
lines changed

1 file changed

+41
-41
lines changed

lib/src/fields/form_builder_dropdown.dart

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -254,47 +254,39 @@ class FormBuilderDropdown<T> extends FormBuilderFieldDecoration<T> {
254254
builder: (FormFieldState<T?> field) {
255255
final state = field as _FormBuilderDropdownState<T>;
256256

257-
void changeValue(T? value) {
258-
state.didChange(value);
259-
}
260-
261-
return InputDecorator(
257+
final hasValue = items.map((e) => e.value).contains(field.value);
258+
return DropdownButtonFormField<T>(
259+
isExpanded: isExpanded,
262260
decoration: state.decoration,
263-
isEmpty: state.value == null,
264-
child: DropdownButtonHideUnderline(
265-
child: DropdownButton<T>(
266-
isExpanded: isExpanded,
267-
items: items,
268-
value: field.value,
269-
style: style,
270-
isDense: isDense,
271-
disabledHint: field.value != null
272-
? (items
273-
.firstWhereOrNull((dropDownItem) =>
274-
dropDownItem.value == field.value)
275-
?.child ??
276-
Text(field.value.toString()))
277-
: disabledHint,
278-
elevation: elevation,
279-
iconSize: iconSize,
280-
icon: icon,
281-
iconDisabledColor: iconDisabledColor,
282-
iconEnabledColor: iconEnabledColor,
283-
onChanged:
284-
state.enabled ? (value) => changeValue(value) : null,
285-
onTap: onTap,
286-
focusNode: state.effectiveFocusNode,
287-
autofocus: autofocus,
288-
dropdownColor: dropdownColor,
289-
focusColor: focusColor,
290-
itemHeight: itemHeight,
291-
selectedItemBuilder: selectedItemBuilder,
292-
menuMaxHeight: menuMaxHeight,
293-
borderRadius: borderRadius,
294-
enableFeedback: enableFeedback,
295-
alignment: alignment,
296-
),
297-
),
261+
items: items,
262+
value: hasValue ? field.value : null,
263+
style: style,
264+
isDense: isDense,
265+
disabledHint: field.value != null
266+
? (items
267+
.firstWhereOrNull((dropDownItem) =>
268+
dropDownItem.value == field.value)
269+
?.child ??
270+
Text(field.value.toString()))
271+
: disabledHint,
272+
elevation: elevation,
273+
iconSize: iconSize,
274+
icon: icon,
275+
iconDisabledColor: iconDisabledColor,
276+
iconEnabledColor: iconEnabledColor,
277+
onChanged:
278+
state.enabled ? (T? value) => state.didChange(value) : null,
279+
onTap: onTap,
280+
focusNode: state.effectiveFocusNode,
281+
autofocus: autofocus,
282+
dropdownColor: dropdownColor,
283+
focusColor: focusColor,
284+
itemHeight: itemHeight,
285+
selectedItemBuilder: selectedItemBuilder,
286+
menuMaxHeight: menuMaxHeight,
287+
borderRadius: borderRadius,
288+
enableFeedback: enableFeedback,
289+
alignment: alignment,
298290
);
299291
},
300292
);
@@ -305,4 +297,12 @@ class FormBuilderDropdown<T> extends FormBuilderFieldDecoration<T> {
305297
}
306298

307299
class _FormBuilderDropdownState<T>
308-
extends FormBuilderFieldDecorationState<FormBuilderDropdown<T>, T> {}
300+
extends FormBuilderFieldDecorationState<FormBuilderDropdown<T>, T> {
301+
@override
302+
void didUpdateWidget(covariant FormBuilderDropdown<T> oldWidget) {
303+
super.didUpdateWidget(oldWidget);
304+
if (widget.items != oldWidget.items) {
305+
setValue(initialValue);
306+
}
307+
}
308+
}

0 commit comments

Comments
 (0)