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

Denis

03/04/2021, 1:50 AM
Is there a way to change the order of drawing for deep descendants (not direct children) of the same parent? I'm trying
.zIndex
, but it only works "for the children of the same layout parent". Code in the thread.
🚫 1
Copy code
@Preview
@Composable
fun ZIndexPreview() {
    val z1 = Modifier.zIndex(1f)
    val z2 = Modifier.zIndex(2f)
    val z3 = Modifier.zIndex(3f)

    Box(contentAlignment = Alignment.Center) {
        SomeBox("Magenta", Color.Magenta, textModifier = z3, modifier = z1)
        JustText("Blue", Color.Blue, modifier = z2.offset(x = 20.dp, y = 40.dp))
    }
}

@Composable
fun JustText(text: String, color: Color, modifier: Modifier) {
    Text(
        text,
        fontSize = 40.sp,
        modifier = modifier
            .background(color, shape = RoundedCornerShape(50))
            .padding(5.dp),
        color = Color.White
    )
}

@Composable
fun SomeBox(text: String, color: Color, textModifier: Modifier, modifier: Modifier) {
    Box(modifier.background(Color.Cyan).requiredSize(200.dp), contentAlignment = Alignment.Center) {
        JustText(text, color, modifier = textModifier)
    }
}
I want the Magenta to be drawn over the Blue. And Blue should be visible avobe the Cyan box.