Home > Software engineering >  How do I make a donut in Kotlin Compose?
How do I make a donut in Kotlin Compose?

Time:01-01

In my main activity I call the donut routine with the following parameters:

setContent {
    Row() {
        donut(_size = 100.dp, _padding = 10.dp)
    }
}

And here is the donut function:

@Preview
@Composable
fun donut(_size: Dp = 120.dp, _padding: Dp = 5.dp) {
    Box(modifier = Modifier
        .size(_size)
        .clip(CircleShape)
        .background(Color.Green)
        .padding(_padding)
        .clip(CircleShape)
        .background(Color.Red))
}

I know the donut function works but it won't display in preview. When trying to see a preview I get the following message: Unable to find @Preview 'com.pelicancolder.chapter1_03.MainActivity.donut'

However I don't understand why compose can't find the donut function. Thanks in advance.

CodePudding user response:

As I answered in your other question, The @Preview has to be applied to a function without parameters. Remove the @Preview from your donut function and add another function as below

@Preview
@Composable
fun PreviewDonut() {
    donut()
}

You can also make the new function private should you wish.

  • Related