Hi, if I need to know size of composable, I can use:
•
BoxWithConstraints
•
Box
with
Modifier.onSizeChanged{...}
When to use which one? What is better for performance?
➕ 2
p
Pablichjenkov
05/18/2023, 5:16 AM
You also have onGloballyPositioned{} and onPlaced{} modifiers. I am curious to know too
l
Loney Chou
05/18/2023, 7:25 AM
Depends on whether you need that to determine the content, like responsive layout, or using it in some modifiers. If you only need to know the size, use
Modifier.onSizeChanged
. Otherwise, use
BoxWithConstraints
. The latter one, however, comes with performance penalty. You could also change the content in those modifier callbacks, this way it would trigger another recomposition, which sometimes is not a good thing.
a
Albert Chang
05/18/2023, 7:29 AM
Also note that if you don't need the size in composition phase, e.g. you just want to determine children sizes basing on parent size instead of showing different content depending on parent size, a custom layout is better.
Albert Chang
05/18/2023, 7:31 AM
But you could also change the content in those modifier callbacks, this way it would trigger another recomposition, which sometimes is not a good thing.