Artier
Artier

Reputation: 1673

getting error @Composable invocations can only happen from the context of a @Composable function | composable inside pointerInput

I want to call Text inside detectDragGestures it gives error

error: @Composable invocations can only happen from the context of a @Composable function

Canvas(
        modifier = Modifier
        .fillMaxSize()
        .pointerInput(Unit) {
            detectDragGestures(
                onDragStart = { touch ->
                    Text("0")
                },
                
            )
        }
    )

Upvotes: 0

Views: 71

Answers (1)

Atul Sharma
Atul Sharma

Reputation: 1102

If you want to show the Text composable on drag then you manage a mutableState like in this sample.

val showText = remember { mutableStateOf(false) }

if(showText)
    Text("0")

Canvas(
    modifier = Modifier
        .fillMaxSize()
        .pointerInput(Unit) {
            detectDragGestures(
                onDragStart = { touch ->
                    showText.value = true
                },
                //...
            )
    }
)

Note: Don't forgot to set the variable to false after drag ends.

Also you can animate the Text using AnimatedVisibility.

Upvotes: 0

Related Questions