Is there a way in Flutter dropdown_button2 version < 2.0.0 to customize the button and the dropdown separately?

huangapple go评论122阅读模式
英文:

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 &gt; 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.

huangapple
  • 本文由 发表于 2023年3月7日 12:11:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75657977.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定