How to center text in DropdownButton? - flutter

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)

Related

Flutter, how to decrease width of DropdownButtonFormField active menu?

I have a DropdownButtonFormField inside a Container. When I click it, selections open and the width of that menu take all of the width of screen, because of container. I want to decrease the width of that menu.
Here, you see in picture I want to make it half of screen width.
my code:
Center(
child: Container(
margin: EdgeInsets.only(top: _height * .02),
height: _height * .066,
width: _width * .9,
child: DropdownButtonFormField(
borderRadius: BorderRadius.circular(10),
focusColor: Colors.black,
icon: Image.asset("assets/Arrow down.png"),
value: chosen,
dropdownColor: Colors.white,
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(5))),
items: dropdownList
.map((e) => DropdownMenuItem(
child: Text(
e,
textAlign: TextAlign.center,
style:
TextStyle(color: Colors.grey, fontSize: 13),
),
value: e,
))
.toList(),
onChanged: (String? value) {
setState(() {
chosen = value;
});
},
),
),
),
To decrease the width of the active menu in a DropdownButtonFormField in Flutter, you can wrap it in a Container widget and specify the width for that container. Here's an example:
Container(
width: 100,
child: DropdownButtonFormField(
items: [
DropdownMenuItem(
child: Text('Option 1'),
value: 'Option 1',
),
DropdownMenuItem(
child: Text('Option 2'),
value: 'Option 2',
),
DropdownMenuItem(
child: Text('Option 3'),
value: 'Option 3',
),
],
onChanged: (value) {
// Handle the value change
},
),
)

Remove default padding of DropDownButton in 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

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(),

How to make a widget available after a selection in dropdown?

I want to create a card after a user makes a selection in drop down how to achieve this in flutter. The UI model is given below.
The first card is like a permanent one but the second card should appear after if there is any change in the dropdown button.
The lower card should be scrollable and the size should be dynamic like a listview. if the Key and data increases the value should increase.
The Code I have used for creating the app bar and the 1st card is here.
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
toolbarHeight: 75,
title: Center(
child: Text(
'Flutter App',
style: GoogleFonts.lato(
textStyle:
TextStyle(fontWeight: FontWeight.bold, fontSize: 26)),
),
),
),
body: ListView(children: [
Column(
children: [
Container(
height: 250,
child: Column(children: [
Card(
child: Column(
children: [
Row(children: [
Padding(
padding: EdgeInsets.only(left: 30, top: 15),
child: Text(
'Text here',
style: GoogleFonts.poppins(
textStyle: TextStyle(
fontWeight: FontWeight.w700,
fontSize: 37,
)),
textAlign: TextAlign.left,
),
),
]),
Padding(
padding: EdgeInsets.only(top: 30),
child: Text(
'Some text here',
style: GoogleFonts.lato(
textStyle: TextStyle(fontSize: 18)),
),
),
Padding(
padding: EdgeInsets.only(top: 30),
child: DropdownButton(
items: markets
.map<DropdownMenuItem<String>>((String val) {
return DropdownMenuItem<String>(
value: val, child: Text(val));
}).toList(),
onChanged: (sto) {
setState(() {
_mySelection = sto;
});
retrievedata.getPrice(_mySelection);
},
value: _mySelection,
hint: Text('Hint text here'),
),
),
],
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
elevation: 5,
margin: EdgeInsets.all(10),
),
]),
),
],
),
]),
);
}
You can wrap the Card that should appear after dropDownChanges to Visibility widget.
Add this variable
bool _isVisible = false;
Make changes to DropdownButton
DropdownButton(
...
onChanged: (sto) {
setState(() {
_mySelection = sto;
_isVisible = true;
});
retrievedata.getPrice(_mySelection);
},
...
),
Wrap the Card with Visiblity
Visibility (
visible: _isVisible,
child: Card(
child: ....
),
),
If you want the card to be always visible and enable interaction only after the DropdownButton changes, wrap the card to IgnorePointer instead of visibility.
You can achieve this by having an additional state that is responsible for visibility of second card. Something like this:
final visible = false;
#override
Widget build(BuildContext context) {
return
...
onChanged: (sto) {
setState(() {
_mySelection = sto;
visible = true;
});
retrievedata.getPrice(_mySelection);
},
...
And later on we can make the second card visible based on this value:
visible ? SecondCard(
...
) : SizedBox.shrink()
This ensures that nothing is shown until visible is set to true

flutter drop-down list - retrieve selected value and not in a box format

I have made a drop-down list as in the following screen
enter image description here
Here:
I want the drop-down to be in a box like other TextFormFields and arrow to be bigger
my drop-down list background seems to be blurred and not visible
I want the selected value to be replaced on "Choose the id proof"
Following is my code:
Container(
padding: EdgeInsets.symmetric(horizontal: 20),
color: Colors.white,
child: _hintDown(),
),
SizedBox(height: 10),
my dropdown code is:
DropdownButton _hintDown() => DropdownButton<String>(
items: [
DropdownMenuItem<String>(
value: "1",
child: Text(
"Aadhar Card",
),
),
DropdownMenuItem<String>(
value: "2",
child: Text(
"Pancard",
),
),
],
onChanged: (value) {
print("value: $value");
},
hint: Text(
"Choose the id proof!",
style: TextStyle(
color: Colors.black,
),
),
);
Is this code enough or do I need to go for some other?
There are certain changes in the code as per the requirements. You must visit the flutter documentation and read about using DropdownButton class to achieve what you want
See the code clearly, and implement the code in your code base
// look at the comments too to understand the functionality
String dropdownValue = 'Choose the id proof!';
// Return a widgt container to give the border
// and then wrap it around your DropdownButton
Widget _hintDown() => Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.grey),
borderRadius: BorderRadius.circular(8.0)
),
child: Padding(
padding: EdgeInsets.all(5.0),
child: DropdownButton<String>(
value: dropdownValue,
icon: Icon(Icons.arrow_drop_down),
iconSize: 30, //this inicrease the size
elevation: 16,
style: TextStyle(color: Colors.black),
// this is for underline
// to give an underline us this in your underline inspite of Container
// Container(
// height: 2,
// color: Colors.grey,
// )
underline: Container(),
onChanged: (String newValue) {
setState(() {
dropdownValue = newValue;
});
},
items: <String>['Choose the id proof!', 'Adhaar Card', 'Pancard', 'Voter card', 'Passport']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
)
)
);
Result