https://kotlinlang.org logo
#compose
Title
# compose
t

Timo Drick

02/18/2021, 1:27 PM
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

grandstaish

02/18/2021, 1:51 PM
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

Timo Drick

02/18/2021, 1:52 PM
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

grandstaish

02/18/2021, 1:54 PM
oh right, it needs to be composable to get the layout direction
t

Timo Drick

02/18/2021, 1:54 PM
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

Andrey Kulikov

02/18/2021, 2:39 PM
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

Timo Drick

02/18/2021, 3:21 PM
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

Andrey Kulikov

02/18/2021, 4:07 PM
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
20 Views