Is there a way to get the height of a composable? ...
# compose
a
Is there a way to get the height of a composable? Not the min and max height but the measured height? Code in thread.
Copy code
BoxWithConstraints {
  TopAppBar(
    ...
  )
}
I would like to get the height of the TopAppBar here and store it in a
remember{ mutableStateOf() }
to use in other parts of the code.
d
You can use
Modifier.onGloballyPositioned
1
b
There is a default available for app bar height as well. AppBarDefaults (as long as your not creating your own composable or causing it to grow in height.
App bar is generally a static height unless it's being animated
c
Depends why you need the size. If you need the size to influence composition of sibling content you can use
SubcomposeLayout
(
Scaffold
is a good example). If you don't need to influence composition you can use
Layout().
Modifier.onSizeChanged()
or
onGloballyPositioned()
can easily result in composition loops, so should only be used as a last resort and with care.
👍 1
1