有没有办法计算动态尺寸的小部件?

发布于 2025-02-13 23:04:06 字数 172 浏览 0 评论 0原文

如果具有动态大小(宽度)的技能小部件超过屏幕宽度,我想添加一个自定义的Showmore小部件。单击Showmore小部件时,应该显示包装中的所有技能。else显示较少。

“在此处输入图像说明”

I'd like to add a custom showMore widget if the skills widget which has a dynamic size(width) exceeds the screen width. On clicking the showMore widget it should show all the skills in a wrap .else show less.

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城花音 2025-02-20 23:04:06

在颤抖中

约束下降,大小上升

请参见此文档

Flutter使用a 单个Pass AlgorithM 渲染您的申请。这是确保性能的技术选择,但它具有限制。

其中之一是,当您构建小部件树时,您只能访问父级的约束,而不访问任何小部件的大小(因为它们尚未渲染)。

因此,对您的问题的简短答案是:

不,您无法做自己想做的事情(如果某些小部件不合适在屏幕上,则显示某些内容),因为您无法访问构建方法中的任何尺寸。

另一种解决方案是使用Wrap包装芯片或在水平轴上使用ListView,以使芯片列表水平滚动。


无论如何,如果您真的想这样做,则可以用MediaQuery.of(context).Size或使用LayoutBuilder 使用contrapts.maxwidth作为父宽度。然后,您可以检查numberofchips * chipsize< = maxWidth。但是我不建议这样做,因为设计不会响应:

  • 所有芯片的尺寸都相同,因此您最终会得到“ C”的大筹码,也许是像“ Python” Won'这样的长名字。 T适合,您最终会遇到溢出问题。
  • 如果用户更改设备的字体尺寸怎么办?您还将遇到溢出问题。

In flutter

Constraints go down and sizes go up

See this documentation.

Flutter uses a single pass algorithm to render your application. This is a technical choice to ensure performance but it comes with limitations.

One of them is that, when you are building the widget tree, you only have access to the constraints of the parent, and not any size of any widget (since they are not rendered yet).

So a short answer to your question is:

No, you cannot do what you are trying to do (displaying something if some widgets are not fitting on the screen) since you don't have access to any sizes in the build method.

An alternative solution would be to use Wrap to wrap your chips or use a ListView on the horizontal axis to make the list of chips horizontally scrollable.


Anyway, if you really want to do this, you can hardcode the sizes of your chip and access the device size with MediaQuery.of(context).size or by using the LayoutBuilder and using contraints.maxWidth as the parent's width. Then you can check whether or not numberOfChips * chipSize <= maxWidth. But I wouldn't recommend it as the design wouldn't be responsive:

  • All the chips will have the same size, so you'll end up with a big chip for "c" and maybe a long name like "python" won't fit in and you'll end up with overflow issues.
  • What if the user changes the font size of his device? You will also end up with overflow issues.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文