diff --git a/example/lib/main.dart b/example/lib/main.dart index 104cb60..da34a88 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -99,6 +99,7 @@ class _MyHomePageState extends State { // Rounded blue MultiSelectDialogField //################################################################################################ MultiSelectDialogField( + isSingleSelect: true, items: _items, title: Text("Animals"), selectedColor: Colors.blue, diff --git a/lib/dialog/mult_select_dialog.dart b/lib/dialog/mult_select_dialog.dart index 433539f..5343bd1 100644 --- a/lib/dialog/mult_select_dialog.dart +++ b/lib/dialog/mult_select_dialog.dart @@ -78,6 +78,9 @@ class MultiSelectDialog extends StatefulWidget with MultiSelectActions { /// Set the color of the check in the checkbox final Color? checkColor; + /// Enable single choice + final bool? isSingleSelect; + MultiSelectDialog({ required this.items, required this.initialValue, @@ -103,6 +106,7 @@ class MultiSelectDialog extends StatefulWidget with MultiSelectActions { this.selectedItemsTextStyle, this.separateSelectedItems = false, this.checkColor, + this.isSingleSelect, }); @override @@ -154,6 +158,15 @@ class _MultiSelectDialogState extends State> { controlAffinity: ListTileControlAffinity.leading, onChanged: (checked) { setState(() { + if (widget.isSingleSelect != null) { + if (widget.isSingleSelect!) { + if (_selectedValues.length > 0) { + _selectedValues = widget.onItemCheckedChange( + _selectedValues, item.value, checked!); + } + Navigator.pop(context); + } + } _selectedValues = widget.onItemCheckedChange( _selectedValues, item.value, checked!); @@ -197,11 +210,7 @@ class _MultiSelectDialogState extends State> { ), selected: item.selected, onSelected: (checked) { - if (checked) { - item.selected = true; - } else { - item.selected = false; - } + item.selected = checked; setState(() { _selectedValues = widget.onItemCheckedChange( _selectedValues, item.value, checked); diff --git a/lib/dialog/multi_select_dialog_field.dart b/lib/dialog/multi_select_dialog_field.dart index b229a4d..ffc9a8a 100644 --- a/lib/dialog/multi_select_dialog_field.dart +++ b/lib/dialog/multi_select_dialog_field.dart @@ -106,6 +106,9 @@ class MultiSelectDialogField extends FormField> { final GlobalKey? key; FormFieldState>? state; + /// Enable single choice + final bool? isSingleSelect; + MultiSelectDialogField({ required this.items, required this.onConfirm, @@ -141,6 +144,7 @@ class MultiSelectDialogField extends FormField> { this.initialValue = const [], this.autovalidateMode = AutovalidateMode.disabled, this.key, + this.isSingleSelect, }) : super( key: key, onSaved: onSaved, @@ -181,7 +185,8 @@ class MultiSelectDialogField extends FormField> { checkColor: checkColor, isDismissible: isDismissible, ); - return _MultiSelectDialogFieldView._withState(field, state); + return _MultiSelectDialogFieldView._withState( + field, state, isSingleSelect); }); } @@ -216,6 +221,7 @@ class _MultiSelectDialogFieldView extends StatefulWidget { final TextStyle? searchHintStyle; final bool separateSelectedItems; final Color? checkColor; + final bool? isSingleSelect; final bool isDismissible; FormFieldState>? state; @@ -250,11 +256,12 @@ class _MultiSelectDialogFieldView extends StatefulWidget { this.separateSelectedItems = false, this.checkColor, required this.isDismissible, + this.isSingleSelect, }); /// This constructor allows a FormFieldState to be passed in. Called by MultiSelectDialogField. - _MultiSelectDialogFieldView._withState( - _MultiSelectDialogFieldView field, FormFieldState> state) + _MultiSelectDialogFieldView._withState(_MultiSelectDialogFieldView field, + FormFieldState> state, this.isSingleSelect) : items = field.items, title = field.title, buttonText = field.buttonText, @@ -382,6 +389,7 @@ class __MultiSelectDialogFieldViewState builder: (ctx) { return MultiSelectDialog( checkColor: widget.checkColor, + isSingleSelect: widget.isSingleSelect, selectedItemsTextStyle: widget.selectedItemsTextStyle, searchHintStyle: widget.searchHintStyle, searchTextStyle: widget.searchTextStyle,