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.
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
}