flutter dropdownbutton2
intro
flutter’s core dropdown button widget with steady dropdown menu and many options you can customize
to your needs.
features
- dropdown menu always open below the button and you can edit its position by using the offset
parameter. - you can make the menu open above the button by setting showabovebutton to true.
- you can edit (button, menu and menu items) height, width and decoration as you want.
- you can align hint or value and customize them.
- you can edit the scrollbar’s radius,thickness and isalwaysshow.
- you can specify max height for the dropdown menu & it’ll become scrollable if there are more
items. - if you pass null to dropdownmaxheight parameter or didn’t use it, the dropdown menu will take max
height possible for the items and will become scrollable if there are more items. - if you have long scrollable list, the dropdown menu will auto scroll to last selected item and
show it at the middle of the menu if possible. - wrap the dropdownbutton2 with dropdownbuttonhideunderline to hide the underline.
- a custom widget of the dropdownbutton2 below to make it more reusable. you can customize it to
your needs and use it throughout all your app easily as shown in the examples. - you can use dropdownbutton2 as a popup menu button by using the parameter custombutton. you can
pass icon,image or anything you want and customize it as shown in the examples. - you can make the popup menu open onlongpress instead of ontap by setting openwithlongpress
parameter to true. - you can add dividers to the popup menu with different height by passing dividers indexes to
customitemsindexes and the height to customitemsheight. - you can also use dropdownbuttonformfield2 the same way with all options above.
- use decoration parameter for the dropdownbuttonformfield2 to add labeltext,fillcolor and more.
- you can customize the dropdownbuttonformfield2 width by wrapping it with sizedbox and give it the
width you want.
installation
add this line to pubspec.yaml
dependencies:
dropdown_button2: ^1.0.0
import package
import 'package:dropdown_button2/dropdown_button2.dart';
usage & examples
- simple dropdownbutton2 without too many specifications:
string? selectedvalue;
list<string> items = [
'item1',
'item2',
'item3',
'item4',
];
@override
widget build(buildcontext context) {
return scaffold(
body: center(
child: dropdownbuttonhideunderline(
child: dropdownbutton2(
hint: text(
'select item',
style: textstyle(
fontsize: 14,
color: theme
.of(context)
.hintcolor,
),
),
items: items
.map((item) =>
dropdownmenuitem<string>(
value: item,
child: text(
item,
style: const textstyle(
fontsize: 14,
),
),
))
.tolist(),
value: selectedvalue,
onchanged: (value) {
setstate(() {
selectedvalue = value as string;
});
},
buttonheight: 40,
buttonwidth: 140,
itemheight: 40,
itemwidth: 140,
),
),
),
);
}
- more customized dropdownbutton2 using dropdownbutton2 directly:
string? selectedvalue;
list<string> items = [
'item1',
'item2',
'item3',
'item4',
'item5',
'item6',
'item7',
'item8',
];
@override
widget build(buildcontext context) {
return scaffold(
body: center(
child: dropdownbuttonhideunderline(
child: dropdownbutton2(
isexpanded: true,
hint: row(
children: const [
icon(
icons.list,
size: 16,
color: colors.yellow,
),
sizedbox(
width: 4,
),
expanded(
child: text(
'select item',
style: textstyle(
fontsize: 14,
fontweight: fontweight.bold,
color: colors.yellow,
),
overflow: textoverflow.ellipsis,
),
),
],
),
items: items
.map((item) =>
dropdownmenuitem<string>(
value: item,
child: text(
item,
style: const textstyle(
fontsize: 14,
fontweight: fontweight.bold,
color: colors.white,
),
overflow: textoverflow.ellipsis,
),
))
.tolist(),
value: selectedvalue,
onchanged: (value) {
setstate(() {
selectedvalue = value as string;
});
},
icon: const icon(
icons.arrow_forward_ios_outlined,
),
iconsize: 14,
iconenabledcolor: colors.yellow,
icondisabledcolor: colors.grey,
buttonheight: 50,
buttonwidth: 160,
buttonpadding: const edgeinsets.only(left: 14, right: 14),
buttondecoration: boxdecoration(
borderradius: borderradius.circular(14),
border: border.all(
color: colors.black26,
),
color: colors.redaccent,
).copywith(
boxshadow: kelevationtoshadow[2],
),
itemheight: 40,
itemwidth: 200,
itempadding: const edgeinsets.only(left: 14, right: 14),
dropdownmaxheight: 200,
dropdownpadding: null,
dropdownborderradius: borderradius.circular(14),
dropdownborder: null,
dropdowncolor: colors.redaccent,
elevation: 8,
scrollbarradius: const radius.circular(40),
scrollbarthickness: 6,
scrollbaralwaysshow: true,
offset: const offset(-20, 0),
),
),
),
);
}
- more customized dropdownbutton2 using the reusable custom widget attached below:
string? selectedvalue;
list<string> items = [
'item1',
'item2',
'item3',
'item4',
'item5',
'item6',
'item7',
'item8',
];
@override
widget build(buildcontext context) {
return scaffold(
body: center(
child: customdropdownbutton2(
hint: 'select item',
dropdownitems: items,
value: selectedvalue,
onchanged: (value) {
setstate(() {
selectedvalue = value;
});
},
),
),
);
}
- dropdownbutton2 as popup menu button using custombutton parameter and adding custom items with
different height like dividers:
example 1 using icon:
class custombuttontest extends statefulwidget {
const custombuttontest({key? key}) : super(key: key);
@override
state<custombuttontest> createstate() => _custombuttonteststate();
}
class _custombuttonteststate extends state<custombuttontest> {
@override
widget build(buildcontext context) {
return scaffold(
body: center(
child: dropdownbuttonhideunderline(
child: dropdownbutton2(
custombutton: const icon(
icons.list,
size: 46,
color: colors.red,
),
customitemsindexes: const [3],
customitemsheight: 8,
items: [
...menuitems.firstitems.map(
(item) =>
dropdownmenuitem<menuitem>(
value: item,
child: menuitems.builditem(item),
),
),
const dropdownmenuitem<divider>(enabled: false, child: divider()),
...menuitems.seconditems.map(
(item) =>
dropdownmenuitem<menuitem>(
value: item,
child: menuitems.builditem(item),
),
),
],
onchanged: (value) {
menuitems.onchanged(context, value as menuitem);
},
itemheight: 48,
itemwidth: 160,
itempadding: const edgeinsets.only(left: 16, right: 16),
dropdownpadding: const edgeinsets.symmetric(vertical: 6),
dropdownborderradius: borderradius.circular(4),
dropdownborder: null,
dropdowncolor: colors.redaccent,
elevation: 8,
offset: const offset(0, 8),
),
),
),
);
}
}
class menuitem {
final string text;
final icondata icon;
const menuitem({
required this.text,
required this.icon,
});
}
class menuitems {
static const list<menuitem> firstitems = [home, share, settings];
static const list<menuitem> seconditems = [logout];
static const home = menuitem(text: 'home', icon: icons.home);
static const share = menuitem(text: 'share', icon: icons.share);
static const settings = menuitem(text: 'settings', icon: icons.settings);
static const logout = menuitem(text: 'log out', icon: icons.logout);
static widget builditem(menuitem item) {
return row(
children: [
icon(
item.icon,
color: colors.white,
size: 22
),
const sizedbox(
width: 10,
),
text(
item.text,
style: const textstyle(
color: colors.white,
),
),
],
);
}
static onchanged(buildcontext context, menuitem item) {
switch (item) {
case menuitems.home:
//do something
break;
case menuitems.settings:
//do something
break;
case menuitems.share:
//do something
break;
case menuitems.logout:
//do something
break;
}
}
}
example 2 using image and openwithlongpress parameter:
class custombuttontest extends statefulwidget {
const custombuttontest({key? key}) : super(key: key);
@override
state<custombuttontest> createstate() => _custombuttonteststate();
}
class _custombuttonteststate extends state<custombuttontest> {
@override
widget build(buildcontext context) {
return scaffold(
body: center(
child: dropdownbuttonhideunderline(
child: dropdownbutton2(
custombutton: container(
height: 240,
width: 240,
decoration: boxdecoration(
borderradius: borderradius.circular(40),
image: const decorationimage(
image: networkimage(
'https://cdn.pixabay.com/photo/2020/05/11/06/20/city-5156636_960_720.jpg',
),
fit: boxfit.cover,
),
),
),
openwithlongpress: true,
customitemsindexes: const [3],
customitemsheight: 8,
items: [
...menuitems.firstitems.map(
(item) =>
dropdownmenuitem<menuitem>(
value: item,
child: menuitems.builditem(item),
),
),
const dropdownmenuitem<divider>(enabled: false, child: divider()),
...menuitems.seconditems.map(
(item) =>
dropdownmenuitem<menuitem>(
value: item,
child: menuitems.builditem(item),
),
),
],
onchanged: (value) {
menuitems.onchanged(context, value as menuitem);
},
itemheight: 48,
itemwidth: 160,
itempadding: const edgeinsets.only(left: 16, right: 16),
dropdownpadding: const edgeinsets.symmetric(vertical: 6),
dropdownborderradius: borderradius.circular(4),
dropdownborder: null,
dropdowncolor: colors.redaccent,
elevation: 8,
offset: const offset(40, -4),
),
),
),
);
}
}
class menuitem {
final string text;
final icondata icon;
const menuitem({
required this.text,
required this.icon,
});
}
class menuitems {
static const list<menuitem> firstitems = [like, share, download];
static const list<menuitem> seconditems = [cancel];
static const like = menuitem(text: 'like', icon: icons.favorite);
static const share = menuitem(text: 'share', icon: icons.share);
static const download = menuitem(text: 'download', icon: icons.download);
static const cancel = menuitem(text: 'cancel', icon: icons.cancel);
static widget builditem(menuitem item) {
return row(
children: [
icon(
item.icon,
color: colors.white,
size: 22,
),
const sizedbox(
width: 10,
),
text(
item.text,
style: const textstyle(
color: colors.white,
),
),
],
);
}
static onchanged(buildcontext context, menuitem item) {
switch (item) {
case menuitems.like:
//do something
break;
case menuitems.share:
//do something
break;
case menuitems.download:
//do something
break;
case menuitems.cancel:
//do something
break;
}
}
}
custom dropdownbutton2 widget
class customdropdownbutton2 extends statelesswidget {
final string hint;
final string? value;
final list<string> dropdownitems;
final valuechanged<string?>? onchanged;
final alignment? hintalignment;
final alignment? valuealignment;
final double? buttonheight, buttonwidth;
final edgeinsetsgeometry? buttonpadding;
final decoration? buttondecoration;
final int? buttonelevation;
final widget? icon;
final double? iconsize;
final color? iconenabledcolor;
final color? icondisabledcolor;
final double? itemheight, itemwidth;
final edgeinsetsgeometry? itempadding;
final double? dropdownheight;
final edgeinsetsgeometry? dropdownpadding;
final borderradius? dropdownborderradius;
final boxborder? dropdownborder;
final int? dropdownelevation;
final color? dropdowncolor;
final radius? scrollbarradius;
final double? scrollbarthickness;
final bool? scrollbaralwaysshow;
final offset? offset;
const customdropdownbutton2({
required this.hint,
required this.value,
required this.dropdownitems,
required this.onchanged,
this.hintalignment,
this.valuealignment,
this.buttonheight,
this.buttonwidth,
this.buttonpadding,
this.buttondecoration,
this.buttonelevation,
this.icon,
this.iconsize,
this.iconenabledcolor,
this.icondisabledcolor,
this.itemheight,
this.itemwidth,
this.itempadding,
this.dropdownheight,
this.dropdownpadding,
this.dropdownborderradius,
this.dropdownborder,
this.dropdownelevation,
this.dropdowncolor,
this.scrollbarradius,
this.scrollbarthickness,
this.scrollbaralwaysshow,
this.offset,
key? key,
}) : super(key: key);
@override
widget build(buildcontext context) {
return dropdownbuttonhideunderline(
child: dropdownbutton2(
isexpanded: true,
//to avoid long text overflowing.
hint: container(
alignment: hintalignment,
child: text(
hint,
overflow: textoverflow.ellipsis,
maxlines: 1,
style: textstyle(
fontsize: 14,
color: theme
.of(context)
.hintcolor,
),
),
),
value: value,
items: dropdownitems
.map((item) =>
dropdownmenuitem<string>(
value: item,
child: container(
alignment: valuealignment,
child: text(
item,
overflow: textoverflow.ellipsis,
maxlines: 1,
style: const textstyle(
fontsize: 14,
),
),
),
))
.tolist(),
onchanged: onchanged,
icon: icon ?? const icon(icons.arrow_forward_ios_outlined),
iconsize: iconsize ?? 12,
iconenabledcolor: iconenabledcolor,
icondisabledcolor: icondisabledcolor,
buttonheight: buttonheight ?? 40,
buttonwidth: buttonwidth ?? 140,
buttonpadding:
buttonpadding ?? const edgeinsets.only(left: 14, right: 14),
buttondecoration: buttondecoration ??
boxdecoration(
borderradius: borderradius.circular(14),
border: border.all(
color: colors.black45,
),
color: theme
.of(context)
.canvascolor,
).copywith(
boxshadow: kelevationtoshadow[buttonelevation ?? 0],
),
itemheight: itemheight ?? 40,
itemwidth: itemwidth ?? 140,
itempadding: itempadding ?? const edgeinsets.only(left: 14, right: 14),
dropdownmaxheight: dropdownheight ?? 240,
//max height for the dropdown menu & becoming scrollable if there are more items. if you pass null it will take max height possible for the items.
dropdownpadding: dropdownpadding,
dropdownborderradius: dropdownborderradius ?? borderradius.circular(14),
dropdownborder: dropdownborder,
//default has no border.
dropdowncolor: dropdowncolor ?? theme
.of(context)
.canvascolor,
elevation: dropdownelevation ?? 8,
scrollbarradius: scrollbarradius ?? const radius.circular(40),
scrollbarthickness: scrollbarthickness,
scrollbaralwaysshow: scrollbaralwaysshow,
offset: offset,
//null or offset(0, 0) will open just under the button. you can edit as you want.
showabovebutton: false, //default is false to show menu below button
),
);
}
}
thanks
if something is missing or you want to add some feature, feel free to open a ticket or contribute!
Comments are closed.