LazyColumn
/ LazyRow
の引数で登場する contentPadding: PaddingValues
で複数の PaddingValues
を加算したい時のコード例です。
実装
Scaffold(
topBar = {
TopAppBar()
}
) { contentPadding ->
val layoutDirection = LocalLayoutDirection.current
LazyColumn(
contentPadding = PaddingValues(
top = 16.dp + contentPadding.calculateTopPadding(),
start = contentPadding.calculateStartPadding(layoutDirection),
end = contentPadding.calculateEndPadding(layoutDirection),
bottom = 16.dp + contentPadding.calculateBottomPadding(),
),
) {
...
}
}
PaddingValues#calculateXXXPadding()
で持っている padding を取得できるので、加算したい値をプラスして PaddingValues
に新たに渡すことで加算しています。
汎用的にするなら
@Composable
operator fun PaddingValues.plus(other: PaddingValues): PaddingValues {
val layoutDirection = LocalLayoutDirection.current
return PaddingValues(
top = calculateTopPadding() + other.calculateTopPadding(),
start = calculateStartPadding(layoutDirection) + other.calculateStartPadding(layoutDirection),
end = calculateEndPadding(layoutDirection) + other.calculateEndPadding(layoutDirection),
bottom = calculateBottomPadding() + other.calculateBottomPadding(),
)
}
このような拡張関数を用意しておくと次のように実装することができます。
Scaffold(
topBar = {
TopAppBar()
}
) { contentPadding ->
LazyColumn(
contentPadding = contentPadding + PaddingValues(vertical = 16.dp)
) {
...
}
}