Skip to content

Instantly share code, notes, and snippets.

@tdcolvin
Last active February 6, 2024 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tdcolvin/47a8f9fd7bb425c54efe3fa46b022a4c to your computer and use it in GitHub Desktop.
Save tdcolvin/47a8f9fd7bb425c54efe3fa46b022a4c to your computer and use it in GitHub Desktop.
@Composable
fun FlashingCountingButton() {
val scope = rememberCoroutineScope()
var buttonColor by remember { mutableStateOf(Color.Red) }
var count by remember { mutableStateOf(1) }
Column {
Button(colors = ButtonDefaults.buttonColors(backgroundColor = buttonColor),
onClick = {
val job = scope.launch {
// launch a separate coroutine, inside this one, to increase the count
launch {
while(true) {
delay(500)
count ++
}
}
//...and here we change the background colour
while(true) {
delay(500)
buttonColor = Color(Random.nextInt(0xFF), Random.nextInt(0xFF), Random.nextInt(0xFF), 0xFF)
}
}
// cancel the above coroutine after 5 seconds
scope.launch {
delay(5_000)
job.cancel()
}
}
) {
Text("Cycle Random Colours (count = $count)")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment