Vaibhav Jaiswal
03/05/2024, 10:46 AMWindowInsets.statusBar
and WindowInsets.navigationBar
paddings as 0
But when I open that screen again it then gets the correct paddingStylianos Gakis
03/05/2024, 12:13 PMVaibhav Jaiswal
03/05/2024, 12:13 PMStylianos Gakis
03/05/2024, 12:15 PMVaibhav Jaiswal
03/05/2024, 1:45 PMWindowInsets.statusBar.asPaddingValues()
, and its 0,
Window Insets are not being consumed anywhere up the heirarchy, as I am drawing edgeToEdge()
Stylianos Gakis
03/05/2024, 1:46 PMStylianos Gakis
03/05/2024, 1:48 PMStylianos Gakis
03/05/2024, 1:48 PMVaibhav Jaiswal
03/05/2024, 1:58 PMIn composable:
val scrollBehaviour = rememberHideAppBarScrollBehaviour(
height = APP_BAR_HEIGHT.dp,
windowInsets = WindowInsets.statusBars,
hideDirection = HideDirection.UP
)
enum class HideDirection {
UP, DOWN
}
class HideAppBarScrollBehaviour(
private val height: Dp,
density: Density,
windowInsets: WindowInsets,
hideDirection: HideDirection
) {
var heightOffset by mutableStateOf(0f)
private set
private val insetsPaddingPx = when (hideDirection) {
HideDirection.UP -> windowInsets.getTop(density)
HideDirection.DOWN -> windowInsets.getBottom(density)
}
private val insetsPadding = with(density) {
insetsPaddingPx.toDp()
}
private val heightPx = with(density) {
height.toPx() + insetsPaddingPx
}
/**
* A value that represents the collapsed height percentage of the app bar.
* A 0.0 represents a fully expanded bar, and 1.0 represents a fully collapsed bar (computed as heightOffset / heightOffsetLimit).
*/
val collapsedFraction: Float
get() = (if (heightPx != 0f) heightOffset / heightPx
else 0f).let { if (it < 0) -it else it }
val nestedScrollConnection = object : NestedScrollConnection {
override fun onPreScroll(available: Offset, source: NestedScrollSource): Offset {
val delta = available.y
val newOffset = heightOffset + delta
heightOffset = newOffset.coerceIn(-heightPx, 0f)
return Offset.Zero
}
}
@Composable
fun contentPaddingAnimated() = animateDpAsState(
targetValue = (height + insetsPadding) * (1 - collapsedFraction)
)
}
@Composable
fun rememberHideAppBarScrollBehaviour(
height: Dp,
windowInsets: WindowInsets,
hideDirection: HideDirection
): HideAppBarScrollBehaviour {
val density = LocalDensity.current
return remember {
HideAppBarScrollBehaviour(
height = height,
density = density,
windowInsets = windowInsets,
hideDirection = hideDirection
)
}
}
This is how I am using itVaibhav Jaiswal
03/05/2024, 1:59 PMStylianos Gakis
03/05/2024, 2:07 PM@Composable
fun rememberHideAppBarScrollBehaviour(
height: Dp,
windowInsets: WindowInsets,
hideDirection: HideDirection
): HideAppBarScrollBehaviour {
val density = LocalDensity.current
return remember {
HideAppBarScrollBehaviour(
height = height,
density = density,
windowInsets = windowInsets,
hideDirection = hideDirection
)
}
}
If any of the inputs change you simply do not re-create the HideAppBarScrollBehaviour
class with the new information. It retains whatever stale information you had before.
Is this what you want here?Vaibhav Jaiswal
03/05/2024, 2:08 PMval scrollBehaviour = rememberHideAppBarScrollBehaviour(
height = APP_BAR_HEIGHT.dp,
windowInsets = WindowInsets.statusBars,
hideDirection = HideDirection.UP
)
Stylianos Gakis
03/05/2024, 2:09 PMVaibhav Jaiswal
03/07/2024, 8:58 AM