In the below code, toPx() works in Canvas but not in Surface.
Why?
Canvas(modifier = Modifier.size(16.dp)) {
    val textPaint = Paint().asFrameworkPaint().apply {
        textSize = 32.dp.toPx()
    }
}
Surface(modifier = Modifier.size(16.dp)) {
    val textPaint = Paint().asFrameworkPaint().apply {
        textSize = 32.dp.toPx() // Error `toPx()`
    }
}    
                The toPx() function is defined inside  a Density interface and you cannot use it unless you provide it.  The Canvas works with a DrawScope which provides it.
To use it you can provide the Density using theLocalDensity provider.
Something like:
val dpToPx = with(LocalDensity.current) { 32.dp.toPx() }
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With