multi select flutter
multi select flutter is a package for easily creating multi-select widgets in a variety of ways.
features
- supports formfield features like validator.
- neutral default design that can be altered to your heart’s content.
- choose between a dialog or bottomsheet style widget.
- easily switch the
listtype
from list to chip. - make your multi select
searchable
for larger lists.
install
add this to your pubspec.yaml file:
dependencies:
multi_select_flutter: ^2.1.0
usage
multiselectdialog
can be used in the builder of showdialog()
and triggered with your own button.
void _showmultiselectdialog(buildcontext context) async {
await showdialog(
context: context,
builder: (ctx) {
return multiselectdialog(
items: _animals.map((e) => multiselectitem(e, e)).tolist(),
initialvalue: _selectedanimals,
onconfirm: (values) {...},
);
},
);
}
multiselectbottomsheet
can be used in the builder of showmodalbottomsheet()
and triggered with your own button.
void _showmultiselect(buildcontext context) async {
await showmodalbottomsheet(
isscrollcontrolled: true, // required for min/max child size
context: context,
builder: (ctx) {
return multiselectbottomsheet(
items: _animals.map((e) => multiselectitem(e, e)).tolist(),
initialvalue: _selectedanimals,
onconfirm: (values) {...},
maxchildsize: 0.8,
);
},
);
}
multiselectchipdisplay
this widget can be used alongside your own button, or it can be specified as a chipdisplay
parameter of widgets like multiselectdialogfield
.
to use this widget effectively, make sure to set the state any time the source list is changed.
you can also remove items from the source list in the ontap function.
multiselectchipdisplay(
items: _selectedanimals.map((e) => multiselectitem(e, e)).tolist(),
ontap: (value) {
setstate(() {
_selectedanimals.remove(value);
});
},
),
when using the chipdisplay
parameter as part of a multiselectdialogfield for example, the multiselectchipdisplay still renders outside the boxdecoration of the field as seen here:
if you want to encapsulate the multiselectchipdisplay, wrap the multiselectdialogfield in a container and apply the decoration to that instead:
container(
decoration: boxdecoration(...),
child: multiselectdialogfield(
items: _items,
chipdisplay: multiselectchipdisplay(...),
),
),
multiselectdialogfield / multiselectbottomsheetfield
these widgets provide an inkwell button which open the dialog or bottom sheet.
multiselectbottomsheetfield(
items: _animals.map((e) => multiselectitem(e, e.name)).tolist(),
listtype: multiselectlisttype.chip,
searchable: true,
decoration: boxdecoration(...),
onconfirm: (values) {
setstate(() {
_selectedanimals = values;
});
},
chipdisplay: multiselectchipdisplay(),
),
multiselectdialogformfield / multiselectbottomsheetformfield
these widgets are the formfield versions of multiselectdialogfield
and multiselectbottomsheetfield
. you can make use of formfield parameters such as validator and onsaved.
it comes with a default bottom-border that can be overriden with the decoration
parameter.
multiselectdialogformfield(
items: _animals.map((e) => multiselectitem(e, e.name)).tolist(),
key: _multiselectkey,
validator: (value) {
if (value == null || value.isempty) {
return "required";
}
return null;
},
onconfirm: (values) {
setstate(() {
_selectedanimals = values;
});
_multiselectkey.currentstate.validate();
},
),
constructors
multiselectdialog
parameter | type | default | description |
---|---|---|---|
confirmtext |
text | text("ok") |
specifies the confirm button text. |
canceltext |
text | text("cancel") |
specifies the cancel button text. |
height |
double | null |
give the dialog a fixed height. |
initialvalue |
list<dynamic> | null |
list of selected values. required to retain values when re-opening the dialog. |
items |
list<multiselectitem<v>> | null |
the source list of options. |
listtype |
multiselectlisttype | multiselectlisttype.list |
change the listtype. can be either multiselectlisttype.list or multiselectlisttype.chip |
onselectionchanged |
function(list<dynamic>) | null |
fires when an item is selected or unselected. |
onconfirm |
function(list<dynamic> ) |
null |
fires when the confirm button is pressed. |
searchable |
bool | false |
toggle search functionality within the dialog. |
title |
string | "select" |
the title that is displayed at the top of the dialog. |
searchplaceholder |
string | "search" |
set the placeholder text of the search field. |
selectedcolor |
color | null |
set the color of the checkbox or chip items that are selected. |
colorator |
color function(v) | null |
set the selected color of individual items based on their value. applies to both chips and checkboxes. |
multiselectdialogfield
multiselectdialogfield has all the parameters of multiselectdialog plus these extra parameters:
parameter | type | default | description |
---|---|---|---|
barriercolor |
color | null |
set the color of the space outside the dialog. |
buttontext |
text | "select" |
set text that is displayed on the button. |
buttonicon |
icon | icons.arrow_downward |
specify the button icon. |
chipdisplay |
multiselectchipdisplay | null |
attach a multiselectchipdisplay to this field. |
decoration |
boxdecoration | null |
style the container that makes up the field. |
multiselectbottomsheet
parameter | type | default | description |
---|---|---|---|
confirmtext |
text | text("ok") |
specifies the confirm button text. |
canceltext |
text | text("cancel") |
specifies the cancel button text. |
initialchildsize |
double | 0.3 |
the initial height of the bottomsheet. |
initialvalue |
list<dynamic> | null |
list of selected values. required to retain values when re-opening the dialog. |
items |
list<multiselectitem<v>> | null |
the source list of options. |
listtype |
multiselectlisttype | multiselectlisttype.list |
change the listtype. can be either multiselectlisttype.list or multiselectlisttype.chip |
maxchildsize |
double | 0.6 |
set the maximum height threshold of the bottomsheet. |
minchildsize |
double | 0.3 |
set the minimum height threshold of the bottomsheet before it closes. |
onselectionchanged |
function(list<dynamic>) | null |
fires when an item is selected or unselected. |
onconfirm |
function(list<dynamic> ) |
null |
fires when the confirm button is pressed. |
searchable |
bool | false |
toggle search functionality within the dialog. |
title |
string | "select" |
the title that is displayed at the top of the dialog. |
searchplaceholder |
string | "search" |
set the placeholder text of the search field. |
selectedcolor |
color | null |
set the color of the checkbox or chip items that are selected. |
colorator |
color function(v) | null |
set the selected color of individual items based on their value. applies to both chips and checkboxes. |
multiselectbottomsheetfield
multiselectbottomsheetfield has all the parameters of multiselectbottomsheet plus these extra parameters:
parameter | type | default | usage |
---|---|---|---|
barriercolor |
color | null |
set the color of the space outside the bottomsheet. |
buttonicon |
icon | icons.arrow_downward |
specify the button icon. |
buttontext |
text | "select" |
set text that is displayed on the button. |
chipdisplay |
multiselectchipdisplay | null |
attach a multiselectchipdisplay to this field. |
decoration |
boxdecoration | null |
style the container that makes up the field. |
shape |
shapeborder | roundedrectangleborder(borderradius: borderradius.vertical(top: radius.circular(15.0))) |
apply a shapeborder to alter the edges of the bottomsheet. |
multiselectdialogformfield / multiselectbottomsheetformfield
these widgets have all the parameters of their non-formfield counterparts, plus these extra parameters which come from the formfield class:
parameter | type | default | description |
---|---|---|---|
autovalidate |
list<multiselectitem> | false |
if true, form fields will validate and update their error text immediately after every change. default is false. |
key |
globalkey<formfieldstate> | null |
can be used to call methods like _multiselectkey.currentstate.validate() . |
onsaved |
list<multiselectitem> | null |
a callback that is called whenever we submit the field (usually by calling the save method on a form. |
validator |
formfieldvalidator<list> | null |
validation. see flutter’s documentation. |
multiselectchipdisplay
parameter | type | default | description |
---|---|---|---|
alignment |
alignment | alignment.centerleft |
change the alignment of the chips. |
chipcolor |
color | primarycolor |
set the chip color. |
decoration |
boxdecoration | null |
style the container that makes up the chip display. |
items |
list<multiselectitem> | null |
the source list of selected items. |
ontap |
function(dynamic) | null |
fires when a chip is tapped. |
textstyle |
textstyle | null |
style the text on the chips. |
opacity |
double | null |
set the opacity of the chips. |
colorator |
color function(v) | null |
set the chip color of individual items based on their value. |
Comments are closed.