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")
                },
                
            )
        }
    )
1

There are 1 best solutions below

0
Atul Sharma On

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.