Folks, is it possible to take an action on hovered state changes, comparable to the way
clickable { onClick() }
works? I have two composables. I want one of them to "notice" when the other one is hovered. Had I something like
clickable
I could do this by calling a method on the model. I tried using pointerInputs for exit and enter, but they behave very strangely, sending exit/enter pairs for each mouse move within the bounds, and never sending an exit when leaving the bounds.
z
Zach Klippenstein (he/him) [MOD]
01/29/2025, 8:31 PM
easiest way would probably be to collect the interactions emitted from the
MutableInteractionSource
yourself, or observe state changes on the state returned by
MutableInteractionSource.collectHoveredAsState
Zach Klippenstein (he/him) [MOD]
01/29/2025, 8:37 PM
e.g.
Copy code
@Composable fun YourComposable() {
val interactionSource1 = remember { MutableInteractionSource() }
val interactionSource2 = remember { MutableInteractionSource() }
val hovered1 by interactionSource1.collectIsHoveredAsState()
LaunchedEffect(Unit) {
snapshotFlow { hovered1 }.collect {
// TODO: Perform action when interactionSource1 changes hovered state
}
} // etc for 2
YourHoverable(interactionSource = interactionSource1)
YourHoverable(interactionSource = interactionSource2)
}
You could collect interactions yourself but it's a little more work since you have to save