compose-collapsing-toolbar icon indicating copy to clipboard operation
compose-collapsing-toolbar copied to clipboard

Wrong initial scroll position when toolbar has dynamic height

Open Veeksi opened this issue 11 months ago • 2 comments

My toolbar is built with ConstraintLayout and it depends on WindowInsets so I can calculate statusBarHeight like this:

toolbar = {
    val paddingValues = WindowInsets.systemBars.asPaddingValues()

    Box(
        modifier = Modifier
            .padding(top = paddingValues.calculateTopPadding())
            .fillMaxWidth()
            .height(300.dp),
        contentAlignment = Alignment.BottomCenter
    ) {
        Text("Hello")
    }
}

When my toolbar container has dynamic height, the initial scrolling position is wrong because it doesn't care about dynamic statusBarHeight. You can reproduce the issue by placing those components into toolbar scope.

I am using scrollStrategy = ScrollStrategy.ExitUntilCollapsed,

You can see the actual problem in the video: https://streamable.com/i4gwqm

Veeksi avatar Mar 01 '24 10:03 Veeksi

Looks like PR #101 fixes this issue

Veeksi avatar Mar 01 '24 11:03 Veeksi

   @Composable
   fun rememberStatusBarHeightDp(): Dp {
       val view = LocalView.current
       val density = LocalDensity.current

       val statusBarHeightPx = remember {
           ViewCompat.getRootWindowInsets(view)
               ?.getInsets(WindowInsetsCompat.Type.statusBars())
               ?.top ?: 0
       }

       return with(density) { statusBarHeightPx.toDp() }
   }

Use this code for top padding

masterdev01 avatar Jun 03 '24 12:06 masterdev01