Flutter - 如何从 DropdownButton 对齐所选项目?

spe*_*ght 14 alignment drop-down-menu flutter

我无法将 DropdownButton 中的所选项目与中心对齐。

我已经尝试过在 DropdownMenuItem 下的 child: Center(),它能够对齐这些项目,但是在我选择了其中一个项目后,所选项目立即与左侧对齐。我还想将所选项目与中心对齐。

有谁知道如何实现它?

提前致谢。

无法将所选项目与中心对齐

_dropdownValues:

final List<String> _dropdownValues = [
  "One",
  "Two12345",
  "Three123456789",
  "Four",
  ];

  String _selectedValue;
Run Code Online (Sandbox Code Playgroud)

在小部件构建下:

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,
                ),
              ),
            ),
          ],
        ),
      ),
Run Code Online (Sandbox Code Playgroud)

Sve*_*ven 14

如果您不介意设置固定宽度,您可以将您的DropdownMenuItem Text孩子包裹在SizedBox. 然后将textAlign属性设置为TextAlign.center,如下所示:

DropdownButton(
  // ...
  items: _dropdownValues.map((value) => DropdownMenuItem(
    child: SizedBox(
      width: 100.0, // for example
      child: Text(value, textAlign: TextAlign.center),
    ),
    value: value,
  )).toList(),
  // ...
),
Run Code Online (Sandbox Code Playgroud)


Wat*_*ame 8

以防有人偶然发现这个问题。您需要指定宽度,这可以通过使用 的selectedItemBuilder属性来实现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(),
    ),
  );
}
Run Code Online (Sandbox Code Playgroud)

来自这里的代码

  • 这应该是正确的答案 - 接受的答案不会对齐所选项目,仅对齐列表。这个答案可以完成任务。谢谢! (2认同)

Mah*_*eja 7

只需将Center作为父项添加到 DropdownMenuItem 的子项

   DropdownButton(
      items: genderList
          .map((string) => DropdownMenuItem(
                child: Center(
                  child: Text(
                    string,
                    style: Theme.of(context).textTheme.bodyText2,
                  ),
                ),
              ))
          .toList(), 
      
      onChanged: (value) {  },
    )
Run Code Online (Sandbox Code Playgroud)