There is a simple piece of code, One button plus three components with random background colors. when I press the button, Two composables that used DrawScope are redrawn again (I think it is due to the ripple effect of the button).
Is it a recomposition!? The Document says :
Recomposition is the process of calling your composable functions again when inputs change. This happens when the function's inputs change. When Compose recomposes based on new inputs, it only calls the functions or lambdas that might have changed, and skips the rest.
How can I prevent this?
private fun randomColor() = Color(
Random.nextInt(256),
Random.nextInt(256),
Random.nextInt(256),
alpha = 255
)
@Preview
@Composable
fun preview() {
Column {
Button(onClick = { /*nothing*/ }) { Text(text = "Do nothing") }
Canvas(modifier = Modifier
.fillMaxWidth().height(100.dp) ){
drawCircle(color = randomColor(), radius = size.minDimension*.4f)
}
Text(modifier = Modifier
.drawBehind {
drawRect(color = randomColor())
}.fillMaxWidth().height(50.dp),
text="Text component with drawBehind")
Text(modifier = Modifier
.background(color = randomColor())
.fillMaxWidth()
.height(50.dp),text="Background color set on the modifier")
}
}