change the flutter dropdown to textalign left - flutter

how to make the dropdown to the left, because what i get is the dropdown in the middle
DropdownButton<String>(
value: _chosenValue2,
//elevation: 5,
style: TextStyle(color: Colors.black),
items: <String>[
'Personal Type',
'Other',
].map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
hint: Text(
"Customer Model",
style: TextStyle(
color: Colors.black,
fontSize: 16,
fontWeight: FontWeight.w600),
),
onChanged: (String value) {
setState(() {
_chosenValue2 = value;
});
},
),

Try below code hope its helpful to you. Add your dropdown widget inside Container and set Alignment.centerLeft
Container(
// padding: EdgeInsets.all(5), if you required some padding to dropdown to set padding
alignment:Alignment.centerLeft,
child: DropdownButton<String>(
value: _chosenValue2,
//elevation: 5,
style: TextStyle(color: Colors.black),
items: <String>[
'Personal Type',
'Other',
].map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
hint: Text(
"Customer Model",
style: TextStyle(
color: Colors.black,
fontSize: 16,
fontWeight: FontWeight.w600),
),
onChanged: (String value) {
setState(() {
_chosenValue2 = value;
});
},
),
),
Your result screen->

You could use quite a few approaches. You could wrap your drop-down widget in a Container or Align widget and set their alignment property to Alignment.bottomLeft or Alignment.centerLeft or Alignment.topLeft. You could also wrap your dropdown widget inside just a Container and then use margin property to move your widget like margin: EdgeInsets.only(right: 20) or to any value of your desire. Lastly, you could also wrap your widget with a Padding widget and set the padding property to an extent you want your drop-down widget to move leftwards like padding: const EdgeInsets.only(right: 50)

Related

isExpanded making text and icon inside invisible in Flutter

we're trying to do a dropdownbutton in flutter that has the arrow icon static all the way to the right of the button. We've tried doing isExpanded, but that just makes the text and icon dissapear from the box. We don't know what we're doing wrong.
Here is the code for our button:
padding:
const EdgeInsets.only(left: 30, right: 30),
//VIKTIG
child: DropdownButtonHideUnderline(
child: DropdownButton(
hint: Text(
chosenActivity == null
//TODO: Hint text "aktivitet" i mitten av knappen?
? 'Välj aktivitet'
: chosenActivity.name,
style: Styles.h5,
textAlign: TextAlign.center,
),
//isExpanded: false,
value: dropdownvalue,
items: _activities.map((Activity activity) {
return DropdownMenuItem(
value: activity,
child: Text(activity.name),
);
}).toList(),
// After selecting the desired option,it will
// change button value to selected value
onChanged: (Object? newValue) {
setState(() {
chosenActivity = newValue!;
});
print(chosenActivity.name);
},
icon: const Padding(
//Icon at tail, arrow bottom is default icon
padding: EdgeInsets.only(left: 30),
child: Icon(
Icons.arrow_circle_down_sharp,
color: Colors.black,
)),
iconEnabledColor: Colors.white,
//Icon color
style: const TextStyle(
//te
color: Colors.black, //Font color
fontSize:
20 //font size on dropdown button
),
),
),
),
We want the icon to be here without it moving:
[1]: https://i.stack.imgur.com/0tQ1b.png
Sorry if this is unclear,
Thanks!

change dropdown text colour from selected text

i have a dropdown which contain list of text, then i want have different color of select text and color the text from the dropdown item.
DropdownButton<String>(
hint: Text(
'Topic',
style: TextStyle(fontFamily: 'Cairo', fontSize: 20),
),
isExpanded: true,
value: dropdownValue,
dropdownColor: Colors.black,
elevation: 4,
//notices 1
style: const TextStyle(
color: Colors.deepPurple, fontFamily: 'Cairo'),
onChanged: (String? newValue) {
setState(() {
dropdownValue = newValue;
});
},
items: tpoicList
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Container(
// alignment: Alignment.centerRight,
child: Text(
value,
style: const
//notices 2
TextStyle(color: Colors.black),
),
),
);
}).toList(),
)
i notices that in the notice 1 style is also affect the notice 2 style. All i want to is to have different color in the text dropdown and selected text
what i am trying to archive
In the image you will notices the background color is different from the text color also if i select a item from the select item, the text should be in another colors
The DropdownButton has a selectedItemBuilder parameter that should solve your problem. Add something like this to your code:
selectedItemBuilder: (context) {
final List<DropdownMenuItem<String>> list = [];
for (int i = 0; i < tpoicList.lenght; i++) {
list.add(DropdownMenuItem<String>(
value: tpoicList[i],
child: Container(
child: Text(
value,
style: i == dropdownValue?
[YourNewTextStyle],
const TextStyle(color: Colors.black),
),
),
));
}
return list;
},
[official] change dropdown text colour from selected text
selectedItemBuilder: (BuildContext context) {
return tpoicList.map((String value) {
return Align(
alignment: Alignment.centerLeft,
child: Text(
dropdownValue,
style: const TextStyle(color: Colors.white),
),
);
}).toList();
},

How to remove the elevation in DropdownButton?

help me to remove this elevation in DropdownButton, I tried using elevation = 0, but nothing happened or if you have better widget to display item like DropdownButton please show me.
thank you
To remove the underline we should wrap the DropdownButton widget with the DropdownButtonHideUnderline widget.
The elevation property will make the drop-down layer elevated when it's in the open state.
Example
DropdownButtonHideUnderline(
child: DropdownButton(
style: TextStyle(color: Colors.black),
items: <String>[
'Android',
'IOS',
'Flutter',
'Python',
].map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
hint: Text(
"Please choose a langauage",
style: TextStyle(
color: Colors.black,
fontSize: 16,
fontWeight: FontWeight.w600),
),
onChanged: (value) {
print(value);
},
),
),

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

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)