Which is the simplest and better way to display di...
# compose-desktop
p
Which is the simplest and better way to display dialogs when a button is pressed? I only can find samples that sets true a "showDialog" variable that is checked in "visible" property of Dialog, being Dialog on the logic of the screen but simply not visible until that variable has changed. I really don't like this approach. I whould prefeer simply calling a Composable function called "DisplayDialog" in the onclick event of the button. That function has the logic for showing the dialog, but doing that results in an exception: "@Composable invocations can only happen from the context of a @Composable function"
z
This API design is a feature, not a bug – one of the key features of declarative UI, in fact. Hoisting state out of things like dialogs and giving you full control means you don’t have to try to sync your state with the private state inside components you don’t control. There are a lot of talks and articles about the advantages of this. That said, if you still want to write imperative code for this, that’s fine – but you probably shouldn’t be trying to use a declarative UI toolkit to do it.
a
In Compose, yep! For the simplest cases it might seem like extra work (I have to keep around this extra variable to decide if I want to show the dialog?), but the payoff is you now have full control over when the dialog is shown as cases get more complex. Want to hide the dialog for some other reason, or have the conditions to show be based on some other factors? Just change the state!