Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - How to align selected item from DropdownButton?

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,
                ),
              ),
            ),
          ],
        ),
      ),
like image 763
speedlight Avatar asked Apr 23 '19 07:04

speedlight


People also ask

How do you align text in DropdownButton in flutter?

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!

How do you customize DropdownButton in flutter?

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.


4 Answers

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(),
  // ...
),
like image 64
Sven Avatar answered Oct 22 '22 14:10

Sven


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

like image 45
WatsMyName Avatar answered Oct 22 '22 15:10

WatsMyName


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) {  },
    )
like image 45
Mahmoud Abu Alheja Avatar answered Oct 22 '22 15:10

Mahmoud Abu Alheja


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(),
)
like image 36
Ouardia Avatar answered Oct 22 '22 15:10

Ouardia