New Compose Multiplatform components arrived on Composables UICheck it out →

Modifier in Compose Ui

pointerInput

Common

Create a modifier for processing pointer input within the region of the modified element.

Last updated:

Installation

dependencies {
   implementation("androidx.compose.ui:ui:1.7.0-beta04")
}

Overloads

@Deprecated(PointerInputModifierNoParamError, level = DeprecationLevel.ERROR
@Suppress(
    "UNUSED_PARAMETER",
    "UnusedReceiverParameter",
    "ModifierFactoryUnreferencedReceiver"

fun Modifier.pointerInput(
    block: suspend PointerInputScope.() -> Unit
)

fun Modifier.pointerInput(
    key1: Any?,
    block: suspend PointerInputScope.() -> Unit
)

fun Modifier.pointerInput(
    key1: Any?,
    key2: Any?,
    block: suspend PointerInputScope.() -> Unit
)

fun Modifier.pointerInput(
    vararg keys: Any?,
    block: suspend PointerInputScope.() -> Unit
)

Code Examples

keyedPointerInputModifier

@Sampled
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)
                    }
                }
        )
    }
}

rememberedUpdatedParameterPointerInputModifier

@Sampled
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)
                    }
                }
        )
    }
}