LazyColumn, how to add window insets padding to my padding?

1.1k Views Asked by At

I have a LazyColumn where I have a contentPadding = PaddingValues(16.dp)

Now, I want to add navigation bar height to the bottom padding, as to achieve the "edge to edge" effect

so contentPadding = WindowInsets.navigationBars.asPaddingValues()

But how do I add these two together?

i.e.

LazyColumn(
        contentPadding = WindowInsets.navigationBars.asPaddingValues() + PaddingValues(16.dp),

I created this

@Composable
operator fun PaddingValues.plus(paddingValues: PaddingValues): PaddingValues {
    val layoutDirection = LocalLayoutDirection.current
    return PaddingValues(
        start = calculateStartPadding(layoutDirection) + paddingValues.calculateStartPadding(layoutDirection),
        top = calculateTopPadding() + paddingValues.calculateTopPadding(),
        end = calculateEndPadding(layoutDirection) + paddingValues.calculateEndPadding(layoutDirection),
        bottom = calculateBottomPadding() + paddingValues.calculateBottomPadding()
    )
}

But it feels wrong

1

There are 1 best solutions below

0
On

Prior to .asPaddingValues(), WindowInsets can be added to existing WindowInsets through the WindowInsets.add() extension function.

val padding: Dp = 16.dp

val contentPadding: PaddingValues = WindowInsets(
    left = padding,
    top = padding,
    right = padding,
    bottom = padding,
)
    .add(WindowInsets.navigationBars)
    .asPaddingValues()

LazyColumn(
    contentPadding = contentPadding,
    ...
)

Unfortunately, there is no convenient WindowInsets constructor analogous to PaddingValues(all: Dp) at this time. Google uses this convention in the Now In Android repository on this specific line.