Hi, if I need to know size of composable, I can us...
# compose
e
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
You also have onGloballyPositioned{} and onPlaced{} modifiers. I am curious to know too
l
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
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.
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.
It's always bad. See this thread.
l
Yeah, I don't recommend doing that as well.