Skip to content

Instantly share code, notes, and snippets.

@Farhandroid
Last active August 31, 2023 06:34
Show Gist options
  • Save Farhandroid/0d5e8d34ce205fdf1a2bf2b24062ef87 to your computer and use it in GitHub Desktop.
Save Farhandroid/0d5e8d34ce205fdf1a2bf2b24062ef87 to your computer and use it in GitHub Desktop.
AnimateAsStateExample.kt
@Composable
fun AnimateAsStateExample() {
// State to control the background color
var boxColor by remember { mutableStateOf(Color.Blue) }
// Animate the background color
val backgroundColor by animateColorAsState(targetValue = boxColor, label = "")
// Box with clickable modifier to change the color
Box(
modifier = Modifier
.size(200.dp)
.background(backgroundColor)
.clickable {
// Toggle color between Blue and Green when clicked
boxColor = if (boxColor == Color.Blue) Color.Green else Color.Blue
}
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment