Hello, I am using `Layout` to wrap 2 buttons to cr...
# compose
n
Hello, I am using
Layout
to wrap 2 buttons to create a custom layout, one is
OutlinedButton
and another is
Button
. But when I pressing on the
Button
, the whole custom layout starting flash, it seems the whole Layout keep recompose(I add the log in Layout’s MeasurePolicy lamda and it keeps printing). However, it doesn’t happen if I press on
OutLineButton
. How could I avoid this? Thanks. It only happened when I wrap the Button in another Composable, if I just use Button directly(in first level), no issue Is it because when button is pressed, the size change so trigger some recomposition? I found the root cause, if set Button’s
elevation
to
null
, the problem is gone. But is there method that keep elevation and let Layout works as expected? 🤔
f
What do you mean by "_the whole custom layout starting flash_"? And could you provide some code sample of your layout and the buttons?
n
Thanks for reply! This is the code:
Copy code
@Composable
fun MyBodyContent() {
    MyLayout {
        MyButton(onClick = {})
        OutlinedButton(onClick = {}) {
            Text("OutlinedButton")
        }
    }
}

@Composable
fun MyButton(
    onClick: (() -> Unit)
) {
    Button(
        onClick = onClick
    ) {
        Text(text = "Button")
    }
}

@Composable
fun MyLayout(
    modifier: Modifier = Modifier,
    content: @Composable () -> Unit
    ) {
        Layout(
            modifier = modifier,
            content = content
        ) { measurables, constraints ->
            val placeables = measurables.map {
                it.measure(constraints)
            }

            // Track the x co-ord we have placed children up to
            var xPosition = 0
            val buttonHeight = placeables.first().height
            layout(constraints.maxWidth, buttonHeight) {
                placeables.forEach { placeable ->
                    // Position item on the screen
                    placeable.placeRelative(x = xPosition, y = 0)

                    // Record the y co-ord placed up to
                    xPosition += placeable.width
                }
            }
        }
}
When click the
Button
, something wrong with it, it starts to shift and gone
But when I use button directly the issue won’t happened
I found the root cause, if I set Button’s elevation to null, the issue disappears.