admin管理员组

文章数量:1392002

I have this extension function written over a modifier

fun Modifier.ignoreParentPadding(padding: Dp) = this.layout { measurable, constraints ->
    val placeable = measurable.measure(
        constraints.copy(
            maxWidth = constraints.maxWidth + (padding.roundToPx() * 2),
        ),
    )
    layout(placeable.width, placeable.height) {
        placeable.place(0, 0)
    }
}

This is how i use it

ComposeMessagePart(
            modifier = Modifier.ignoreParentPadding(smallMediumUnit),
            messageOwner = selectedMessageOwner,
        )

I have noticed that there are unnecessary compositions that occur for ComposeMessagePart composable , and it reports that each time it recieves a different LayoutElement .

I do not see this happen if i do not use the extension function rather directly do the following

  ComposeMessagePart(
            modifier = Modifier.layout { measurable, constraints ->
                val placeable = measurable.measure(
                    constraints.copy(
                        maxWidth = constraints.maxWidth + (smallMediumUnit.roundToPx() * 2),
                    ),
                )
                layout(placeable.width, placeable.height) {
                    placeable.place(0, 0)
                }
            },
            messageOwner = selectedMessageOwner,
        )

The code above works fine and no unneccesary recomposition is seen. I have not yet being able to rationalize as to why do recompositions happen in the ComposeMessagePart if the same code runs as an extension vs in-place .

Any idea folks ?

本文标签: android jetpack composeModifier extension causes unnecessary recompositionsStack Overflow