compose-multiplatform icon indicating copy to clipboard operation
compose-multiplatform copied to clipboard

How to display a Dialog in Compose Multiplaform

Open ferrandp opened this issue 3 years ago • 3 comments

Hi, I am trying to display a Dialog in a Compose Multiplaform project. I have found multiple code examples looking like this :

    Dialog(onDismissRequest = { /*TODO*/ }) {
        Card {
            Text("Some random text", Modifier.padding(24.dp))
        }
    }

My IDE shows errors with this code and can't find imports, but when I copy/paste the imports and run the Android app, everything works as expected. However when I try to run the Desktop app, I get some compilation issues :

e: Cannot find a parameter with this name: onDismissRequest
e: No value passed for parameter 'create'
e: No value passed for parameter 'dispose'

I am using Compose version 1.1.1 and in the common dependencies, I have

api(compose.runtime)
api(compose.foundation)
api(compose.material)
api(compose.ui)

What am I missing?

ferrandp avatar Jul 14 '22 09:07 ferrandp

This is because you don't pass required parameters. use this code:

val dialogState = remember { mutableStateOf(false) }
Button(onClick = { dialogState.value = true }) {
      Text("show dialog")
}
Dialog(visible = dialogState.value, onCloseRequest = { dialogState.value = false }) {
      Text("dialog content")
}

amparhizgar avatar Jul 14 '22 12:07 amparhizgar

Thanks for your answer. Unfortunately, this code does not work neither on Android nor on Desktop. Nothing shows on desktop, whereas I get a compilation issue when trying to build the Android application:

e: Cannot find a parameter with this name: visible

The IDE does not even put the Dialog Composable in autocompletion when I start typing, so it is not a problem of parameters.

ferrandp avatar Jul 14 '22 22:07 ferrandp

Please configure your build.gradle.kts according to the projects in examples.

If you are creating app only for desktop you can check this.

Use compose.desktop.currentOs:

plugins {
    ...
    id("org.jetbrains.compose")
}

...

dependencies {
    implementation(compose.desktop.currentOs)
    ...
}

You may also have to check files like gradle.properties and settings.gradle.kts.

sdercolin avatar Jul 20 '22 08:07 sdercolin

I think it is strange, that the Dialog API differs discretely on Android and desktop

        // Desktop
        Dialog(
            onCloseRequest = {}
        ) {
            Content()
        }
        // Android
        Dialog(
            onDismissRequest = {},
            content = {
                Content()
            }
        )

Burtan avatar Oct 30 '22 17:10 Burtan

// Android Dialog( onDismissRequest = {}, content = { Content() } )

We still need to wrap it with another layer ourselves?

wwalkingg avatar Mar 12 '23 13:03 wwalkingg

It has been almost a year since this issue has been opened. What is the recommended solution for the discrepancy in the Compose Multiplatform vs JetPack Compose APIs in onCloseRequest and onDismissRequest?

h0tk3y avatar May 14 '23 20:05 h0tk3y

I was able to create a dialog with a edit text to edit a information. however I dont know how to return this editted string to the window that called the popup?

cainan avatar Jun 15 '23 23:06 cainan

Regarding initial topic: we're investigating how to "commonize" API like that. For now it differs between Android and Multiplatform, yes.

@cainan I guess like in regular case with TextField - using remember/state outside or passing your own callback

MatkovIvan avatar Jun 16 '23 08:06 MatkovIvan