smartselect
smartselect allows you to easily convert your usual form select or dropdown into dynamic page, popup dialog, or sliding bottom sheet with various choices input such as radio, checkbox, switch, chips, or even custom input. supports single and multiple choice. inspired by smart select component from framework7.
preview
features
- select single or multiple choice
- open choices modal in full page, bottom sheet, or popup dialog
- various choices input (radio, checkbox, switch, chips, or custom widget)
- various choices layout (list, wrap, or grid)
- grouping choices with easy support to sticky header
- searchable choices with highlighted result
- disabled or hidden choices
- customizable trigger/tile widget
- customizable modal style
- customizable modal header style
- customizable modal footer
- customizable choices style
- build choice items from any
list
- easy load async choice items
- and many more
usage
for a complete usage, please see the example.
to read more about classes and other references used by smart_select
, see the api reference.
single choice
// available configuration for single choice
smartselect<t>.single({
// the primary content of the widget.
// used in trigger widget and header option
string title,
// the text displayed when the value is null
string placeholder = 'select one',
// the current value of the single choice widget.
@required t value,
// called when single choice value changed
@required valuechanged<s2singlestate<t>> onchange,
// choice item list
list<s2choice<t>> choiceitems,
// other available configuration
// explained below
...,
...,
})
// simple usage
string value = 'flutter';
list<s2choice<string>> options = [
s2choice<string>(value: 'ion', title: 'ionic'),
s2choice<string>(value: 'flu', title: 'flutter'),
s2choice<string>(value: 'rea', title: 'react native'),
];
@override
widget build(buildcontext context) {
return smartselect<string>.single(
title: 'frameworks',
value: value,
choiceitems: options,
onchange: (state) => setstate(() => value = state.value)
);
}
multiple choice
// available configuration for multiple choice
smartselect<t>.multiple({
// the primary content of the widget.
// used in trigger widget and header option
string title,
// the text displayed when the value is null
string placeholder = 'select one',
// the current value of the single choice widget.
@required list<t> value,
// called when single choice value changed
@required valuechanged<s2multistate<t>> onchange,
// choice item list
list<s2choice<t>> choiceitems,
// other available configuration
// explained below
...,
...,
})
// a simple usage
list<int> value = [2];
list<s2choice<int>> frameworks = [
s2choice<int>(value: 1, title: 'ionic'),
s2choice<int>(value: 2, title: 'flutter'),
s2choice<int>(value: 3, title: 'react native'),
];
@override
widget build(buildcontext context) {
return smartselect<int>.multiple(
title: 'frameworks',
value: value,
choiceitems: options,
onchange: (state) => setstate(() => value = state.value),
);
}
choices
// configuration
smartselect<t>.[single|multiple]({
// other configuration
...,
...,
// choice item list
list<s2choice<t>> choiceitems,
// other configuration
...,
...,
});
choiceitems
can be input directly as in the example below, more info about s2choice
can be found on the api reference
smartselect<t>.[single|multiple](
...,
...,
choiceitems: <s2choice<t>>[
s2choice<t>(value: 1, title: 'ionic'),
s2choice<t>(value: 2, title: 'flutter'),
s2choice<t>(value: 3, title: 'react native'),
],
);
choiceitems
also can be created from any list using helper provided by this package, like the example below
list<map<string, string>> days = [
{ 'value': 'mon', 'title': 'monday' },
{ 'value': 'tue', 'title': 'tuesday' },
{ 'value': 'wed', 'title': 'wednesday' },
{ 'value': 'thu', 'title': 'thursday' },
{ 'value': 'fri', 'title': 'friday' },
{ 'value': 'sat', 'title': 'saturday' },
{ 'value': 'sun', 'title': 'sunday' },
];
smartselect<string>.[single|multiple](
...,
...,
choiceitems: s2choice.listfrom<string, map<string, string>>(
source: days,
value: (index, item) => item['value'],
title: (index, item) => item['title'],
),
);
load choice item asynchronously
please follow these example
modal configuration
more info about s2modalconfig
can be found on the api reference
// available configuration
smartselect<t>.[single|multiple]({
// other configuration
...,
...,
// modal validation of single choice widget
validationcallback<t> modalvalidation,
// modal configuration
s2modalconfig modalconfig,
// configure modal style
// shortcut to [modalconfig.style]
s2modalstyle modalstyle,
// configure modal header style
// shortcut to [modalconfig.headerstyle]
s2modalheaderstyle modalheaderstyle,
// modal type to display choices
// shortcut to [modalconfig.type]
s2modaltype modaltype,
// use different title with the trigger widget title
// shortcut to [modalconfig.title]
string modaltitle,
// whether the option list need to confirm
// to return the changed value
// shortcut to [modalconfig.useconfirm]
bool modalconfirm,
// whether the options list modal use header or not
// shortcut to [modalconfig.useheader]
bool modalheader,
// whether the option list is filterable or not
// shortcut to [modalconfig.usefilter]
bool modalfilter,
// whether the filter is autocomplete or need confirmation
// shortcut to [modalconfig.filterauto]
bool modalfilterauto,
// custom searchbar hint
// shortcut to [modalconfig.filterhint]
string modalfilterhint,
// other configuration
...,
...,
});
modal type
by default smartselect will open choices modal in full page. you can change it by changing with this value:
// available option
enum s2modaltype {
// open in full page
fullpage,
// open in popup dialog
popupdialog,
// open in sliding bottom sheet
bottomsheet,
}
modal style
// available option to configure modal style
s2modalstyle({
// modal border shape
// used in popup dialog and bottom sheet
shapeborder shape,
// modal elevation
// used in popup dialog and bottom sheet
double elevation,
// modal background color
color backgroundcolor,
// modal clip behavior
clip clipbehavior,
})
modal header style
// available option to configure modal header style
s2modalheaderstyle({
// header border shape
shapeborder shape,
// header elevation
double elevation,
// header background color
color backgroundcolor,
// header brightness
brightness brightness,
// whether the header title is centered
bool centertitle,
// whether the header use automaticallyimplyleading or not
bool useleading,
// header text style
// used by title and search field
textstyle textstyle,
// header icon theme
iconthemedata icontheme,
// header actions icon theme
iconthemedata actionsicontheme,
})
choices configuration
more info about s2choiceconfig
can be found on the api reference
// available option to configure choices
smartselect<t>.[single|multiple]({
// other configuration
...,
...,
// choice configuration
s2choiceconfig choiceconfig,
// configure choice style
// shortcut to [choiceconfig.style]
s2choicestyle choicestyle,
// configure choices group header style
// shortcut to [choiceconfig.headerstyle]
s2choiceheaderstyle choiceheaderstyle,
// choice widget type
// shortcut to [choiceconfig.type]
s2choicetype choicetype,
// choice layout to display items
// shortcut to [choiceconfig.layout]
s2choicelayout choicelayout,
// choice list scroll direction
// currently only support when
// [layout] is [s2choicelayout.wrap]
// shortcut to [choiceconfig.direction]
axis choicedirection,
// whether the choices list is grouped
// shortcut to [choiceconfig.isgrouped]
bool choicegrouped,
// whether the choices item use divider or not
// shortcut to [choiceconfig.usedivider]
bool choicedivider,
// for grid choice layout
// shortcut to [choiceconfig.griddelegate]
slivergriddelegate choicegrid,
// other configuration
...,
...,
});
choice type
by default smartselect will use radios
for single choice and checkboxes
for multiple choice, but it can change by changing with this value:
// type of choice input
enum s2choicetype {
// use radio widget
// for single choice
radios,
// use checkbox widget
// for multiple choice
checkboxes,
// use switch widget
// for multiple choice
switches,
// use chip widget
// for single and multiple choice
chips,
}
choice layout
by default smartselect will use list
, but it can change by changing with this value:
// layout of choice item
enum s2choicelayout {
// use list view widget
list,
// use wrap view widget
wrap,
// use grid view widget
grid,
}
choice styles
// available option to configure choice style
s2choicestyle({
// how much space to place between children in a run in the main axis.
// when use [smartselectchoicetype.chips] or [usewrap] is [true]
double spacing,
// how much space to place between the runs themselves in the cross axis.
// when use [smartselectchoicetype.chips] or [usewrap] is [true]
double runspacing,
// choices wrapper padding
edgeinsetsgeometry wrapperpadding,
// choices item padding
edgeinsetsgeometry padding,
// choices item title style
textstyle titlestyle,
// choices item subtitle style
textstyle subtitlestyle,
// whether the chips use checkmark or not
bool showcheckmark,
// where to place the control in widgets that use
// [listtile] to position a control next to a label.
s2choicecontrol control,
// highlight color
color highlightcolor,
// primary color of selected choice item
color activecolor,
// primary color of unselected choice item
color color,
// secondary color of selected choice item
color activeaccentcolor,
// secondary color of unselected choice item
color accentcolor,
// brightness for selected chip
brightness activebrightness,
// brightness for unselected chip
brightness brightness,
// opacity for selected chip border, only effect when
// [activebrightness] is [brightness.light]
double activeborderopacity,
// opacity for unselected chip border, only effect when
// [brightness] is [brightness.light]
double borderopacity,
// shape clip behavior
clip clipbehavior,
})
choice header style
// available option to configure choices group header widget style
s2choiceheaderstyle({
// group header background color
color backgroundcolor,
// highlight color
color highlightcolor,
// group header text style
textstyle textstyle,
// group header padding
edgeinsetsgeometry padding,
// group header height
double height,
})
builder widget
builder for single choice
// available builder configuration
// for single choice
smartselect<t>.single({
// other configuration
...,
...,
// builder collection of single choice widget
s2singlebuilder<t> builder,
// builder for custom tile widget
// shortcut to [builder.tile]
s2widgetbuilder<s2singlestate<t>> tilebuilder,
// builder for custom modal widget
// shortcut to [builder.modal]
s2widgetbuilder<s2singlestate<t>> modalbuilder,
// builder for custom modal header widget
// shortcut to [builder.modalheader]
s2widgetbuilder<s2singlestate<t>> modalheaderbuilder,
// builder for custom modal actions widget
// shortcut to [builder.modalactions]
s2listwidgetbuilder<s2singlestate<t>> modalactionsbuilder,
// builder for custom modal confirm action widget
// shortcut to [builder.modalconfirm]
s2widgetbuilder<s2singlestate<t>> modalconfirmbuilder,
// builder for divider widget between header, body, and footer modal
// shortcut to [builder.modaldivider]
s2widgetbuilder<s2singlestate<t>> modaldividerbuilder,
// builder for custom footer widget
// shortcut to [builder.modalfooter]
s2widgetbuilder<s2singlestate<t>> modalfooterbuilder,
// other configuration
...,
...,
});
builder for multiple choice
// available builder configuration
// for multiple choice
smartselect<t>.multiple({
// other configuration
...,
...,
// builder collection of single choice widget
s2multibuilder<t> builder,
// builder for custom tile widget
// shortcut to [builder.tile]
s2widgetbuilder<s2multistate<t>> tilebuilder,
// builder for custom modal widget
// shortcut to [builder.modal]
s2widgetbuilder<s2multistate<t>> modalbuilder,
// builder for custom modal header widget
// shortcut to [builder.modalheader]
s2widgetbuilder<s2multistate<t>> modalheaderbuilder,
// builder for custom modal actions widget
// shortcut to [builder.modalactions]
s2listwidgetbuilder<s2multistate<t>> modalactionsbuilder,
// builder for custom modal confirm action widget
// shortcut to [builder.modalconfirm]
s2widgetbuilder<s2multistate<t>> modalconfirmbuilder,
// builder for divider widget between header, body, and footer modal
// shortcut to [builder.modaldivider]
s2widgetbuilder<s2multistate<t>> modaldividerbuilder,
// builder for custom footer widget
// shortcut to [builder.modalfooter]
s2widgetbuilder<s2multistate<t>> modalfooterbuilder,
// other configuration
...,
...,
});
other builder
// another builder configuration
smartselect<t>.[single|multiple]({
// other configuration
...,
...,
// builder for modal filter widget
// shortcut to [builder.modalfilter]
s2widgetbuilder<s2filter> modalfilterbuilder,
// builder for modal filter toggle widget
// shortcut to [builder.modalfiltertoggle]
s2widgetbuilder<s2filter> modalfiltertogglebuilder,
// builder for each custom choices item widget
// shortcut to [builder.choice]
s2choicebuilder<t> choicebuilder,
// builder for each custom choices item title widget
// shortcut to [builder.choicetitle]
s2choicebuilder<t> choicetitlebuilder,
// builder for each custom choices item subtitle widget
// shortcut to [builder.choicesubtitle]
s2choicebuilder<t> choicesubtitlebuilder,
// builder for each custom choices item secondary widget
// shortcut to [builder.choicesecondary]
s2choicebuilder<t> choicesecondarybuilder,
/// builder for custom divider widget between choices item
// shortcut to [builder.choicedivider]
indexedwidgetbuilder choicedividerbuilder,
// builder for custom empty display
// shortcut to [builder.choiceempty]
s2widgetbuilder<string> choiceemptybuilder,
// a widget builder for custom choices group
// shortcut to [builder.choicegroup]
s2choicegroupbuilder choicegroupbuilder,
// a widget builder for custom header choices group
// shortcut to [builder.choiceheader]
s2choiceheaderbuilder choiceheaderbuilder,
// other configuration
...,
...,
});
tile widget
default tile
// default tile/trigger widget
s2tile<t>({
// the value of the selected option.
string value,
// the primary content of the list tile.
widget title,
// a widget to display before the title.
// typically an [icon] or a [circleavatar] widget.
widget leading,
// a widget to display after the title.
// typically an [icon] widget.
widget trailing,
// whether this list tile is intended to display loading stats.
bool isloading,
// string text used as loading text
string loadingtext,
// whether this list tile is intended to display two lines of text.
bool istwoline,
// whether this list tile is interactive.
bool enabled,
// if this tile is also [enabled] then icons and text are rendered with the same color.
bool selected,
// whether this list tile is part of a vertically dense list.
bool dense,
// whether the [value] is displayed or not
bool hidevalue,
// the tile's internal padding.
edgeinsetsgeometry padding,
// called when the user taps this list tile.
gesturetapcallback ontap,
// widget to display below the tile
// usually used to display chips with s2tilechips
widget body,
})
// usage example
smartselect<t>.single(
...,
...,
tilebuilder: (context, state) {
return s2tile(
title: state.titlewidget,
value: state.valuedisplay,
ontap: state.showmodal,
isloading: true,
);
},
);
// usage example from state
smartselect<t>.multiple(
...,
...,
tilebuilder: (context, state) {
return s2tile.fromstate(
state,
isloading: true,
);
},
);
tile with chips
// chips tile/trigger widget
s2tilechips({
// list of value of the selected choices.
int chiplength,
// widget builder for chip label item
indexedwidgetbuilder chiplabelbuilder,
// widget builder for chip avatar item
indexedwidgetbuilder chipavatarbuilder,
// widget builder for chip item
indexedwidgetbuilder chipbuilder,
// called when the user delete the chip item.
valuechanged<int> chipondelete,
// chip color
color chipcolor,
// chip border opacity
double chipborderopacity,
// chip brightness
brightness chipbrightness,
// chip delete button color
color chipdeletecolor,
// chip delete button icon
icon chipdeleteicon,
// chip spacing
double chipspacing,
// chip run spacing
double chiprunspacing,
// chip shape border
shapeborder chipshape,
// the [widget] displayed when the [values] is null
widget placeholder,
// whether the chip list is scrollable or not
bool scrollable,
// chip list padding
edgeinsetsgeometry padding,
})
/// usage example
smartselect<string>.multiple(
...,
...,
value: users,
tilebuilder: (context, state) {
return s2tile.fromstate(
state,
hidevalue: true,
body: s2tilechips(
chiplength: state.valueobject.length,
chiplabelbuilder: (context, i) {
return text(state.valueobject[i].title);
},
chipavatarbuilder: (context, i) {
return circleavatar(
backgroundimage: networkimage(state.valueobject[i].meta['picture']['thumbnail'])
);
},
chipondelete: (i) {
setstate(() => users.remove(state.valueobject[i].value));
},
chipcolor: colors.blue,
chipbrightness: brightness.dark,
chipborderopacity: .5,
),
);
},
);
Comments are closed.