如何在Dropdown中更改Flutter DropdownMenuItem的宽度/填充? [英] How can we change the width/padding of a Flutter DropdownMenuItem in a Dropdown?

查看:2582
本文介绍了如何在Dropdown中更改Flutter DropdownMenuItem的宽度/填充?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Flutter中,我可以使用DropdownMenuItems构建一个Dropdown,如下所示:

In Flutter, I can build a Dropdown with DropdownMenuItems, like this:

我添加的DropdownMenuItem总是比下拉菜单本身宽:

The DropdownMenuItems I add are always wider than the dropdown itself:

如何调整DropdownMenuItem的宽度,或删除多余的水平填充?

How do you adjust the width of the DropdownMenuItem, or remove the extra horizontal padding?

我的DropdownMenuItem小部件如下所示:

My DropdownMenuItem widget looks like this:

DropdownMenuItem(
    value: unit.name,
    child: Text('hey'),
);

而我的Dropdown小部件看起来像这样:

while my Dropdown widget looks like this:

return Container(
    width: 300.0,
    child: DropdownButtonHideUnderline(
      child: DropdownButton(
        value: name,
        items: listOfDropdownMenuItems,
        onChanged: onChanged,
        style: Theme.of(context).textTheme.title,
      ),
    ),
);

推荐答案

已添加此功能.参见 https://github.com/flutter/flutter/pull/14849

您现在可以将其包装在ButtonTheme中,并将alignedDropdown设置为true.

You can now wrap it in a ButtonTheme and set alignedDropdown to true.

return Container(
    width: 300.0,
    child: DropdownButtonHideUnderline(
      child: ButtonTheme(
        alignedDropdown: true,
          child: DropdownButton(
            value: name,
            items: listOfDropdownMenuItems,
            onChanged: onChanged,
            style: Theme.of(context).textTheme.title,
         ),
      ),
    ),
);

这篇关于如何在Dropdown中更改Flutter DropdownMenuItem的宽度/填充?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆