Skip to content

Commit

Permalink
Merge pull request #276 from AhmedLSayed9/enhance/always_call_on_chan…
Browse files Browse the repository at this point in the history
…ged_when_tapping_enabled_item

Always call on changed when tapping enabled item
  • Loading branch information
AhmedLSayed9 committed May 16, 2024
2 parents ded4182 + e3bfb35 commit b9c173d
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 70 deletions.
64 changes: 31 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -453,52 +453,50 @@ Widget build(BuildContext context) {
return DropdownItem(
value: item,
height: 40,
//disable default onTap to avoid closing menu when selecting an item
enabled: false,
closeOnTap: false,
child: ValueListenableBuilder<List<String>>(
valueListenable: multiValueListenable,
builder: (context, multiValue, _) {
final isSelected = multiValue.contains(item);
return InkWell(
onTap: () {
if (item == 'All') {
isSelected
? multiValueListenable.value = []
: multiValueListenable.value = List.from(items);
} else {
multiValueListenable.value = isSelected
? ([...multiValue]..remove(item))
: [...multiValue, item];
}
},
child: Container(
height: double.infinity,
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: Row(
children: [
if (isSelected)
const Icon(Icons.check_box_outlined)
else
const Icon(Icons.check_box_outline_blank),
const SizedBox(width: 16),
Expanded(
child: Text(
item,
style: const TextStyle(
fontSize: 14,
),
return Container(
height: double.infinity,
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: Row(
children: [
if (isSelected)
const Icon(Icons.check_box_outlined)
else
const Icon(Icons.check_box_outline_blank),
const SizedBox(width: 16),
Expanded(
child: Text(
item,
style: const TextStyle(
fontSize: 14,
),
),
],
),
),
],
),
);
},
),
);
}).toList(),
multiValueListenable: multiValueListenable,
onChanged: (value) {},
onChanged: (value) {
final multiValue = multiValueListenable.value;
final isSelected = multiValue.contains(value);
if (value == 'All') {
isSelected
? multiValueListenable.value = []
: multiValueListenable.value = List.from(items);
} else {
multiValueListenable.value = isSelected
? ([...multiValue]..remove(value))
: [...multiValue, value!];
}
},
selectedItemBuilder: (context) {
return items.map(
(item) {
Expand Down
5 changes: 1 addition & 4 deletions packages/dropdown_button2/lib/src/dropdown_button2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,7 @@ class _DropdownButton2State<T> extends State<DropdownButton2<T>>
buttonRect: _rect,
selectedIndex: _selectedIndex ?? 0,
isNoSelectedItem: _selectedIndex == null,
onChanged: widget.onChanged,
capturedThemes:
InheritedTheme.capture(from: context, to: navigator.context),
style: _textStyle!,
Expand Down Expand Up @@ -611,10 +612,6 @@ class _DropdownButton2State<T> extends State<DropdownButton2<T>>
_removeDropdownRoute();
_isMenuOpen.value = false;
widget.onMenuStateChange?.call(false);
if (!mounted || newValue == null) {
return;
}
widget.onChanged?.call(newValue.result);
});

widget.onMenuStateChange?.call(true);
Expand Down
1 change: 1 addition & 0 deletions packages/dropdown_button2/lib/src/dropdown_menu_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ class _DropdownItemButtonState<T> extends State<_DropdownItemButton<T>> {
final DropdownItem<T> dropdownItem = widget.route.items[widget.itemIndex];

dropdownItem.onTap?.call();
widget.route.onChanged?.call(dropdownItem.value);

if (dropdownItem.closeOnTap) {
Navigator.pop(
Expand Down
2 changes: 2 additions & 0 deletions packages/dropdown_button2/lib/src/dropdown_route.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class _DropdownRoute<T> extends PopupRoute<_DropdownRouteResult<T>> {
required this.buttonRect,
required this.selectedIndex,
required this.isNoSelectedItem,
required this.onChanged,
required this.capturedThemes,
required this.style,
required this.barrierDismissible,
Expand All @@ -29,6 +30,7 @@ class _DropdownRoute<T> extends PopupRoute<_DropdownRouteResult<T>> {
final ValueNotifier<Rect?> buttonRect;
final int selectedIndex;
final bool isNoSelectedItem;
final ValueChanged<T?>? onChanged;
final CapturedThemes capturedThemes;
final TextStyle style;
final FocusNode parentFocusNode;
Expand Down
64 changes: 31 additions & 33 deletions packages/dropdown_button2_test/lib/src/multi_select_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,52 +36,50 @@ class _MultiSelectExampleState extends State<MultiSelectExample> {
return DropdownItem(
value: item,
height: 40,
//disable default onTap to avoid closing menu when selecting an item
enabled: false,
closeOnTap: false,
child: ValueListenableBuilder<List<String>>(
valueListenable: multiValueListenable,
builder: (context, multiValue, _) {
final isSelected = multiValue.contains(item);
return InkWell(
onTap: () {
if (item == 'All') {
isSelected
? multiValueListenable.value = []
: multiValueListenable.value = List.from(items);
} else {
multiValueListenable.value = isSelected
? ([...multiValue]..remove(item))
: [...multiValue, item];
}
},
child: Container(
height: double.infinity,
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: Row(
children: [
if (isSelected)
const Icon(Icons.check_box_outlined)
else
const Icon(Icons.check_box_outline_blank),
const SizedBox(width: 16),
Expanded(
child: Text(
item,
style: const TextStyle(
fontSize: 14,
),
return Container(
height: double.infinity,
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: Row(
children: [
if (isSelected)
const Icon(Icons.check_box_outlined)
else
const Icon(Icons.check_box_outline_blank),
const SizedBox(width: 16),
Expanded(
child: Text(
item,
style: const TextStyle(
fontSize: 14,
),
),
],
),
),
],
),
);
},
),
);
}).toList(),
multiValueListenable: multiValueListenable,
onChanged: (value) {},
onChanged: (value) {
final multiValue = multiValueListenable.value;
final isSelected = multiValue.contains(value);
if (value == 'All') {
isSelected
? multiValueListenable.value = []
: multiValueListenable.value = List.from(items);
} else {
multiValueListenable.value = isSelected
? ([...multiValue]..remove(value))
: [...multiValue, value!];
}
},
selectedItemBuilder: (context) {
return items.map(
(item) {
Expand Down

0 comments on commit b9c173d

Please sign in to comment.