简体   繁体   English

Flutter:如何最小化 DropdownButton 的宽度

[英]Flutter: How to minimize width of DropdownButton

The DropdownButton in Flutter sets its width based on the largest DropdownMenuItem from the items it has. Flutter 中的DropdownButton根据其拥有的项目中最大的DropdownMenuItem设置其宽度。 That leaves a lot of white space between the item and the arrow if a small item is selected.如果选择了一个小项目,则在项目和箭头之间留下很多空白。 How do I make the button scale to just fit the selected item's width?如何使按钮缩放以适合所选项目的宽度?

I tried using Expanded , some Flexible stuff and I still can't make it change its width.我尝试使用Expanded和一些Flexible的东西,但我仍然无法改变它的宽度。

DropdownButton(
    value: null,
    hint: Text("Small text"),
    items: ..., //Text widgets that have more text and are larger than the hint
)

I am trying to get rid of that space underlined between the hint and the arrow:我试图摆脱hint和箭头之间带下划线的空间: 在此处输入图像描述

Wrap Dropdown button with ButtonTheme and add alignedDropdown = true like:ButtonTheme包装 Dropdown 按钮并添加alignedDropdown = true ,如:

ButtonTheme(
  alignedDropdown: true,
  child: DropdownButton(...),
)

alignedDropdown will match the menu items' width with buttons. alignedDropdown 将菜单项的宽度与按钮相匹配。 Then we need specific width, so wrap ButtonTheme with SizedBox or Container:然后我们需要特定的宽度,所以用 SizedBox 或 Container 包裹 ButtonTheme:

SizedBox(
  width: 25, // Your width for dropdowns
  child: ButtonTheme(...),
)

You could try wrapping it in a ButtonTheme and setting alignedDropdown: true 您可以尝试将其包装在ButtonTheme中并设置alignedDropdown: true

return Container(
    width: 300.0,
    child: DropdownButtonHideUnderline(
      child: ButtonTheme(
        alignedDropdown: true,
          child: DropdownButton(
            value: null,
            hint: Text("Small text"),
            items: ..., //Text widgets that have more text and are larger than the hint
            onChanged: onChanged,
            style: Theme.of(context).textTheme.title,
         ),
      ),
    ),
);

this works:这有效:

new Container(
            padding: const EdgeInsets.fromLTRB(10.0, 5, 10, 0),
            child: new Container(
              padding: const EdgeInsets.fromLTRB(10, 5, 5, 5),
              decoration: new BoxDecoration(
                  color: Colors.white,
                  borderRadius: new BorderRadius.only(
                      topLeft: const Radius.circular(5.0),
                      bottomLeft: const Radius.circular(5.0),
                      bottomRight: const Radius.circular(5.0),
                      topRight: const Radius.circular(5.0))),
              child: new Center(
                  child: new Column(children: [
                new DropdownButton(
                  underline: Text(''),
                  icon: Icon(Icons.keyboard_arrow_down),
                  hint: new Text('Small text'),
                  style: TextStyle(
                    color: Colors.white30,
                  ),
                  isExpanded: true,
                  value: _selectedLocation,
                  onChanged: (String newValue) {
                    setState(() {
                      _selectedLocation = newValue;
                    });
                  },
                  items: _locations.map((String location) {
                    return new DropdownMenuItem<String>(
                      value: location,
                      child: new Text(
                        location,
                        style: TextStyle(
                            color: myColour, fontWeight: FontWeight.bold),
                      ),
                    );
                  }).toList(),
                ),
              ])),
            ))

If I understand your problem correctly then I have also encountered it and I have a solution.如果我正确理解了您的问题,那么我也遇到了它并且我有解决方案。 Simply make a Wrap widget as the parent of your DropdownButton.只需将 Wrap 小部件作为 DropdownButton 的父级即可。 It will make the dropdown menu wrap to the size of the value (not sure how it would react with the null value though)它将使下拉菜单环绕到值的大小(但不确定它会如何与空值反应)

Wrap(children: <Widget>[DropdownButton(
value: null,
hint: Text("Small text"),
items: //Text widgets that have more text and are larger than the hint
)])

到目前为止,我能够做到的方式是限制下拉列表中项目的宽度。

The width is actually determined by the widest item provided by the selectedItemBuilder argument (which uses the items if not defined).宽度实际上由 selectedItemBuilder 参数提供的最宽项目确定(如果未定义,则使用项目)。 With this little trick I managed to have it working:通过这个小技巧,我设法让它工作:

    DropdownButton<T>(
      value: value,
      selectedItemBuilder: (ctx) => [for (final item in items) value], 
      items: items,
      onChanged: (value) => ... update value

The selectedItemBuilder in the example produces a list of repeated items (the one corresponding to the selected value).示例中的 selectedItemBuilder 生成重复项的列表(与所选值对应的项)。 You will need a stateful widget to handle the value.您将需要一个有状态的小部件来处理该值。

to remove under line warp Dropdown with DropdownButtonHideUnderline使用 DropdownButtonHideUnderline 删除下线变形 Dropdown

I managed to remove the whitespace by defining alignment: Alignment.centerRight .我设法通过定义alignment: Alignment.centerRight来删除空格。 By default, alignment is set to Alignment.centerStart which forces the Text to align start while the Icon is set to align end.默认情况下,alignment 设置为Alignment.centerStart ,这会强制文本对齐开始,而图标设置为对齐结束。 So by aligning the text center right it fills the whitespace that appears between the text and icon.因此,通过将文本居中对齐,它会填充出现在文本和图标之间的空白区域。 Full Code Sample:完整代码示例:

DropdownButton(
    alignment: Alignment.centerRight,
    child: .....
     )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM