A slightly modified version of the @Thracian solution which uses an extension function and replaces deprecated calls:
```
fun Modifier.pointerPressRelease(
key: Any?,
onPress: (PressInteraction.Press) -> Unit,
onRelease: (PressInteraction.Release) -> Unit
): Modifier = this then Modifier.pointerInput(key) {
coroutineScope {
awaitEachGesture {
val down = awaitFirstDown()
down.consume()
val press = PressInteraction.Press(down.position)
onPress(press)
val up = waitForUpOrCancellation()
if (up != null) {
up.consume()
onRelease(PressInteraction.Release(press))
}
}
}
}
@Composable
private fun InteractionSample() {
val interactionSource = remember { MutableInteractionSource() }
val onClick: () -> Unit = {
Log.d("TAG", "I want to receive the clicks from the red box")
}
Box {
Box(
Modifier
.size(200.dp)
.background(Color.Blue)
.clickable(
interactionSource = interactionSource,
indication = rememberRipple(),
onClick = onClick
)
) {}
Box(
Modifier
.size(200.dp)
.offset(x = 190.dp)
.alpha(0.2F)
.background(Color.Red)
.pointerPressRelease(
key = Unit,
onPress = { interactionSource.tryEmit(it) },
onRelease = {
interactionSource.tryEmit(it)
onClick()
}
)
) {}
}
}
```
If you want red one to have ripple effect when you touch blue Composable you can use a shared `InteractionSource` to trigger interaction for different composables
// 🔥 This interaction source is set by inner composable to trigger ripple on outer one
val interactionSource = MutableInteractionSource()
val coroutineScope = rememberCoroutineScope()
Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center
) {
Surface(
modifier = Modifier
.size(200.dp)
.clickable(
interactionSource = interactionSource,
indication = rememberRipple(),
onClick = {
}
),
color = Color.Red
) {}
Surface(
modifier = Modifier
.size(50.dp)
.clickable {
coroutineScope.launch {
val press = PressInteraction.Press(Offset.Zero)
interactionSource.emit(
press
)
interactionSource.emit(
PressInteraction.Release(press)
)
}
},
color = Color.Blue
) {}
}
The issue you will get here with blue Composable's ripple is it will start from Offset.Zero. You can set center of blue Composable manually. I don't know if there is a way to get exact position of press from InteractionSource or Interaction.