Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Navigation and App Structure  ›  Lesson

Dialogs and Bottom Sheets

Dialog 14 minIn Compose a dialog or bottom sheet is shown while a piece of state says so, and dismissing it means setting that state back.

Dialogs and Bottom Sheets

Deleting a plant in Sprout should ask "Are you sure?" first. And choosing a watering schedule would be neater in a panel that slides up from the bottom. Both are overlays, and in Compose both follow the same rule, which surprises people coming from older Android code: there is no show() call.

The rule

A dialog is on screen while its state is true; dismiss means set it false.

Hold a boolean in state and wrap the dialog in an if:

var showDelete by remember { mutableStateOf(false) }

IconButton(onClick = { showDelete = true }) {
    Icon(Icons.Filled.Delete, contentDescription = "Delete plant")
}

if (showDelete) {
    AlertDialog(
        onDismissRequest = { showDelete = false },
        title = { Text("Delete Monstera?") },
        text = { Text("Its watering history will be lost.") },
        confirmButton = {
            TextButton(onClick = { onDelete(); showDelete = false }) { Text("Delete") }
        },
        dismissButton = {
            TextButton(onClick = { showDelete = false }) { Text("Cancel") }
        }
    )
}

onDismissRequest is called when the user taps outside or presses Back. It is only a request: the dialog closes because you set the state to false. Leave it empty and the dialog cannot be dismissed that way.

A ModalBottomSheet works the same way: show it inside if (showSheet) and set the flag false in its onDismissRequest. Choose by purpose:

  • AlertDialog — a short decision that interrupts: confirm, discard, allow.
  • Bottom sheet — extra content or a list of options tied to the current screen, such as picking a watering schedule.

The shortcut

For any overlay, answer three blanks: which flag shows it, who sets it true, who sets it false.

Sprout's delete confirmationStep 1 of 4
  1. Which flag

    showDelete, remembered, starting false.

Drills

Answer fast — trust the rule.

quick drill

How do you show an AlertDialog in Compose?

quick drill

When is onDismissRequest called?

quick drill

A dialog's onDismissRequest = { } is empty. What happens when the user taps outside?

quick drill

Which overlay fits "choose one of five watering schedules" best?

Remember

  • A dialog is on screen while its state is true; dismiss means set it false.
  • Answer the three blanks: which flag, who sets true, who sets false.
  • Dialogs for short decisions; bottom sheets for options and extra content.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainConfirm before deleting

Deleting a plant in Sprout must ask for confirmation first. Describe the state and composables you would use, what happens when the user taps outside the dialog, and when you would use a bottom sheet instead.