在构建阶段如何使用其他小部件的约束和大小 [英] How to use the constraints and sizes of other widgets during the build phase

查看:77
本文介绍了在构建阶段如何使用其他小部件的约束和大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想确保涵盖所有情况

  1. 父级小部件读取(并可能使用)孩子的大小或约束

  1. A Parent widget reads (and possibly uses) a child's size or constraints

子级"小部件读取(并可能使用)父级的大小或约束

A Child widget reads (and possibly uses) a parent's size or constraints

子级"小部件读取(并可能使用)另一个子级的大小或约束

A Child widget reads (and possibly uses) another child's size or constraints

解决方案似乎是...

The solution seems to be to...

  1. 让构建阶段运行

  1. Let the build phase run

然后构建我要从中检索数据的小部件的大小和约束

Then the size and constraints of the widget I want to retreive data from are built

然后我中断常规的相序(因此并条机功能不运行)

Then I interrupt the regular sequence of phases (so the draw Frame function does not run)

然后使用我现在拥有的数据重新运行构建阶段

Then rerun the build phase with the data I now have

并允许画框

问题是我不知道该怎么做

Problem is I don't know how to do this

我早些时候发布了一个与此相关的更具体的问题,并且得到了一些响应,布局生成器,自定义多子布局和自定义单子布局...

I posted a much more specific question earlier relating to this and I got a couple of responses, layout builder, custom multi child layout, and custom single child layout...

但这是针对非常特殊的情况

But these are for very specific circumstances

布局"构建器构建可依赖于父窗口小部件大小的窗口小部件树.所以它只能处理情况2

The Layout builder builds a widget tree that can depend on the parent widget's size. So It only handles case 2

另外两个具有局限性,即父母的大小不能取决于孩子的大小.因此,似乎暗示它仅处理情况2.

And the other two have the limitation that the size of the parent cannot depend on the size of the child. So it seems to imply that it only handles case 2.

那还有什么其他解决方案呢?

So what other solutions are out there?

rect getter插件可以正常工作,但不会中断并条机,因此会出现口吃的情况 https://pub.dartlang.org/packages/rect_getter

The rect getter plugin works but does not interupt the draw Frame so there is a frame of stutter https://pub.dartlang.org/packages/rect_getter

我还没有尝试过的after布局插件,但是似乎发生了同样的问题,因为它说它在第一帧之后运行 https://pub.dartlang.org/packages/after_layout

The after layout plugin I have not tried yet but it seems like the same issue occurs because it says it runs after the first frame https://pub.dartlang.org/packages/after_layout

感谢您的帮助! 我讨厌不得不不断地使用变通方法:/

Any help is appreciated! I would hate to have to just live with using work arounds constantly :/

推荐答案

小部件不能故意这样做. 窗口小部件应该从不,取决于大小和位置或任何内容.甚至没有他们的孩子.

Widgets can't do such thing on purpose. A Widget should never depend on the size and position or anything. Not even their children.

如果您遇到需要的情况,则LayoutBuilder在大多数情况下应该没问题.

If you ever face a situation where you need to, LayoutBuilder should be fine most of the time.

如果不是,则要创建的不是Widget.但是RenderObject却是颤振的下层之一.

If it's not, what you want to create isn't a Widget. But a RenderObject instead, one of the lower layer of flutter.

CenterStackColumn之类的小部件实际上是使用RenderObject计算的.

Widgets such as Center, Stack or Column are in fact computed using RenderObject.

这篇关于在构建阶段如何使用其他小部件的约束和大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆