I just wanted to sense check my usage of `BoxWithC...
# compose
c
I just wanted to sense check my usage of
BoxWithConstraints
Feels I might need to use a Layout/SubcomposeLayout instead, but I’m not sure I’m using the maxWidth and maxHeight to calculate sizes and positions for items on a “grid”
For anyone curious, it’s to produce a football lineup for different formations
a
Your instincts are sound, I would use
Layout
for this. Specifically I would use parent data to carry the coordinates in the grid and define a custom modifier function to declare the coordinates of each child within this layout.
c
Thanks Adam, are there are good resource on using
Layout
? It’s still one of those mysterious Compose concepts for me currently
I’ve rebuilt this using Layout (I think in the way you suggested) Could anyone explain why this is better than using
BoxWithConstraints
though? Since it feels very similar
a
You've got the idea. 🙂 Maybe have the extension return the
PositionData
rather than individual elements of it, so you can obtain it from the measurable once instead of fetching/casting it for every field, and if you want to be industrial strength about it you can define a receiver scope interface that offers an appropriate modifier factory function
Layout happens at a later stage after composition has already completed. It's only responsible for measuring and placing UI elements that were already composed.
BoxWithConstraints
forces going back to the full composition => measure => layout => draw sequence for the children when only measure => layout => draw is needed.
BoxWithConstraints
is only needed if you want to change what you actually compose based on available space, such as switching between different layouts for phone vs. tablet or desktop screen sizes.
Using
BoxWithConstraints
when
Layout
can accomplish what you want will always be less efficient since there's more work involved.
c
That makes sense, thanks for all the help Adam 👍