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

feat: add single select to dialog field #181

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class _MyHomePageState extends State<MyHomePage> {
// Rounded blue MultiSelectDialogField
//################################################################################################
MultiSelectDialogField(
isSingleSelect: true,
items: _items,
title: Text("Animals"),
selectedColor: Colors.blue,
Expand Down
19 changes: 14 additions & 5 deletions lib/dialog/mult_select_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ class MultiSelectDialog<T> extends StatefulWidget with MultiSelectActions<T> {
/// 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,
Expand All @@ -103,6 +106,7 @@ class MultiSelectDialog<T> extends StatefulWidget with MultiSelectActions<T> {
this.selectedItemsTextStyle,
this.separateSelectedItems = false,
this.checkColor,
this.isSingleSelect,
});

@override
Expand Down Expand Up @@ -154,6 +158,15 @@ class _MultiSelectDialogState<T> extends State<MultiSelectDialog<T>> {
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!);

Expand Down Expand Up @@ -197,11 +210,7 @@ class _MultiSelectDialogState<T> extends State<MultiSelectDialog<T>> {
),
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);
Expand Down
14 changes: 11 additions & 3 deletions lib/dialog/multi_select_dialog_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ class MultiSelectDialogField<V> extends FormField<List<V>> {
final GlobalKey<FormFieldState>? key;
FormFieldState<List<V>>? state;

/// Enable single choice
final bool? isSingleSelect;

MultiSelectDialogField({
required this.items,
required this.onConfirm,
Expand Down Expand Up @@ -141,6 +144,7 @@ class MultiSelectDialogField<V> extends FormField<List<V>> {
this.initialValue = const [],
this.autovalidateMode = AutovalidateMode.disabled,
this.key,
this.isSingleSelect,
}) : super(
key: key,
onSaved: onSaved,
Expand Down Expand Up @@ -181,7 +185,8 @@ class MultiSelectDialogField<V> extends FormField<List<V>> {
checkColor: checkColor,
isDismissible: isDismissible,
);
return _MultiSelectDialogFieldView<V>._withState(field, state);
return _MultiSelectDialogFieldView<V>._withState(
field, state, isSingleSelect);
});
}

Expand Down Expand Up @@ -216,6 +221,7 @@ class _MultiSelectDialogFieldView<V> extends StatefulWidget {
final TextStyle? searchHintStyle;
final bool separateSelectedItems;
final Color? checkColor;
final bool? isSingleSelect;
final bool isDismissible;
FormFieldState<List<V>>? state;

Expand Down Expand Up @@ -250,11 +256,12 @@ class _MultiSelectDialogFieldView<V> 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<V> field, FormFieldState<List<V>> state)
_MultiSelectDialogFieldView._withState(_MultiSelectDialogFieldView<V> field,
FormFieldState<List<V>> state, this.isSingleSelect)
: items = field.items,
title = field.title,
buttonText = field.buttonText,
Expand Down Expand Up @@ -382,6 +389,7 @@ class __MultiSelectDialogFieldViewState<V>
builder: (ctx) {
return MultiSelectDialog<V>(
checkColor: widget.checkColor,
isSingleSelect: widget.isSingleSelect,
selectedItemsTextStyle: widget.selectedItemsTextStyle,
searchHintStyle: widget.searchHintStyle,
searchTextStyle: widget.searchTextStyle,
Expand Down