Is there a way to make items in columns or rows wi...
# compose
s
Is there a way to make items in columns or rows with a main axis spacing arrangement animate nicely in and out? Ideally just using
AnimateVisibility
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. 🧵.
Copy code
@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")
            }
        }
    }
}
I know that adding padding modifiers to each item would solve it, but that is just plain annoying to do, and doesn't feel right semantically, either.
d
I don't think there's a way to do it with arrangement but have you considered using
Spacer()
s ? That's usually my preferred way of doing this, rather than padding modifiers.
s
That feels wrong for the same reason as using padding modifiers. Ideally, I would just tell my Column to put x dp space between each item.
s
When there's animation at play, I share the required spacings between the items, so that the space is part of the item itself. I hate it, but haven't found a better way to do this.
🙁 1
1