有没有办法计算动态尺寸的小部件?
如果具有动态大小(宽度)的技能小部件超过屏幕宽度,我想添加一个自定义的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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在颤抖中
请参见此文档。
Flutter使用a 单个Pass AlgorithM 渲染您的申请。这是确保性能的技术选择,但它具有限制。
其中之一是,当您构建小部件树时,您只能访问父级的约束,而不访问任何小部件的大小(因为它们尚未渲染)。
因此,对您的问题的简短答案是:
不,您无法做自己想做的事情(如果某些小部件不合适在屏幕上,则显示某些内容),因为您无法访问构建方法中的任何尺寸。
另一种解决方案是使用
Wrap
包装芯片或在水平轴上使用ListView
,以使芯片列表水平滚动。无论如何,如果您真的想这样做,则可以用
MediaQuery.of(context).Size
或使用LayoutBuilder 使用
contrapts.maxwidth
作为父宽度。然后,您可以检查numberofchips * chipsize< = maxWidth
。但是我不建议这样做,因为设计不会响应:In flutter
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 aListView
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 theLayoutBuilder
and usingcontraints.maxWidth
as the parent's width. Then you can check whether or notnumberOfChips * chipSize <= maxWidth
. But I wouldn't recommend it as the design wouldn't be responsive: