英文:
Is there a way in Flutter dropdown_button2 version < 2.0.0 to customize the button and the dropdown separately?
问题
在Flutter中,对于dropdown_button2
版本小于2.0.0
,我想分别自定义按钮和下拉菜单。具体来说,我想给按钮一个固定宽度,同时让下拉菜单的宽度根据下拉菜单中最长项的文本来自动调整。请注意,我正在使用版本1.7.0
。
我查看了版本2.0.0
,但它与之前版本不同。我们无法使用它。我们需要继续使用版本1.7.0
。
英文:
In Flutter dropdown_button2
version < 2.0.0
, I want to customize the button and the dropdown separately. Specifically, I want to give the button a fixed width, while letting the dropdown width expand and shrink to fit the text of the longest item in the dropdown. Please note that I'm using version 1.7.0
I looked at version 2.0.0
and it's different. We can't use it. We need to keep using version 1.7.0
.
答案1
得分: 0
我通过根据下拉框中最长文本的动态计算宽度来解决了这个问题。类似于以下代码:
dropdownWidth: longestItemWidth > minItemWidth
? longestItemWidth
: minItemWidth
我通过将所有项目的长度提取到一个数组中并对其进行排序来计算longestItemWidth
,然后我选择数组中的最后一个项目,这将是最长的项目。
英文:
I solved the problem by dynamically calculating the width of the drop down depending on the longest text in the drop down. Somethng like:
dropdownWidth: longestItemWidth > minItemWidth
? longestItemWidth
: minItemWidth
I calculated the longestItemWidth by exracting the length of all items into an array and sortig it. Then I picked the last item in the array which will be the longest.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论