I want to make sure I cover all cases
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...
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
And allow the frame to draw
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
The Layout builder builds a widget tree that can depend on the parent widget's size. So It only handles case 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?
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
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 :/
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.
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 aRenderObject
instead, one of the lower layer of flutter.Widgets such as
Center
,Stack
orColumn
are in fact computed usingRenderObject
.