PaddingValues is since alph12 just an interface wh...
# compose
t
PaddingValues is since alph12 just an interface what makes it very difficult to add or substract values. E.g. i want to add a FAB padding to the content padding of my LazyColumn. How can i do it now?
My current approach is following:
Copy code
val ld = LocalLayoutDirection.current
val paddingFab = PaddingValues(
    start = padding.calculateStartPadding(ld),
    top = padding.calculateTopPadding(),
    end = padding.calculateEndPadding(ld),
    bottom = padding.calculateBottomPadding() + fabHeight
)
g
I’d probably create my own
plus
operator, but I agree it seems like something that should be provided out of the box.
Copy code
operator fun PaddingValues.plus(other: PaddingValues): PaddingValues = TODO()
Then you could
+
your paddings together:
Copy code
val padding = PaddingValues(...)
val fabPadding = PaddingValues(bottom = ...)
val result = padding + fabPadding
t
Copy code
@Composable
fun PaddingValues.add(start: Dp = 0.dp, top: Dp = 0.dp, end: Dp = 0.dp, bottom: Dp = 0.dp): PaddingValues {
    val ld = LocalLayoutDirection.current
    return PaddingValues(
        start = calculateStartPadding(ld) + start,
        top = calculateTopPadding() + top,
        end = calculateEndPadding(ld) + end,
        bottom = calculateBottomPadding() + bottom
    )
}
g
oh right, it needs to be composable to get the layout direction
t
But it does not look good when i do have to calculate this values each time i add some thing. Also i do have to create a new insance of PaddingValues. When i do this inside of a LazyColumn item it is not optimal.
👍 1
a
Copy code
fun PaddingValues.add(start: Dp = 0.dp, top: Dp = 0.dp, end: Dp = 0.dp, bottom: Dp = 0.dp): PaddingValues {
    return object: PaddingValues {
        override fun calculateLeftPadding(layoutDirection: LayoutDirection) =
            this@add.calculateLeftPadding(layoutDirection) +
                if (layoutDirection == LayoutDirection.Ltr) start else end

        override fun calculateTopPadding(): Dp =
            this@add.calculateTopPadding() + top

        override fun calculateRightPadding(layoutDirection: LayoutDirection) =
            this@add.calculateRightPadding(layoutDirection) +
                if (layoutDirection == LayoutDirection.Ltr) end else start

        override fun calculateBottomPadding() =
            this@add.calculateBottomPadding() + bottom
    }
}
as a non composable option
👍 1
🧙 1
t
Yes but still it is a pain to work with this new system. Would be better to have two different padding interfaces one which is layout direction aware. And another which is not. And just use a simple data class for both.
a
the reason for it is we want to allow passing both rtl-specific and absolute paddings into all the components which accept PaddingValues. otherwise we would have to add a separate LazyColumn overload, for example, to only support absolute paddings