Menno Wellner
02/24/2025, 9:08 AM@Composable
fun ErrorDialog(title: String, message: String, onDismiss: () -> Unit)
Compose should be able to recompose the dialog when the value message
changes. I'm using an dialog manager to update the values of title
and message
, and wether the dialog is visible with this function:
@Composable
fun DialogHost() {
val dialogState by remember { mutableStateOf(dialogState) }
if (dialogState.value) {
ErrorDialog(title, message) { dialogState.value = false }
}
}
However, in my case it does update the value of message
as I can see different text displayed when another error occurs, but the size/state of the dialog is not updated. This causes the dialog to be too small for all the content it needs to show and thus disappear on the bottom side of the dialog.
Anyone an idea why this happens, and how to fix or has a different approach to a reusable dialog with dynamic content?Csaba Szugyiczki
02/24/2025, 9:16 AMusePlatformDefaultWidth
dialog property, which used to lead to this behaviour. Not sure if there is something similar, that you could changeMenno Wellner
02/24/2025, 9:19 AMMenno Wellner
02/24/2025, 9:40 AMWindow(
onCloseRequest = onDismiss
) {
Dialog(
onDismissRequest = onDismiss,
properties = DialogProperties(usePlatformDefaultWidth = false)
) { /* Dialog content */ }
}
This does actually show the dialog correctly but the dialog is pretty big. So I then add this to the Window element:
state = rememberWindowState(
size = DpSize(350.dp, Dp.Unspecified))
According to the documentation setting Dp.Unspecified
would set the height of the window to the correct size depending on the content it needs to show. However, doing so I'm just shown a floating window header 😂 🤦♂️
Then I thought lets follow the other route using the DialogWindow
block, but this results as described my initial post. Using the DialogWindow
like:
@Composable
fun ErrorDialog(title: String, message: String, onDismess: () -> Unit) {
DialogWindow(
onCloseRequest = onDismiss,
title = title,
alwaysOnTop = true,
resizable = false,
state = rememberDialogState(
position = WindowPosition(Alignment.Center),
height = Dp.Unspecified
)
) { /* Dialog content */ }
}
Menno Wellner
02/24/2025, 9:44 AMColumn(
modifier = Modifier
.fillMaxWidth()
.wrapContentHeight(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(
text = message,
textAlign = TextAlign.Center,
modifier = Modifier.padding(10.dp)
)
Button(
onClick = onDismiss,
modifier = Modifier
.padding(vertical = 10.dp)
.width(125.dp),
colors = ButtonDefaults.buttonColors(
backgroundColor = Color(0, 149, 225),
contentColor = Color.White
)
) {
Text(
text = "Ok",
fontSize = 12.sp,
fontWeight = FontWeight.Light,
textAlign = TextAlign.Center,
)
}
}