flutter - Flutter:如何最小化 DropdownButton 的宽度
问题描述
Flutter 中的DropdownButton
宽度根据DropdownMenuItem
它拥有的项目中的最大值设置它的宽度。如果选择了一个小项目,则会在项目和箭头之间留下很多空白。如何使按钮缩放以适合所选项目的宽度?
我尝试使用Expanded
,一些Flexible
东西,但我仍然无法改变它的宽度。
DropdownButton(
value: null,
hint: Text("Small text"),
items: ..., //Text widgets that have more text and are larger than the hint
)
解决方案
用下拉按钮包裹ButtonTheme
并添加alignedDropdown = true
如下:
ButtonTheme(
alignedDropdown: true,
child: DropdownButton(...),
)
alignedDropdown 将菜单项的宽度与按钮相匹配。然后我们需要特定的宽度,所以用 SizedBox 或 Container 包裹 ButtonTheme:
SizedBox(
width: 25, // Your width for dropdowns
child: ButtonTheme(...),
)
推荐阅读
- javascript - 如何创建具有动态变化的列宽的网格布局?
- javascript - 如何在chart.js中用正常数字创建对数刻度?
- c++ - 我可以在主函数中返回一个结构吗?
- javascript - Firebase 函数 api 调用在模拟器上工作但不在生产中
- python - Python中的sqlite3:如何根据适用于其他2列的过滤器来平均列中的值?
- javascript - 为什么 Object3D.getWorldPosition 不只返回一个 THREE.Vector3?
- mongodb - Mongo Source 连接器中的最终投票记录不是实时的
- javascript - 你如何将数据从一个 observable 发送到另一个 observable?
- java - 下载文件时未定义 response.data
- python - python `threading.local` 跨使用`Gunicorn` 和`eventlet` 的请求是否安全?