kotlinintellij-ideaandroid-jetpack-composecompose-multiplatform

How to use Preview for Compose Desktop in IntelliJ IDEA Community Edition?


When I'm creating a Compose Multiplatform app in IntelliJ IDEA, how can I get a preview of code marked with @Composable and @Preview? Do I need the paid version for this?

I'm on Windows and I have the "Compose Multiplatform for Desktop IDE Support" plugin installed, I've restarted IntelliJ multiple times, but it won't show me an option for previewing my app. My Kotlin file looks like this:

package foo.bar

import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import org.jetbrains.compose.ui.tooling.preview.Preview

@Composable
@Preview
fun MainContent() {
    Text("Hello world!")
}

However it also doesn't work with the App.kt file from the template.

So how do I get a preview now?


Solution

  • Nvm figured it out, the problem was that I was importing org.jetbrains.compose.ui.tooling.preview.Preview, but instead I should've been importing androidx.compose.desktop.ui.tooling.preview.Preview. After changing the third import line in my example code, it worked.

    Also, I noticed that you can't open a preview when the composable functions takes any arguments, because then Compose doesn't know what to fill in for those arguments. Unfortunately, assigning them default values didn't seem to work. Looks like you can only preview functions with zero arguments.