Build apps faster with our new App builder! Check it out →

pointerInput

Common

Modifier in Compose Ui

Last updated:

Installation

dependencies {
   implementation("androidx.compose.ui:ui:1.8.0-alpha04")
}

Overloads

@Suppress("UNUSED_PARAMETER", "UnusedReceiverParameter", "ModifierFactoryUnreferencedReceiver")
@Deprecated(PointerInputModifierNoParamError, level = DeprecationLevel.ERROR)
fun Modifier.pointerInput(block: suspend PointerInputScope.() -> Unit): Modifier
@Deprecated(
    "This function is deprecated. Use the PointerInputEventHandler block variation instead",
    level = DeprecationLevel.HIDDEN,
    replaceWith =
        ReplaceWith(
            "pointerInput(key1 = key1, pointerInputEventHandler = block)",
            "androidx.compose.ui.input.pointer.Modifier.pointerInput"
        )
)
fun Modifier.pointerInput(key1: Any?, block: suspend PointerInputScope.() -> Unit): Modifier

fun Modifier.pointerInput(key1: Any?, block: PointerInputEventHandler): Modifier
@Deprecated(
    "This function is deprecated. Use the PointerInputEventHandler block variation instead",
    level = DeprecationLevel.HIDDEN,
    replaceWith =
        ReplaceWith(
            "pointerInput(key1 = key1, key2 = key2, pointerInputEventHandler = block)",
            "androidx.compose.ui.input.pointer.Modifier.pointerInput"
        )
)
fun Modifier.pointerInput(
    key1: Any?,
    key2: Any?,
    block: suspend PointerInputScope.() -> Unit
): Modifier

fun Modifier.pointerInput(key1: Any?, key2: Any?, block: PointerInputEventHandler): Modifier
@Deprecated(
    "This function is deprecated. Use the PointerInputEventHandler block variation instead",
    level = DeprecationLevel.HIDDEN,
    replaceWith =
        ReplaceWith(
            "pointerInput(keys = keys, pointerInputEventHandler = block)",
            "androidx.compose.ui.input.pointer.Modifier.pointerInput"
        )
)
fun Modifier.pointerInput(
    vararg keys: Any?,
    block: suspend PointerInputScope.() -> Unit
): Modifier

fun Modifier.pointerInput(vararg keys: Any?, block: PointerInputEventHandler): Modifier

Code Examples

keyedPointerInputModifier

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

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)
                }
            }
        )
    }
}
by @alexstyl