Remove default padding of DropDownButton in flutter? - flutter

I want to achieve a result like this where my dropdown button has very less padding.
This is what I currently have
so far I have tried adjusting height of dropDownButton, wrapping with a container, assigning padding but nothing works
here's my code
Container(
child: Center(
child: DropdownButtonHideUnderline(
child: ButtonTheme(
alignedDropdown: true,
child: DropdownButton<String>(
dropdownColor: AppColors().white,
value: value,
style: TextStyles().medium12,
icon: Icon(
Icons.keyboard_arrow_down,
color: AppColors().green,
),
onChanged: (newValue) {
setState(() {
value = newValue!;
});
},
items: <String>[
AppStrings().getString().english_language,
AppStrings().getString().arabic_language,
AppStrings().getString().urdu_language,
].map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(
value,
),
);
}).toList(),
),
),
),
),
padding: EdgeInsets.only(
left: 10.w,
right: 10.w,
),
decoration: BoxDecoration(
border: Border.all(color: AppColors().green, width: 6.w),
borderRadius: BorderRadius.all(
Radius.circular(25.r) // <--- border radius here
),
),
);
can someone please help me? thankyou so much

just set dropdownbutton isDense: true

Related

Flutter - How to Change Dropdown Item List Size and Alignment

I am using flutter DropdownButton for list out something and it worked well while selecting an option from the list the drop-down list appears full screen, I need to give some padding so that the user can cancel the list(hide).
The code:
ListView(children: <Widget>[
....
Container(
padding: EdgeInsets.all(5.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(5.0),
border: Border.all(
color: Colors.grey,
style: BorderStyle.solid,
width: 0.80),
),
alignment: Alignment.center,
child: DropdownButtonHideUnderline(
child: DropdownButton(
isExpanded: true,
hint: Text('Select Your Height'),
icon: Icon(Icons.arrow_downward),
iconSize: 20,
iconEnabledColor: AppColors.PRIMARY_COLOR,
items: _heights.map((dropDownHieghtItem) {
return DropdownMenuItem(
value:
dropDownHieghtItem['height_id'].toString(),
child: Text(dropDownHieghtItem['height_name']));
}).toList(),
onChanged: (String newselectedHieght) {
setState(() {
heightID = newselectedHieght.toString();
});
},
value: heightID)),
),......
])
instead of using Text widget inside DropdownMenuItem use Container and add custom TextStyle
like this:
_heights.map((dropDownHieghtItem) {
return DropdownMenuItem(
value:
dropDownHieghtItem['height_id'].toString(),
child: Container(
alignment: Alignment.center,
child: Text(dropDownHieghtItem['height_name'],style: TextStyle(fontSize: 20,color: Colors.red),textAlign: TextAlign.center,)
)
);
}).toList(),

Dropdown button display text cut off Flutter

Hi I have a dropdown button whose hint and selected value text is cut off when too long:
It should be displaying "Department Code / Department Number"
Code:
DropdownButton<String> dropdownList(BuildContext context) {
return new DropdownButton<String>(
hint: new Text(
"Department Code / Department Number",
),
value: selectedDept,
isDense: true,
onChanged: (String newValue) {
//...
},
items: _item.map((Dept map) {
return new DropdownMenuItem<String>(
value: map.code,
child:
new Text(map.code, style: new TextStyle(color: Colors.black)),
);
}).toList(),
isExpanded: true,
);
}
Widget build(BuildContext context) {
return Scaffold(
child: Column(
children: <Widget>[
Container(
padding: EdgeInsets.fromLTRB(
20, 20, 10, 20),
decoration: ShapeDecoration(
shape: RoundedRectangleBorder(
side: BorderSide(
width: 1.0,
style: BorderStyle.solid,
color: Colors.grey[400]),
borderRadius:
BorderRadius.all(
Radius.circular(
15.0)),
),
),
child:
DropdownButtonHideUnderline(
child: dropdownList(
context)),
)
]
)
)
}
Any idea how to fix the display?
If you dont want to increase the size of the dropdown, Change the content padding to something that suits you.
DropdownButtonFormField(
itemHeight: 50,
decoration: InputDecoration(
contentPadding:
EdgeInsets.symmetric(vertical: 5, horizontal: 10),
.
.
.
Theres usually a default value for contentPadding which is cutting up your text.
To access input decoration you'll need to use DropdownButtonFromField
In the Container(), add in BoxConstraints and specify minHeight and maxHeight
constraints: new BoxConstraints(
minHeight: 50.0,
maxHeight: 70.0,
)
Just add padding to make space between your inner content.
return DropdownMenuItem<String>(
value: value,
child: Padding(
padding: EdgeInsets.all(4),
child: Text(map.code, style: new TextStyle(color: Colors.black)),
),
);

Custom Dropdown Button and MenuItems Flutter

I am trying to build my own drop down button with separated and condensed menu items, like the image below:
here's the code I have tried so far, I got the drop down width to match the container but the items can't be customized so far and always the height starts from above the button and is not occupying the width of the container:
body: Container(
margin: EdgeInsets.symmetric(horizontal: 10.0, vertical: 5.0),
child: Container(
width: double.infinity,
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(10.0)),
border: Border.all(color: Colors.brown, width: 1.0)),
padding: EdgeInsets.fromLTRB(10.0, 5.0, 10.0, 5.0),
child: DropdownButtonHideUnderline(
child: ButtonTheme(
alignedDropdown: true,
child: DropdownButton(
isExpanded: true,
isDense: true,
value: selection,
icon: Icon(
Icons.arrow_drop_down,
color: Colors.brown,
),
iconSize: 40,
underline: Container(
height: 1,
color: Colors.transparent,
),
onChanged: (String val) => setState(() => selection = val),
items: settingsOptions.map((option) {
return DropdownMenuItem(
value: option,
child: Text(option),
);
}).toList(),
),
),
)
),
),
This is the output from the code:
How do I customize the items width, height and add dividers like in the first image? Thanks
This is an example modify as you like !
DropdownButton(
isExpanded: true,
isDense: true,
value: selection,
icon: Icon(
Icons.arrow_drop_down,
color: Colors.brown,
),
iconSize: 40,
underline: Container(
height: 1,
color: Colors.transparent,
),
onChanged: (String val) => setState(() => selection = val),
items: sampleList.map((option) {
return DropdownMenuItem(
value: option,
child: Container(
width:double.infinity,
alignment:Alignment.centerLeft,
padding: const EdgeInsets.fromLTRB(0,8.0,0,6.0),
child:Text(option),
decoration:BoxDecoration(
border:Border(top:BorderSide(color:Colors.grey,width:1))
)
),
);
}).toList(),
selectedItemBuilder:(con){
return sampleList.map((m){
return Text(m,);
}).toList();
}
)
I came across a flutter Library called dropdown_below in pub.dev. It has additional methods that can allow you customize dropdownMenuItem to your preferred layout.
DropdownBelow(
value: category,
// isDense: true,
itemWidth: MediaQuery.of(context).size.width * 0.92,
itemTextstyle: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w400,
color: Colors.black),
boxTextstyle: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w400,
color: Colors.grey.shade600),
boxPadding: EdgeInsets.fromLTRB(13, 12, 0, 12),
boxWidth: MediaQuery.of(context).size.width,
boxHeight: 60,
hint: Text('choose video'),
items: video.data.value.videos
.map((e) => DropdownMenuItem(
onTap: () => e.title,
value: e.title ?? category,
child: Container(
width: double.infinity,
decoration: BoxDecoration(),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
e.title ?? '$category',
overflow: TextOverflow.ellipsis,
),
),
),
))
.toList(),
onChanged: (video) {
context.read(videoProvider).cateroryOnChange(video);
},
),
Library Link: https://pub.dev/packages/dropdown_below
The problem with the DropdownButton is that the menu will open randomly based on the selected index and other things. Also, You can't edit its code by just trying to pass offset as the logic code of the paint work based on that and trying to hardcode the selectedItemOffset to a value won't work perfectly fine.
So use DropDownButton2 , which is built just over this .
Package: DropDownButton2
Credits: Ahmed Elsayed
For reference: Refer this link

How to center text in DropdownButton?

I'm trying to make the DropdownButton hint, text and menu items appear in the center instead of left but TextAlign.center does not seem to be doing anything.
Image of Dropdown with the hint:
Image of Dropdown with the selected item as text:
Image of the Menu items when arrow is pressed:
My code:
return Theme(
data: ThemeData(canvasColor: blackTrans2, brightness: Brightness.dark),
child:Container(
width: MediaQuery.of(context).size.width/1.2,
decoration: BoxDecoration(
color: blackTrans,
borderRadius: BorderRadius.all(Radius.circular(5.0)),
),
child:DropdownButtonHideUnderline(
child: ButtonTheme(
alignedDropdown: true,
child: DropdownButton(
value: _dateSelected,
hint: AutoSizeText(NA_FLIGHT_PAGE_DROPDOWN, style: TextStyle(color: white,),textAlign: TextAlign.center,),
isDense: false,
onChanged: (String newValue){
setState(() {
_dateSelected = newValue;
});
},
items: snapshot.data.documents.map((DocumentSnapshot document){
return DropdownMenuItem<String>(
value: document.documentID,
child: AutoSizeText(document.documentID, style: TextStyle(color: white,),textAlign: TextAlign.center,),
);
}).toList(),
),
),
)
)
);
Not sure if this affects anything but I'm using AutoSizeText in order to resize my text dynamically.
Update: I managed to make the Menu items appear in the center by using Center, but the text and hint is still remains to the left tho even with Center... :
// Does not seem to change the hint or text position (when menu item selected)
hint: Center(child:AutoSizeText(NA_FLIGHT_PAGE_DROPDOWN, style: TextStyle(color: white,),textAlign: TextAlign.center,)),
// Changes the menu item to the center instead of left
child: Center(child:AutoSizeText(document.documentID, style: TextStyle(color: white,),textAlign: TextAlign.center,)),
For those who are seeing option to change flutter class dropdown.dart. You don't need to do that.
Do this:
set property isExpanded to true
use Center widget with DropdownMenuItem class children.
IsExpanded will also take care of overflow.
DropdownButton(
isExpanded: true,
value: category_selected,
items: categories.map<DropdownMenuItem<String>>((var value) {
return DropdownMenuItem<String>(
value: value["name"],
child: Center(
child: Text(
value["name"],
textAlign: TextAlign.center,
),
),
);
}).toList(),
),
A simple and straight answer is Not Possible. But there's always a way.
You have to go to the dropdown.dart provided with the flutter package.If you're using VSCode Ctrl+Click on the DrpoDownMenuItem class and change the following code.
#override
Widget build(BuildContext context) {
return Container(
height: _kMenuItemHeight,
alignment: AlignmentDirectional.centerStart,
child: child,
);
}
Change the alignment: AlignmentDirectional.centerStart to alignment: AlignmentDirectional.center and it should work :)
Yes you can do it like the following code sample.
Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(Radius.circular(5))),
margin: EdgeInsets.only(top: 5, bottom: 5),
padding: EdgeInsets.only(right: 5, left: 5),
child: ConstrainedBox(
constraints: const BoxConstraints(
minWidth: double.infinity,
),
child: Container(
child: Center(
child: DropdownButtonHideUnderline(
child: DropdownButton(
iconSize: 0.0,
items: <DropdownMenuItem<int>>[
new DropdownMenuItem(
child: new Text(
'Optional Information',
style: TextStyle(color: Colors.black54),
),
),
new DropdownMenuItem(
child: new Text(
'Male',
style: TextStyle(color: Colors.black54),
),
),
new DropdownMenuItem(
child: new Text(
'Female',
style: TextStyle(color: Colors.black54),
)),
],
onChanged: (V) {},
),
),
),
),
),
),
Here i have used my color and decoration view for my porpose you can change it as per you requirement.
You can use the code below to perfectly position the text in the dropdown at the center and still have the dropdown icon positioned at the right end of your container.
DropdownButtonHideUnderline(
child: DropdownButton(
value: dropdownvalue,
items: items.map((String items) {
return DropdownMenuItem(
value: items,
child: Center(
child: Text(items),
),
);
}).toList(),
onChanged: (String? newValue) {
setState(() {
dropdownvalue = newValue!;
});
},
isExpanded: true,
),
),
So we simply add isExpanded: true, and add a Center() widget to the Text(items) in the DropdownMenuItem().
There is a alignment property in DropdownMenuItem class.
DropdownButton(
//...
isExpanded: true, // this will take all width available
)
If you want to change it's width wrap DropdownButton with SizedBox or Padding.
item: [
DropdownMenuItem(
//...
alignment: AlignmentDirectional.center,
),
]
in your DropdownButtonFormField or DropdownButton
add
isExpanded: true
thing like this
DropdownButtonFormField<String>(isExpanded: true)

How can i change the width/padding of a Flutter DropdownMenuItem in a DropdownButton?

How can we change the width/padding of a Flutter DropdownMenuItem in a Dropdown?
Row(
children: <Widget>[
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
LightText(
text: "Agent",
),
Padding(
padding: EdgeInsets.only(top: 3),
),
Container(
height: 27,
child: Row(
children: <Widget>[
DropdownButtonHideUnderline(
child: DropdownButton<Agent>(
// isExpanded: true,
hint: Text(
agentName == null ? "" : agentName,
style: TextStyle(
fontSize: MediaQuery.of(context).size.width * 0.035,
),
),
value: selectedAgent,
onChanged: (Agent value) async {
selectedAgent = value;
agentName = selectedAgent.getAgentName();
agentId = selectedAgent.getAgentId();
},
items: agentList.map((Agent agent) {
return DropdownMenuItem<Agent>(
value: agent,
child: SizedBox(
width: 25.0,
child: LightText(
text: agent.name,
textColor: Colors.black,
),
),
);
}).toList(),
),
),
],
),
decoration: ShapeDecoration(
shape: RoundedRectangleBorder(
side: BorderSide(width: 1.0, color: lightGrey),
borderRadius: BorderRadius.all(Radius.circular(3.0)),
),
),
),
],
),
),
SizedBox(
width: 30,
),
TextBoxData(
labelText: "% Commission",
controllerText: percentageCommision,
enableVal: true,
borderColor: lightGrey,
)
],
)
Wrap Dropdown button with ButtonTheme and add alignedDropdown = true like:
ButtonTheme(
alignedDropdown: true,
child: DropdownButton(...),
)
alignedDropdown will match the menu items' width with buttons. Then we need specific width, so wrap ButtonTheme with SizedBox or Container:
SizedBox(
width: 25, // Your width for dropdowns
child: ButtonTheme(...),
)
You can control the width/padding of a Flutter DropdownMenuItems in a DropdownButton by wrapping it inside a Container widget.
Then simply assign height, width and padding to that Container widget.
Example is given below:
Widget dropDownButtonsColumn(List<String> list, String hint){
return Padding(
padding: const EdgeInsets.only(left: 40, right: 40 , bottom: 24,top:12),
child: Container(
height: 55, //gives the height of the dropdown button
width: MediaQuery.of(context).size.width, //gives the width of the dropdown button
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(3)),
color: Color(0xFFF2F2F2)
),
// padding: const EdgeInsets.symmetric(horizontal: 13), //you can include padding to control the menu items
child: Theme(
data: Theme.of(context).copyWith(
canvasColor: Colors.yellowAccent, // background color for the dropdown items
buttonTheme: ButtonTheme.of(context).copyWith(
alignedDropdown: true, //If false (the default), then the dropdown's menu will be wider than its button.
)
),
child: DropdownButtonHideUnderline( // to hide the default underline of the dropdown button
child: DropdownButton<String>(
iconEnabledColor: Color(0xFF595959), // icon color of the dropdown button
items: list.map((String value){
return DropdownMenuItem<String>(
value: value,
child: Text(value,
style: TextStyle(
fontSize: 15
),
),
);
}).toList(),
hint: Text(hint,style: TextStyle(color: Color(0xFF8B8B8B),fontSize: 15),), // setting hint
onChanged: (String value){
setState(() {
bankSelected = value; // saving the selected value
});
},
value: bankSelected, // displaying the selected value
),
)
),
),
);
}
Outputs:
With horizontal padding 50, given inside the Container widget:
Hope this helps!!