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

Always call on changed when tapping enabled item #276

Merged
Merged
Show file tree
Hide file tree
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
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
Loading