Jetpack Compose Show Confirm Dialog

December 16, 2021
@Composable
fun test() {
    var showDeleteConfirm by remember { mutableStateOf(false) }

    Button(onClick = { showDeleteConfirm = true }) {
        Text("Delete")
    }

    if (showDeleteConfirm) {
        ConfirmDialog(
            content = "Confirm Delete?",
            onDismiss = { showDeleteConfirm = false },
            onConfirm = {
                showDeleteConfirm = false
                onDelete() // perform actual operation
            }
        )
    }
}
@Composable
fun ConfirmDialog(title: String? = null, content: String, onDismiss: () -> Unit, onConfirm: () -> Unit) {
    AlertDialog(
        modifier = Modifier.fillMaxWidth(),
        onDismissRequest = {
            onDismiss()
        },
        /*
        // https://stackoverflow.com/questions/69452854/increase-space-between-title-and-text-of-alertdialog-in-compose
        title = {
           if (!title.isNullOrEmpty()) {
               Text(title)
           }
        },
         */
        text = {
            Column (
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(8.dp),
                // horizontalAlignment = Alignment.CenterHorizontally
            ) {
                // CircularProgressIndicator()
                // Text("Hello")
                if (!title.isNullOrEmpty()) {
                    Text(title,
                        modifier = Modifier.padding(vertical = 8.dp),
                        style = MaterialTheme.typography.subtitle1)
                }

                Text(content)
                // Timber.d("message=${progressState.message}")
            }
        },
        // buttons = { }
        dismissButton = {
            Button(onClick = { onDismiss() }) {
                Text("No")
            }
        },
        confirmButton = {
            Button(onClick = { onConfirm() }) {
                Text("Yes")
            }
        }
    )
}
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.