spe*_*ght 14 alignment drop-down-menu flutter
我无法将 DropdownButton 中的所选项目与中心对齐。
我已经尝试过在 DropdownMenuItem 下的 child: Center(),它能够对齐这些项目,但是在我选择了其中一个项目后,所选项目立即与左侧对齐。我还想将所选项目与中心对齐。
有谁知道如何实现它?
提前致谢。
_dropdownValues:
final List<String> _dropdownValues = [
  "One",
  "Two12345",
  "Three123456789",
  "Four",
  ];
  String _selectedValue;
在小部件构建下:
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,
                ),
              ),
            ),
          ],
        ),
      ),
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(),
  // ...
),
以防有人偶然发现这个问题。您需要指定宽度,这可以通过使用 的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(),
    ),
  );
}
来自这里的代码
只需将Center作为父项添加到 DropdownMenuItem 的子项
   DropdownButton(
      items: genderList
          .map((string) => DropdownMenuItem(
                child: Center(
                  child: Text(
                    string,
                    style: Theme.of(context).textTheme.bodyText2,
                  ),
                ),
              ))
          .toList(), 
      
      onChanged: (value) {  },
    )
| 归档时间: | 
 | 
| 查看次数: | 14592 次 | 
| 最近记录: |