How to use the constraints and sizes of other widgets during the build phase

百般思念 提交于 2019-12-02 08:01:59

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

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

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

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

I'm the author of rect getter plugin , and I quite intrested in your qestion.

Yes , as other guys said, I think flutter is a very flexible UI framwork, so we do not need know a widget's actual size in most case. But when I tried to write a anime effect whitch simulate open a book, I found it was necessary to know a 'Card''s rect information, and finally I found the way to get a rect from a rended object from the source code of heros.dart. As a result , it became the rect getter plugin whitch you known.

In fact, maybe I'm facing the same problem with you, because I want to write a 'Masonry' layout like this, so I should know the previous items's rect infomation to decide where the current item should be drawn.

My solution is : 1.Wrap the child widget by rect_getter, build the layout normally first; 2.And then, use timer to get the child's rect information immediately after build complete; 3.Call setState() to rebuild the layout, and use children's rect information to calculate each child's constraintbox in viewport.

Code fragment (NOT finished) :

and current progress:

My codes can found here, but they were so terrible and commented use Chinese, so I don't think you will want to read them.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!