Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unresolved reference on pointerInput and detectTransformGestures

Here’s the error log:
Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
public fun Modifier.pointerInput(key1: Any?, key2: Any?, block: suspend PointerInputScope.() -> Unit): Modifier defined in androidx.compose.ui.input.pointer
public fun Modifier.pointerInput(key1: Any?, block: suspend PointerInputScope.() -> Unit): Modifier defined in androidx.compose.ui.input.pointer
public fun Modifier.pointerInput(vararg keys: Any?, block: suspend PointerInputScope.() -> Unit): Modifier defined in androidx.compose.ui.input.pointer
public fun Modifier.pointerInput(block: suspend PointerInputScope.() -> Unit): Modifier defined in androidx.compose.ui.input.pointer. Line 80

Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
public suspend fun PointerInputScope.detectTransformGestures(panZoomLock: Boolean = …, onGesture: (centroid: Offset, pan: Offset, zoom: Float, rotation: Float) -> Unit): Unit defined in androidx.compose.foundation.gestures. Line 81

I’m trying to make a simple mobile app that let you draw on your phone. Sort of Paint app on PC. I’m using jetpack compose for it. Using kotlin.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here is the code for the DrawingArea:

// Drawing
            val density = LocalDensity.current.density

            Canvas(
                modifier = Modifier
                    .fillMaxSize()
                    .background(Color.White)
            ) {
                pointerInput(Unit) {
                    detectTransformGestures { _, pan, _, _ ->
                        if (pan != Offset(0f, 0f)) {
                            val scaledPan = pan * density
                            points = points + Offset(scaledPan.x, scaledPan.y)
                        }
                    }
                }

                val paint = Paint().apply {
                    color = Color.Black
                    strokeWidth = 5f
                    style = androidx.compose.ui.graphics.PaintingStyle.Stroke
                }

i’ve also imported

import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.foundation.gestures.detectTransformGestures

I’ve also modified the needed dependencies on build.gradle.kts for the compose

I’ve asked my colleague and chatGPT for the solution, but still on dead end. Would really appreciate the help 😀 (i’m new to android studio, please explain it carefully 🙂

>Solution :

You need to call pointerInput as a Modifier extension function, instead of inside Canvas:

            Canvas(
                modifier = Modifier
                    .fillMaxSize()
                    .background(Color.White)
                    .pointerInput(Unit) {
                         detectTransformGestures { _, pan, _, _ ->
                             if (pan != Offset(0f, 0f)) {
                                 val scaledPan = pan * density
                                 points = points + Offset(scaledPan.x, scaledPan.y)
                             }
                         }
                     }

            ) {
                // ... content
            }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading