I want to make dashed border like this to my butto...
# compose
e
I want to make dashed border like this to my button, for this I am drawing rectangle with dashed stroke, but there is obscure exception: “java.lang.NullPointerException: null cannot be cast to non-null type androidx.compose.ui.graphics.AndroidPathEffect”
Copy code
Canvas(modifier = Modifier.fillMaxSize(), 
onDraw = {
                            drawRect(
                                size = Size(packFloats(200f, 50f)),
                                color = Color.Blue,
                                style = Stroke(width = 1f, pathEffect = PathEffect.dashPathEffect(floatArrayOf(10f, 10f), 0f))
                            )
}
)
Have you got any idea, why this exception is appearing?
k
@Nader Jawad
j
Probably Nader is going to need more than just the message. Can you post the whole trace in this thread or in a gist?
b
j
I had a similar issue last week https://issuetracker.google.com/issues/179062252 which has already been resolved (maybe for alpha12 it will be released). The problem there though was that when making a draw call with no pathEffect after one which did have one the cast failed
1
😆 1
n
Yes this looks similar to the issue that @Jeisson Sáchica has linked to. If you need a workaround @Elnur Jeksenov you can try the following code that allows you to draw with the canvas directly instead of leveraging the
DrawScope
API.
Copy code
val paint = remember {
        Paint().apply {
            color = Color.Blue
            style = PaintingStyle.Stroke
            strokeWidth = 1f
            pathEffect = PathEffect.dashPathEffect(floatArrayOf(10f, 10f), 0f)
        }
    }
    Canvas(modifier = Modifier.fillMaxSize()) {
        drawIntoCanvas { canvas ->
            canvas.drawRect(0f, 0f, 200f, 50f, paint)
        }
    }
🙌 1
Also FWIW in the same code provided you wouldn't need to call
Size(packFloats(200f, 50f)
but rather just call
Size(200f, 50f)
directly. This is a side effect of how inline classes in Kotlin work. We need to expose the packed long value public constructor however, we expose similarly named function constructors to do this work on your behalf
347 Views