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

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

问题描述

我想确保我涵盖所有情况

I want to make sure I cover all cases

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

  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

解决方案似乎是……

  1. 让构建阶段运行

  1. Let the build phase run

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

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

然后我打断常规的阶段序列(因此draw Frame函数不运行)

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

我还没有尝试过后布局插件,但似乎发生了同样的问题,因为它说它在第一帧之后运行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代替,flutter的下层之一.

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天全站免登陆