sindrenm
04/02/2025, 12:34 PMAnimateVisibility
around the items would do the trick, but the arrangement spacing is still applied unless the item is completely gone, and that spacing doesn't animate nicely, either.
Code for this sample in thread. 🧵.sindrenm
04/02/2025, 12:34 PM@Preview
@Composable
private fun AnimatedVisibilityInListPreview() {
Surface {
Column(
modifier = Modifier.padding(16.dp),
verticalArrangement = Arrangement.spacedBy(16.dp),
horizontalAlignment = Alignment.CenterHorizontally,
) {
var isSecondItemVisible by remember { mutableStateOf(true) }
Text(
text = "First Item",
modifier = Modifier
.background(Color.LightGray)
.padding(8.dp),
)
AnimatedVisibility(isSecondItemVisible) {
Text(
text = "Second Item",
modifier = Modifier
.background(Color.LightGray)
.padding(8.dp),
)
}
Button(onClick = { isSecondItemVisible = !isSecondItemVisible }) {
Text("Toggle Second Item Visibility")
}
}
}
}
sindrenm
04/02/2025, 12:35 PMdorche
04/02/2025, 12:52 PMSpacer()
s ? That's usually my preferred way of doing this, rather than padding modifiers.sindrenm
04/02/2025, 12:53 PMStylianos Gakis
04/02/2025, 1:25 PM