Skip to content

Instantly share code, notes, and snippets.

@ilyasipek
Created January 18, 2024 18:20
Show Gist options
  • Save ilyasipek/b092294474f563981f58cdc517c037ec to your computer and use it in GitHub Desktop.
Save ilyasipek/b092294474f563981f58cdc517c037ec to your computer and use it in GitHub Desktop.
CounterAnimationRow
@Composable
fun CounterAnimationRow(
count: Int,
modifier: Modifier = Modifier,
charComposable: @Composable RowScope.(Char) -> Unit,
) {
var oldCount by remember { mutableIntStateOf(count) }
val increasingValueContentTransform = remember {
slideInVertically { -it } + fadeIn() togetherWith slideOutVertically { it } + fadeOut()
}
val decreasingValueContentTransform = remember {
slideInVertically { it } + fadeIn() togetherWith slideOutVertically { -it } + fadeOut()
}
SideEffect {
oldCount = count
}
Row(modifier) {
count.toString().forEach { digit ->
AnimatedContent(
targetState = digit,
transitionSpec = {
if (oldCount < count) {
increasingValueContentTransform
} else {
decreasingValueContentTransform
}.using(
SizeTransform(clip = false)
)
},
) { target ->
charComposable(target)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment