07-21-2023, 12:04 PM
I don't understand how `LayoutBuilder` is used to get the height of a widget.
I need to display the list of Widgets and get their height, so I can compute some special scroll effects. I am developing a package and other developers provide a widget (I don't control them). I read that LayoutBuilder can be used to get the height.
In a very simple case, I tried to wrap a widget in LayoutBuilder.builder and put it in the stack, but I always get `minHeight` `0.0`, and `maxHeight` `INFINITY`. Am I misusing the LayoutBuilder?
It seems that LayoutBuilder is a no go. I found the [CustomSingleChildLayout][1] which is almost a solution.
I extended that delegate, and I was able to get the height of widget in `getPositionForChild(Size size, Size childSize)` method. *but*, the first method that is called is `Size getSize(BoxConstraints constraints)` and as constraints, I get 0 to INFINITY because I'm laying these CustomSingleChildLayouts in a ListView.
My problem is that SingleChildLayoutDelegate `getSize` operates like it needs to return the height of a view. I don't know the height of a child at that moment. I can only return *constraints.smallest* (which is 0, and the height is 0), or *constraints.biggest* which is infinity and crashes the app.
In the documentation it even says:
> ...but the size of the parent cannot depend on the size of the child.
And that's a weird limitation.
[1]:
I need to display the list of Widgets and get their height, so I can compute some special scroll effects. I am developing a package and other developers provide a widget (I don't control them). I read that LayoutBuilder can be used to get the height.
In a very simple case, I tried to wrap a widget in LayoutBuilder.builder and put it in the stack, but I always get `minHeight` `0.0`, and `maxHeight` `INFINITY`. Am I misusing the LayoutBuilder?
It seems that LayoutBuilder is a no go. I found the [CustomSingleChildLayout][1] which is almost a solution.
I extended that delegate, and I was able to get the height of widget in `getPositionForChild(Size size, Size childSize)` method. *but*, the first method that is called is `Size getSize(BoxConstraints constraints)` and as constraints, I get 0 to INFINITY because I'm laying these CustomSingleChildLayouts in a ListView.
My problem is that SingleChildLayoutDelegate `getSize` operates like it needs to return the height of a view. I don't know the height of a child at that moment. I can only return *constraints.smallest* (which is 0, and the height is 0), or *constraints.biggest* which is infinity and crashes the app.
In the documentation it even says:
> ...but the size of the parent cannot depend on the size of the child.
And that's a weird limitation.
[1]:
[To see links please register here]