Skip to content

Commit

Permalink
assert
Browse files Browse the repository at this point in the history
  • Loading branch information
mikecoomber committed Apr 16, 2024
1 parent d7b101c commit 25f216c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
6 changes: 3 additions & 3 deletions example/lib/pages/components/dropdown_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class _DropdownExampleState extends State<DropdownExample> {
selectedItem = value;
});
},
selectedItem: selectedItem,
value: selectedItem,
items: items,
),
Text('Selected item : ${selectedItem}')
Expand All @@ -57,12 +57,12 @@ class _DropdownExampleState extends State<DropdownExample> {
),
ZetaDropdown(
items: items,
selectedItem: selectedItem,
value: selectedItem,
type: ZetaDropdownMenuType.checkbox,
),
ZetaDropdown(
items: items,
selectedItem: selectedItem,
value: selectedItem,
size: ZetaDropdownSize.mini,
type: ZetaDropdownMenuType.radio,
),
Expand Down
18 changes: 10 additions & 8 deletions lib/src/components/dropdown/dropdown.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,21 @@ class ZetaDropdown<T> extends StatefulWidget {
const ZetaDropdown({
required this.items,
this.onChange,
this.selectedItem,
this.value,
this.rounded = true,
this.disabled = false,
this.type = ZetaDropdownMenuType.standard,
this.size = ZetaDropdownSize.standard,
super.key,
});
}) : assert(items.length > 0, 'Items must be greater than 0.');

/// The items displayed in the dropdown.
final List<ZetaDropdownItem<T>> items;

/// The currently selected item.
final T? selectedItem;
/// The value of the selected item.
///
/// If no [ZetaDropdownItem] in [items] has a matching value, the first item in [items] will be set as the selected item.
final T? value;

/// Called with the selected value whenever the dropdown is changed.
final ValueSetter<T>? onChange;
Expand Down Expand Up @@ -97,7 +99,7 @@ class ZetaDropdown<T> extends StatefulWidget {
..add(EnumProperty<ZetaDropdownMenuType>('leadingType', type))
..add(DiagnosticsProperty<bool>('rounded', rounded))
..add(IterableProperty<ZetaDropdownItem<T>>('items', items))
..add(DiagnosticsProperty<T?>('selectedItem', selectedItem))
..add(DiagnosticsProperty<T?>('selectedItem', value))
..add(ObjectFlagProperty<ValueSetter<T>?>.has('onChange', onChange))
..add(EnumProperty<ZetaDropdownSize>('size', size))
..add(DiagnosticsProperty<bool>('disabled', disabled));
Expand Down Expand Up @@ -125,7 +127,7 @@ class _ZetaDropDownState<T> extends State<ZetaDropdown<T>> {
@override
void didUpdateWidget(ZetaDropdown<T> oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.selectedItem != widget.selectedItem) {
if (oldWidget.value != widget.value) {
setState(_setSelectedItem);
}
if (widget.disabled) {
Expand All @@ -139,9 +141,9 @@ class _ZetaDropDownState<T> extends State<ZetaDropdown<T>> {

void _setSelectedItem() {
try {
_selectedItem = widget.items.firstWhere((item) => item.value == widget.selectedItem);
_selectedItem = widget.items.firstWhere((item) => item.value == widget.value);
} catch (e) {
_selectedItem = null;
_selectedItem = widget.items.first;
}
}

Expand Down

0 comments on commit 25f216c

Please sign in to comment.