To do this, the Button has an argument interactionSource. It can be used as follows:
val interactionSource = remember { MutableInteractionSource() }
val isPressed by interactionSource.collectIsPressedAsState()
Button(
onClick = { /*TODO*/ }
interactionSource = interactionSource,
) {
}
If you need to perform some action until the button is released, you can use isPressed with LaunchedEffect:
if (isPressed) {
LaunchedEffect(Unit) {
// execute some action
}
}
It is launched in a coroutine scope, which will be canceled as soon as isPressed becomes false.
An other option is using it with DisposableEffect:
if (isPressed) {
DisposableEffect(Unit) {
// do some action
onDispose {
// cancel some action
}
}
}