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

Select All #17

Open
wants to merge 5 commits 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
41 changes: 39 additions & 2 deletions lib/multiselect_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class MultiSelectDialog<V> extends StatefulWidget {
final Widget title;
final String okButtonLabel;
final String cancelButtonLabel;
final bool showSelectAll;
final String selectAllText;
bool selectAllState = false;
final TextStyle labelStyle;
final ShapeBorder dialogShapeBorder;
final Color checkBoxCheckColor;
Expand All @@ -28,9 +31,12 @@ class MultiSelectDialog<V> extends StatefulWidget {
this.labelStyle = const TextStyle(),
this.dialogShapeBorder,
this.checkBoxActiveColor,
this.checkBoxCheckColor})
this.checkBoxCheckColor,
this.showSelectAll,
this.selectAllText})
: super(key: key);


@override
State<StatefulWidget> createState() => _MultiSelectDialogState<V>();
}
Expand All @@ -45,6 +51,18 @@ class _MultiSelectDialogState<V> extends State<MultiSelectDialog<V>> {
}
}

void _onSelectAllCheckedChange(bool checked) {
setState(() {
widget.selectAllState = checked;
if (checked) {
_selectedValues.removeRange(0, _selectedValues.length);
_selectedValues.addAll(widget.items.map((e) => e.value));
} else {
_selectedValues.removeRange(0, _selectedValues.length);
}
});
}

void _onItemCheckedChange(V itemValue, bool checked) {
setState(() {
if (checked) {
Expand Down Expand Up @@ -73,7 +91,7 @@ class _MultiSelectDialogState<V> extends State<MultiSelectDialog<V>> {
child: ListTileTheme(
contentPadding: EdgeInsets.fromLTRB(14.0, 0.0, 24.0, 0.0),
child: ListBody(
children: widget.items.map(_buildItem).toList(),
children: _buildList(),
),
),
),
Expand All @@ -90,6 +108,16 @@ class _MultiSelectDialogState<V> extends State<MultiSelectDialog<V>> {
);
}

List<Widget> _buildList() {
List<Widget> listBody = [];
if (widget.showSelectAll) {
listBody.add(
_buildSelectAllItem(widget.selectAllState, widget.selectAllText));
}
listBody.addAll(widget.items.map(_buildItem));
return listBody;
}

Widget _buildItem(MultiSelectDialogItem<V> item) {
final checked = _selectedValues.contains(item.value);
return CheckboxListTile(
Expand All @@ -104,4 +132,13 @@ class _MultiSelectDialogState<V> extends State<MultiSelectDialog<V>> {
onChanged: (checked) => _onItemCheckedChange(item.value, checked),
);
}

Widget _buildSelectAllItem(checked, title) {
return CheckboxListTile(
value: checked,
title: Text(title),
controlAffinity: ListTileControlAffinity.leading,
onChanged: (checked) => _onSelectAllCheckedChange(checked),
);
}
}
8 changes: 8 additions & 0 deletions lib/multiselect_formfield.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class MultiSelectFormField extends FormField<dynamic> {
final ShapeBorder dialogShapeBorder;
final Color checkBoxCheckColor;
final Color checkBoxActiveColor;
final bool showSelectAll;
final String selectAllText;

MultiSelectFormField({
FormFieldSetter<dynamic> onSaved,
Expand All @@ -47,6 +49,8 @@ class MultiSelectFormField extends FormField<dynamic> {
this.cancelButtonLabel = 'CANCEL',
this.fillColor,
this.border,
this.showSelectAll = true,
this.selectAllText = "Select All",
this.trailing,
this.chipLabelStyle,
this.chipBackGroundColor,
Expand All @@ -71,6 +75,7 @@ class MultiSelectFormField extends FormField<dynamic> {
(itm) => itm[valueField] == item,
orElse: () => null);
selectedOptions.add(Chip(

labelStyle: chipLabelStyle,
backgroundColor: chipBackGroundColor,
label: Text(
Expand Down Expand Up @@ -107,6 +112,8 @@ class MultiSelectFormField extends FormField<dynamic> {
cancelButtonLabel: cancelButtonLabel,
items: items,
initialSelectedValues: initialSelected,
showSelectAll: showSelectAll,
selectAllText: selectAllText,
labelStyle: dialogTextStyle,
dialogShapeBorder: dialogShapeBorder,
checkBoxActiveColor: checkBoxActiveColor,
Expand Down Expand Up @@ -140,6 +147,7 @@ class MultiSelectFormField extends FormField<dynamic> {
Expanded(
child: title,
),

required
? Padding(
padding: EdgeInsets.only(top: 5, right: 5),
Expand Down