So there are 2 ways to get the size of the contain...
# compose
a
So there are 2 ways to get the size of the container, one is the
Modifier.onSizeChanged
(or globally positioned) and the other one is to use
BoxWithConstraints
, but I am not sure when to use which. Can someone provide examples of use-cases of both of these. The examples should be such that only one of these is a perfect fit for that scenario. Thanks :)
z
This isn’t an example, but might help figure out when to use which: you can’t use
onSizeChanged
if you need the size in order to compose the first frame of your content – the size won’t be available until after the measure pass, which only happens after the first composition pass. And on the other side of that, if you only need to know the size later, e.g. when handling a particular event, then
BoxWithConstraints
is unnecessary and may cause recompositions you don’t need since it will recompose every time your constraints change.
c
Seems like any app which is going to have layouts for different screen/resolution will likely have a BoxWithConstraints at the root of each of it's screen layouts though right? I guess I'm just asking because when I first read your message I was like "oh no. BoxWithConstraints is expensive and avoided", but I realize that was just me misinterpreting your message
a
Note that you only need
BoxWithConstraints
if you want to have difference structures for different sizes. If you just want to for example change the sizes of children according to parent size, using a custom layout is more performant.
☝🏻 1