From 7702ec0ee073b28ea489bc5071dcda2b390f5c72 Mon Sep 17 00:00:00 2001 From: Matthew Larson Date: Wed, 8 Jun 2022 08:24:09 -0500 Subject: [PATCH 1/3] Add onCancel handler --- README.md | 2 ++ example/pubspec.lock | 23 +++++++------------ .../multi_select_bottom_sheet.dart | 15 ++++++++++-- lib/dialog/mult_select_dialog.dart | 6 ++++- lib/util/multi_select_actions.dart | 10 +++++++- pubspec.lock | 23 +++++++------------ 6 files changed, 45 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 59f4b04..adb6fd3 100644 --- a/README.md +++ b/README.md @@ -191,6 +191,7 @@ void _startAnimation(ScrollController controller) { | `listType` | MultiSelectListType | Change the listType. Can be either  `MultiSelectListType.LIST` or `MultiSelectListType.CHIP` | | `onSelectionChanged` | Function(List\) | Fires when an item is selected or unselected. | | `onConfirm` | Function(List) | Fires when the confirm button is pressed. | +| `onCancel` | Function(List) | 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. | @@ -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\) | Fires when an item is selected or unselected. | | `onConfirm` | Function(List) | Fires when the confirm button is pressed. | +| `onCancel` | Function(List) | 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. | diff --git a/example/pubspec.lock b/example/pubspec.lock index 31e424f..1f58269 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -42,7 +42,7 @@ packages: name: collection url: "https://pub.dartlang.org" source: hosted - version: "1.15.0" + version: "1.16.0" cupertino_icons: dependency: "direct main" description: @@ -56,7 +56,7 @@ packages: name: fake_async url: "https://pub.dartlang.org" source: hosted - version: "1.2.0" + version: "1.3.0" flutter: dependency: "direct main" description: flutter @@ -80,7 +80,7 @@ packages: name: material_color_utilities url: "https://pub.dartlang.org" source: hosted - version: "0.1.3" + version: "0.1.4" meta: dependency: transitive description: @@ -101,7 +101,7 @@ packages: name: path url: "https://pub.dartlang.org" source: hosted - version: "1.8.0" + version: "1.8.1" sky_engine: dependency: transitive description: flutter @@ -113,7 +113,7 @@ packages: name: source_span url: "https://pub.dartlang.org" source: hosted - version: "1.8.1" + version: "1.8.2" stack_trace: dependency: transitive description: @@ -148,20 +148,13 @@ packages: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.4.8" - typed_data: - dependency: transitive - description: - name: typed_data - url: "https://pub.dartlang.org" - source: hosted - version: "1.3.0" + version: "0.4.9" vector_math: dependency: transitive description: name: vector_math url: "https://pub.dartlang.org" source: hosted - version: "2.1.1" + version: "2.1.2" sdks: - dart: ">=2.14.0 <3.0.0" + dart: ">=2.17.0-0 <3.0.0" diff --git a/lib/bottom_sheet/multi_select_bottom_sheet.dart b/lib/bottom_sheet/multi_select_bottom_sheet.dart index 26d797f..adfd8c0 100644 --- a/lib/bottom_sheet/multi_select_bottom_sheet.dart +++ b/lib/bottom_sheet/multi_select_bottom_sheet.dart @@ -21,6 +21,9 @@ class MultiSelectBottomSheet extends StatefulWidget /// Fires when confirm is tapped. final void Function(List)? onConfirm; + /// Fires when cancel is tapped. + final void Function(List)? onCancel; + /// Toggles search functionality. final bool searchable; @@ -85,6 +88,7 @@ class MultiSelectBottomSheet extends StatefulWidget this.title, this.onSelectionChanged, this.onConfirm, + this.onCancel, this.listType, this.cancelText, this.confirmText, @@ -328,7 +332,11 @@ class _MultiSelectBottomSheetState extends State> { Expanded( child: TextButton( onPressed: () { - widget.onCancelTap(context, widget.initialValue); + widget.onCancelTap( + context, + widget.initialValue, + widget.onConfirm, + ); }, child: widget.cancelText ?? Text( @@ -348,7 +356,10 @@ class _MultiSelectBottomSheetState extends State> { child: TextButton( onPressed: () { widget.onConfirmTap( - context, _selectedValues, widget.onConfirm); + context, + _selectedValues, + widget.onConfirm, + ); }, child: widget.confirmText ?? Text( diff --git a/lib/dialog/mult_select_dialog.dart b/lib/dialog/mult_select_dialog.dart index 8d00c2b..ba48cf7 100644 --- a/lib/dialog/mult_select_dialog.dart +++ b/lib/dialog/mult_select_dialog.dart @@ -20,6 +20,9 @@ class MultiSelectDialog extends StatefulWidget with MultiSelectActions { /// Fires when confirm is tapped. final void Function(List)? onConfirm; + /// Fires when cancel is tapped. + final void Function(List)? onCancel; + /// Toggles search functionality. Default is false. final bool searchable; @@ -84,6 +87,7 @@ class MultiSelectDialog extends StatefulWidget with MultiSelectActions { this.title, this.onSelectionChanged, this.onConfirm, + this.onCancel, this.listType, this.searchable = false, this.confirmText, @@ -308,7 +312,7 @@ class _MultiSelectDialogState extends State> { ), ), onPressed: () { - widget.onCancelTap(context, widget.initialValue); + widget.onCancelTap(context, widget.initialValue, widget.onCancel); }, ), TextButton( diff --git a/lib/util/multi_select_actions.dart b/lib/util/multi_select_actions.dart index d8ad5e0..e4f4a7e 100644 --- a/lib/util/multi_select_actions.dart +++ b/lib/util/multi_select_actions.dart @@ -14,8 +14,16 @@ class MultiSelectActions { } /// Pops the dialog from the navigation stack and returns the initially selected values. - void onCancelTap(BuildContext ctx, List initiallySelectedValues) { + void onCancelTap( + BuildContext ctx, + List initiallySelectedValues, + Function(List)? onConfirm, + ) { Navigator.pop(ctx, initiallySelectedValues); + + if (onConfirm != null) { + onConfirm(initiallySelectedValues); + } } /// Pops the dialog from the navigation stack and returns the selected values. diff --git a/pubspec.lock b/pubspec.lock index e4e496b..4714166 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -42,14 +42,14 @@ packages: name: collection url: "https://pub.dartlang.org" source: hosted - version: "1.15.0" + version: "1.16.0" fake_async: dependency: transitive description: name: fake_async url: "https://pub.dartlang.org" source: hosted - version: "1.2.0" + version: "1.3.0" flutter: dependency: "direct main" description: flutter @@ -73,7 +73,7 @@ packages: name: material_color_utilities url: "https://pub.dartlang.org" source: hosted - version: "0.1.3" + version: "0.1.4" meta: dependency: transitive description: @@ -87,7 +87,7 @@ packages: name: path url: "https://pub.dartlang.org" source: hosted - version: "1.8.0" + version: "1.8.1" sky_engine: dependency: transitive description: flutter @@ -99,7 +99,7 @@ packages: name: source_span url: "https://pub.dartlang.org" source: hosted - version: "1.8.1" + version: "1.8.2" stack_trace: dependency: transitive description: @@ -134,20 +134,13 @@ packages: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.4.8" - typed_data: - dependency: transitive - description: - name: typed_data - url: "https://pub.dartlang.org" - source: hosted - version: "1.3.0" + version: "0.4.9" vector_math: dependency: transitive description: name: vector_math url: "https://pub.dartlang.org" source: hosted - version: "2.1.1" + version: "2.1.2" sdks: - dart: ">=2.14.0 <3.0.0" + dart: ">=2.17.0-0 <3.0.0" From baa39fb125878e6186132cebde8a381e3e613bbd Mon Sep 17 00:00:00 2001 From: Matthew Larson Date: Wed, 8 Jun 2022 08:29:06 -0500 Subject: [PATCH 2/3] Add onCancel handler to MultiSelectDialogField --- lib/dialog/multi_select_dialog_field.dart | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/lib/dialog/multi_select_dialog_field.dart b/lib/dialog/multi_select_dialog_field.dart index ed72e6c..56c844d 100644 --- a/lib/dialog/multi_select_dialog_field.dart +++ b/lib/dialog/multi_select_dialog_field.dart @@ -39,6 +39,9 @@ class MultiSelectDialogField extends FormField> { /// Fires when confirm is tapped. final void Function(List) onConfirm; + /// Fires when cancel is tapped. + final void Function(List)? onCancel; + /// Toggles search functionality. final bool searchable; @@ -106,6 +109,7 @@ class MultiSelectDialogField extends FormField> { MultiSelectDialogField({ required this.items, required this.onConfirm, + this.onCancel, this.title, this.buttonText, this.buttonIcon, @@ -154,6 +158,7 @@ class MultiSelectDialogField extends FormField> { decoration: decoration, listType: listType, onConfirm: onConfirm, + onCancel: onCancel, onSelectionChanged: onSelectionChanged, initialValue: initialValue, searchable: searchable, @@ -192,6 +197,7 @@ class _MultiSelectDialogFieldView extends StatefulWidget { final MultiSelectChipDisplay? chipDisplay; final List? initialValue; final void Function(List)? onConfirm; + final void Function(List)? onCancel; final bool? searchable; final Text? confirmText; final Text? cancelText; @@ -222,6 +228,7 @@ class _MultiSelectDialogFieldView extends StatefulWidget { this.decoration, this.onSelectionChanged, this.onConfirm, + this.onCancel, this.chipDisplay, this.initialValue, this.searchable, @@ -256,6 +263,7 @@ class _MultiSelectDialogFieldView extends StatefulWidget { decoration = field.decoration, onSelectionChanged = field.onSelectionChanged, onConfirm = field.onConfirm, + onCancel = field.onCancel, chipDisplay = field.chipDisplay, initialValue = field.initialValue, searchable = field.searchable, @@ -390,6 +398,13 @@ class __MultiSelectDialogFieldViewState _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); + }, ); }, ); From 497d70709c96803b5a49fcd8250ff7a7cac24059 Mon Sep 17 00:00:00 2001 From: Matthew Larson Date: Wed, 8 Jun 2022 08:34:14 -0500 Subject: [PATCH 3/3] Add onCancel to MultiSelectDialogField example --- example/lib/main.dart | 3 +++ example/pubspec.lock | 23 +++++++++++++++-------- pubspec.lock | 23 +++++++++++++++-------- 3 files changed, 33 insertions(+), 16 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index 104cb60..f6e32a7 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -121,6 +121,9 @@ class _MyHomePageState extends State { fontSize: 16, ), ), + onCancel: (results) { + print(results); + }, onConfirm: (results) { //_selectedAnimals = results; }, diff --git a/example/pubspec.lock b/example/pubspec.lock index 1f58269..31e424f 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -42,7 +42,7 @@ packages: name: collection url: "https://pub.dartlang.org" source: hosted - version: "1.16.0" + version: "1.15.0" cupertino_icons: dependency: "direct main" description: @@ -56,7 +56,7 @@ packages: name: fake_async url: "https://pub.dartlang.org" source: hosted - version: "1.3.0" + version: "1.2.0" flutter: dependency: "direct main" description: flutter @@ -80,7 +80,7 @@ packages: name: material_color_utilities url: "https://pub.dartlang.org" source: hosted - version: "0.1.4" + version: "0.1.3" meta: dependency: transitive description: @@ -101,7 +101,7 @@ packages: name: path url: "https://pub.dartlang.org" source: hosted - version: "1.8.1" + version: "1.8.0" sky_engine: dependency: transitive description: flutter @@ -113,7 +113,7 @@ packages: name: source_span url: "https://pub.dartlang.org" source: hosted - version: "1.8.2" + version: "1.8.1" stack_trace: dependency: transitive description: @@ -148,13 +148,20 @@ packages: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.4.9" + version: "0.4.8" + typed_data: + dependency: transitive + description: + name: typed_data + url: "https://pub.dartlang.org" + source: hosted + version: "1.3.0" vector_math: dependency: transitive description: name: vector_math url: "https://pub.dartlang.org" source: hosted - version: "2.1.2" + version: "2.1.1" sdks: - dart: ">=2.17.0-0 <3.0.0" + dart: ">=2.14.0 <3.0.0" diff --git a/pubspec.lock b/pubspec.lock index 4714166..e4e496b 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -42,14 +42,14 @@ packages: name: collection url: "https://pub.dartlang.org" source: hosted - version: "1.16.0" + version: "1.15.0" fake_async: dependency: transitive description: name: fake_async url: "https://pub.dartlang.org" source: hosted - version: "1.3.0" + version: "1.2.0" flutter: dependency: "direct main" description: flutter @@ -73,7 +73,7 @@ packages: name: material_color_utilities url: "https://pub.dartlang.org" source: hosted - version: "0.1.4" + version: "0.1.3" meta: dependency: transitive description: @@ -87,7 +87,7 @@ packages: name: path url: "https://pub.dartlang.org" source: hosted - version: "1.8.1" + version: "1.8.0" sky_engine: dependency: transitive description: flutter @@ -99,7 +99,7 @@ packages: name: source_span url: "https://pub.dartlang.org" source: hosted - version: "1.8.2" + version: "1.8.1" stack_trace: dependency: transitive description: @@ -134,13 +134,20 @@ packages: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.4.9" + version: "0.4.8" + typed_data: + dependency: transitive + description: + name: typed_data + url: "https://pub.dartlang.org" + source: hosted + version: "1.3.0" vector_math: dependency: transitive description: name: vector_math url: "https://pub.dartlang.org" source: hosted - version: "2.1.2" + version: "2.1.1" sdks: - dart: ">=2.17.0-0 <3.0.0" + dart: ">=2.14.0 <3.0.0"