Skip to content

Instantly share code, notes, and snippets.

@atomofiron
Last active October 5, 2023 09:12
Show Gist options
  • Save atomofiron/010c57d75ef42cb972ec4fcaed584e54 to your computer and use it in GitHub Desktop.
Save atomofiron/010c57d75ef42cb972ec4fcaed584e54 to your computer and use it in GitHub Desktop.
Insets content padding
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.union
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalLayoutDirection
LazyColumn(contentPadding = insetsPadding { systemBars + displayCutout }) {
}
operator fun WindowInsets.plus(other: WindowInsets) = union(other)
@Composable
inline fun insetsPadding(
crossinline block: @Composable WindowInsets.Companion.() -> WindowInsets,
): PaddingValues = WindowInsets.Companion.block().insetsPadding()
@Composable
fun WindowInsets.insetsPadding(): PaddingValues {
return LocalDensity.current.run {
val direction = LocalLayoutDirection.current
PaddingValues(
start = getLeft(this, direction).toDp(),
top = getTop(this).toDp(),
end = getRight(this, direction).toDp(),
bottom = getBottom(this).toDp(),
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment