Here is a simple example to reproduce the issue I'm having:
@Composable
fun AnimatedVisibilityEx(modifier: Modifier = Modifier) {
var visible by remember { mutableStateOf(true) }
Box(modifier = modifier.fillMaxSize()) {
Column(horizontalAlignment = Alignment.CenterHorizontally) {
Box(
modifier = Modifier
.fillMaxWidth()
.weight(1f)
.background(Color.LightGray)
.border(width = 2.dp, color = Color.Blue, shape = CircleShape)
)
AnimatedVisibility(
modifier = Modifier.weight(1f),
visible = visible
) {
Box(
modifier = Modifier
.fillMaxWidth()
.background(MaterialTheme.colorScheme.primary)
)
}
Box(
modifier = Modifier
.fillMaxWidth()
.weight(1f)
.background(Color.LightGray)
.border(width = 2.dp, color = Color.Red, shape = CircleShape)
)
Button(
onClick = { visible = !visible },
modifier = Modifier.padding(top = 32.dp)
) {
Text(text = "Toggle Visible")
}
}
}
}
I have 3 boxes with equal weights. The middle one is going away on button click and I want a nice animation - the middle one shrinks and the other 2 expand to equally take up the space. However, there is no animation running. When I remove the weights and give them heights then everything works fine. What's the problem and how to fix it?