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

Nat Strangerweather

10/11/2020, 10:24 AM
Hello, I made a custom switch with a number of size parameters. I would like to reuse that composable, but smaller. Is there a way of assigning a scale to the composable, or do I have to pass each size parameter along with the other function params? Here is what the switch looks like:
Copy code
@Composable
fun CustomToggle(
    color: Color, gradientColor: Color, selected: Boolean,
    onSelected: () -> Unit
) {

    val toggleAlign = animate(if (selected) 65.dp else 5.dp)

    Box() {
        Box(
            modifier = Modifier
                .height(40.dp)
                .width(100.dp)
                .clip(RoundedCornerShape(50))
                .background(HorizontalGradient(
                    0.0f to color,
                    0.5f to gradientColor,
                    1.0f to color,
                    startX = 0.0f,
                    endX = with(DensityAmbient.current) { 100.dp.toPx() }
                )
                ),
        ) {}
        Surface(
            modifier = Modifier.size(30.dp)
                .align(Alignment.CenterStart)
                .offset(toggleAlign)
                .clip(RoundedCornerShape(50))
                .toggleable(value = selected, onValueChange = { onSelected() }),
            color = Color.White,
            elevation = 7.dp
        ) {}
    }
}
s

Se7eN

10/11/2020, 11:24 AM
Maybe you can add a scale parameter and do
100.dp * scale
,
40.dp * scale
...
Or if dp doesn't support multiplication then you can do
(100 * scale).dp
n

Nat Strangerweather

10/11/2020, 11:28 AM
Aaahh, yes, good idea! 🙂
👌 1
Thanks!
🙂 1
g

gildor

10/11/2020, 2:20 PM
You also can sale it using drawing scope + scale, I believe, but I think Ashar's suggestion is better, it's more flexible at least
👍 1
3 Views