ivano
02/17/2023, 2:42 PM@Composable invocations can only happen from the context of a @Composable function
refer to onClick()
, instead i get this error every time i try to extract some long code from a composable function for instance a Canvas
I cannot find IN GENERAL what this error means and so I am not able to solve my caseephemient
02/17/2023, 2:44 PMvide
02/17/2023, 2:45 PMephemient
02/17/2023, 2:45 PMivano
02/17/2023, 2:45 PMCanvas(modifier = modifier) {
val spacePerHour = (size.width - spacing) / infos.size
(0 until infos.size - 1 step 2).forEach { i ->
val info = infos[i]
val hour = info.date.hour
drawContext.canvas.nativeCanvas.apply {
drawText(
hour.toString(),
spacing + i * spacePerHour,
size.height - 5,
textPaint
)
}
}
}
CLOVIS
02/17/2023, 2:53 PMivano
02/17/2023, 2:54 PM@Composable invocations
error):
Canvas(modifier = modifier) {
val spacePerHour = (size.width - spacing) / infos.size
(0 until infos.size - 1 step 2).forEach { i ->
val info = infos[i]
val hour = info.date.hour
drawContext.canvas.nativeCanvas.apply {
BuildXAxis(this, hour, spacing, i, spacePerHour, this, textPaint)
}
}
}
@Composable
private fun BuildXAxis(
canvas: NativeCanvas,
hour: Int,
spacing: Float,
i: Int,
spacePerHour: Float,
drawScope: DrawScope,
textPaint: Paint
) {
canvas.drawText(
hour.toString(),
spacing + i * spacePerHour,
drawScope.size.height - 5,
textPaint
)
}
BuildXAxis
method to make it the long function more readableephemient
02/17/2023, 2:55 PM@Composable
from the function if you want to use it in that positionivano
02/17/2023, 2:57 PM