I am unable to align the selected item from DropdownButton to the center.
I have tried child: Center() under the DropdownMenuItem, it is able to align those items however after I have selected one of the items, the selected item was aligned to the Left immediately. I would also like to align the selected item to the Center.
Anyone knows how to achieve it?
Thanks in advance.
Unable to align selected item to Center
_dropdownValues:
final List<String> _dropdownValues = [
"One",
"Two12345",
"Three123456789",
"Four",
];
String _selectedValue;
Under Widget Build:
body: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
child: Text('Name:',),
),
Container(
child: Center(
child: DropdownButton(
hint: Text('Select ...'),
items: _dropdownValues.map((value) => DropdownMenuItem(
child: Center( child: Text(value), ),
value: value,
)).toList(),
onChanged: (String value) {
setState(() {
this._selectedValue = value;
});
},
isExpanded: false,
value: _selectedValue,
),
),
),
],
),
),
You need to specify width and this can be achieved by using selectedItemBuilder property of DropdownButton . This should be the correct answer - the accepted one does not align the selected item, only the list. This answer does the job. Thanks!
To change the dropdown Button color in Flutter, simply wrap your DropdownButton widget inside the Container and provide the styling instructions inside the decoration property using the BoxDecoration widget. Using the color property (insideBoxDecoration) you can assign the color.
If you don't mind setting a fixed width you can wrap your DropdownMenuItem
Text
child in a SizedBox
. Then you set the textAlign
property to TextAlign.center
, like so:
DropdownButton(
// ...
items: _dropdownValues.map((value) => DropdownMenuItem(
child: SizedBox(
width: 100.0, // for example
child: Text(value, textAlign: TextAlign.center),
),
value: value,
)).toList(),
// ...
),
Incase someone stumble upon this issue. You need to specify width and this can be achieved by using selectedItemBuilder
property of DropdownButton
.
final List<String> items = <String>['1','2','3'];
String selectedItem = '1';
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 12.0),
child: DropdownButton<String>(
value: selectedItem,
hint: Container(
alignment: Alignment.centerRight,
width: 180,
child: Text("Hint text", textAlign: TextAlign.end)
),
onChanged: (String string) => setState(() => selectedItem = string),
selectedItemBuilder: (BuildContext context) {
return items.map<Widget>((String item) {
return Container(
alignment: Alignment.centerRight,
width: 180,
child: Text(item, textAlign: TextAlign.end)
);
}).toList();
},
items: items.map((String item) {
return DropdownMenuItem<String>(
child: Text('Log $item'),
value: item,
);
}).toList(),
),
);
}
code from here
just add Center as parent to child of DropdownMenuItem
DropdownButton(
items: genderList
.map((string) => DropdownMenuItem(
child: Center(
child: Text(
string,
style: Theme.of(context).textTheme.bodyText2,
),
),
))
.toList(),
onChanged: (value) { },
)
You probably won't need this now but if anyone is wondering how I found the following solution.
DropdownButton(
hint: Align(alignment: Alignment.centerRight, child: Text('any text')),
...
items: _dropdownValues.map((item) {
return DropdownMenuItem(
child: new Align(alignment: Alignment.centerRight, child: Text(item)),
value: item,);
}).toList(),
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With