Last active
February 6, 2024 13:06
-
-
Save tdcolvin/47a8f9fd7bb425c54efe3fa46b022a4c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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