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

Adding onCancel handler (new attempt) #107

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ void _startAnimation(ScrollController controller) {
| `listType` | MultiSelectListType | Change the listType. Can be either  `MultiSelectListType.LIST` or `MultiSelectListType.CHIP` |
| `onSelectionChanged` | Function(List\<V>) | Fires when an item is selected or unselected. |
| `onConfirm` | Function(List<V>) | Fires when the confirm button is pressed. |
| `onCancel` | Function(List<V>) | Fires when the cancel button is pressed. |
| `searchable` | bool | Enables search functionality within the dialog. |
| `searchHintStyle` | TextStyle | Style the text of the search hint. |
| `searchIcon` | Icon | The icon button that shows the search field. |
Expand Down Expand Up @@ -235,6 +236,7 @@ MultiSelectDialogField has all the parameters of MultiSelectDialog plus these ex
| `minChildSize` | double | Set the minimum height threshold of the BottomSheet before it closes. Default is 0.3 |
| `onSelectionChanged` | Function(List\<V>) | Fires when an item is selected or unselected. |
| `onConfirm` | Function(List<V>) | Fires when the confirm button is pressed. |
| `onCancel` | Function(List<V>) | Fires when the cancel button is pressed. |
| `searchable` | bool | Toggle search functionality within the BottomSheet. |
| `searchHint` | String | Set the placeholder text of the search field. |
| `searchHintStyle` | TextStyle | Style the text of the search hint. |
Expand Down
3 changes: 3 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ class _MyHomePageState extends State<MyHomePage> {
fontSize: 16,
),
),
onCancel: (results) {
print(results);
},
onConfirm: (results) {
//_selectedAnimals = results;
},
Expand Down
15 changes: 13 additions & 2 deletions lib/bottom_sheet/multi_select_bottom_sheet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class MultiSelectBottomSheet<T> extends StatefulWidget
/// Fires when confirm is tapped.
final void Function(List<T>)? onConfirm;

/// Fires when cancel is tapped.
final void Function(List<T>)? onCancel;

/// Toggles search functionality.
final bool searchable;

Expand Down Expand Up @@ -85,6 +88,7 @@ class MultiSelectBottomSheet<T> extends StatefulWidget
this.title,
this.onSelectionChanged,
this.onConfirm,
this.onCancel,
this.listType,
this.cancelText,
this.confirmText,
Expand Down Expand Up @@ -328,7 +332,11 @@ class _MultiSelectBottomSheetState<T> extends State<MultiSelectBottomSheet<T>> {
Expanded(
child: TextButton(
onPressed: () {
widget.onCancelTap(context, widget.initialValue);
widget.onCancelTap(
context,
widget.initialValue,
widget.onConfirm,
);
},
child: widget.cancelText ??
Text(
Expand All @@ -348,7 +356,10 @@ class _MultiSelectBottomSheetState<T> extends State<MultiSelectBottomSheet<T>> {
child: TextButton(
onPressed: () {
widget.onConfirmTap(
context, _selectedValues, widget.onConfirm);
context,
_selectedValues,
widget.onConfirm,
);
},
child: widget.confirmText ??
Text(
Expand Down
6 changes: 5 additions & 1 deletion lib/dialog/mult_select_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class MultiSelectDialog<T> extends StatefulWidget with MultiSelectActions<T> {
/// Fires when confirm is tapped.
final void Function(List<T>)? onConfirm;

/// Fires when cancel is tapped.
final void Function(List<T>)? onCancel;

/// Toggles search functionality. Default is false.
final bool searchable;

Expand Down Expand Up @@ -84,6 +87,7 @@ class MultiSelectDialog<T> extends StatefulWidget with MultiSelectActions<T> {
this.title,
this.onSelectionChanged,
this.onConfirm,
this.onCancel,
this.listType,
this.searchable = false,
this.confirmText,
Expand Down Expand Up @@ -308,7 +312,7 @@ class _MultiSelectDialogState<T> extends State<MultiSelectDialog<T>> {
),
),
onPressed: () {
widget.onCancelTap(context, widget.initialValue);
widget.onCancelTap(context, widget.initialValue, widget.onCancel);
},
),
TextButton(
Expand Down
15 changes: 15 additions & 0 deletions lib/dialog/multi_select_dialog_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ class MultiSelectDialogField<V> extends FormField<List<V>> {
/// Fires when confirm is tapped.
final void Function(List<V>) onConfirm;

/// Fires when cancel is tapped.
final void Function(List<V>)? onCancel;

/// Toggles search functionality.
final bool searchable;

Expand Down Expand Up @@ -106,6 +109,7 @@ class MultiSelectDialogField<V> extends FormField<List<V>> {
MultiSelectDialogField({
required this.items,
required this.onConfirm,
this.onCancel,
this.title,
this.buttonText,
this.buttonIcon,
Expand Down Expand Up @@ -154,6 +158,7 @@ class MultiSelectDialogField<V> extends FormField<List<V>> {
decoration: decoration,
listType: listType,
onConfirm: onConfirm,
onCancel: onCancel,
onSelectionChanged: onSelectionChanged,
initialValue: initialValue,
searchable: searchable,
Expand Down Expand Up @@ -192,6 +197,7 @@ class _MultiSelectDialogFieldView<V> extends StatefulWidget {
final MultiSelectChipDisplay<V>? chipDisplay;
final List<V>? initialValue;
final void Function(List<V>)? onConfirm;
final void Function(List<V>)? onCancel;
final bool? searchable;
final Text? confirmText;
final Text? cancelText;
Expand Down Expand Up @@ -222,6 +228,7 @@ class _MultiSelectDialogFieldView<V> extends StatefulWidget {
this.decoration,
this.onSelectionChanged,
this.onConfirm,
this.onCancel,
this.chipDisplay,
this.initialValue,
this.searchable,
Expand Down Expand Up @@ -256,6 +263,7 @@ class _MultiSelectDialogFieldView<V> extends StatefulWidget {
decoration = field.decoration,
onSelectionChanged = field.onSelectionChanged,
onConfirm = field.onConfirm,
onCancel = field.onCancel,
chipDisplay = field.chipDisplay,
initialValue = field.initialValue,
searchable = field.searchable,
Expand Down Expand Up @@ -390,6 +398,13 @@ class __MultiSelectDialogFieldViewState<V>
_selectedItems = selected;
if (widget.onConfirm != null) widget.onConfirm!(selected);
},
onCancel: (selected) {
if (widget.state != null) {
widget.state!.didChange(selected);
}
_selectedItems = selected;
if (widget.onCancel != null) widget.onCancel!(selected);
},
);
},
);
Expand Down
10 changes: 9 additions & 1 deletion lib/util/multi_select_actions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,16 @@ class MultiSelectActions<T> {
}

/// Pops the dialog from the navigation stack and returns the initially selected values.
void onCancelTap(BuildContext ctx, List<T> initiallySelectedValues) {
void onCancelTap(
BuildContext ctx,
List<T> initiallySelectedValues,
Function(List<T>)? onConfirm,
) {
Navigator.pop(ctx, initiallySelectedValues);

if (onConfirm != null) {
onConfirm(initiallySelectedValues);
}
}

/// Pops the dialog from the navigation stack and returns the selected values.
Expand Down