Create a modifier for processing pointer input within the region of the modified element.
fun keyedPointerInputModifier() {
@Composable
fun MyComposable(parameter: String) {
Box(
Modifier.fillMaxSize().pointerInput(parameter) {
// This entire pointerInput block will restart from the beginning
// if and when `parameter` changes, since it's used as a key in
// the creation of the `pointerInput` modifier
detectTapGestures { performAction(parameter) }
}
)
}
}
fun rememberedUpdatedParameterPointerInputModifier() {
@Composable
fun MyComposable(parameter: String) {
val currentParameter by rememberUpdatedState(parameter)
Box(
Modifier.fillMaxSize().pointerInput(Unit) {
// This pointerInput block will never restart since
// it specifies a key of `Unit`, which never changes
detectTapGestures {
// ...however, currentParameter is updated out from under this running
// pointerInput suspend block by rememberUpdatedState, and will always
// contain the latest value updated by the composition when a tap
// is detected here.
performAction(currentParameter)
}
}
)
}
}