Ask the assistant
0/400
Your question is processed by an AI service and kept to improve answers. Do not enter personal data.

    Dialogs and pop-ups

    Surfaces that sit above the main content: dialogs, a bottom sheet, a snackbar, a banner and a pop-up panel attached to an element. To show a dialog on tap , declare Dialog id:"…" and open it with navigator.showDialog("…") . The other elements of the family are drawn where they stand in the tree.

    Element When to use it
    `Dialog` A dialog on tap ( id: + navigator.showDialog ) or just a dialog surface in the tree
    `AlertDialog` A ready-made "title — text — buttons" layout
    `SimpleDialog` A title and a list of options
    `BottomSheet` A bottom sheet surface
    `SnackBar` A short message with one action button
    `MaterialBanner` A persistent banner with an icon and buttons
    `Popover` Content above the layout, attached to an element; the markup controls its visibility

    Dialog

    A Material dialog surface. With id: it is a declaration : nothing is drawn in place of the element, and the content waits for navigator.showDialog("<id>") , then opens above the app. navigator.close() closes it. Without id: it is just a dialog surface drawn where the element stands.

    Type: single-child

    Column spacing:16
      Dialog id:"confirm"
        Padding
          #padding all 24
          Column spacing:16
            Text "Delete the note?"
            FilledButton onPressed:(){navigator.close()}
              Text "Close"
      FilledButton onPressed:(){navigator.showDialog("confirm")}
        Text "Delete"
    Parameter Type Default Description
    id string — Name of the declared dialog. With it the element takes no space and is shown only through navigator.showDialog
    elevation double from the theme Shadow height of the surface
    backgroundColor color from the theme Surface color
    shadowColor color from the theme Shadow color
    surfaceTintColor color from the theme Surface tint over the background
    #shape structure from the theme Surface shape ( rounded , stadium , beveled … with its own #borderRadius )
    clipBehavior string none Clipping to the shape: none , hardEdge , antiAlias , antiAliasWithSaveLayer
    • An open dialog answers with an action ( action("…") or a variable change), not with a return value: there is nothing that could answer navigator.showDialog .
    • An unknown id in navigator.showDialog does nothing. A dialog whose screen has left the tree cannot be opened.
    • A declared dialog (with id: ) opens with the default surface style: elevation , backgroundColor , #shape and the other styling parameters are read only on a surface without id: .

    Delete confirmation

    @deleted:false
    
    Column spacing:16
      Dialog id:"confirm-delete"
        DecoratedBox
          #decoration color:#ffffff
            #borderRadius circular 20
          Padding
            #padding all 24
            Column spacing:16
              Text "Delete the note?"
              Row spacing:8
                TextButton onPressed:(){navigator.close()}
                  Text "Cancel"
                FilledButton onPressed:(){deleted = true; navigator.close()}
                  Text "Delete"
      FilledButton onPressed:(){navigator.showDialog("confirm-delete")}
        Text "Delete the note"
      Text {deleted ? "Deleted" : "The note is still here"}

    AlertDialog

    A ready-made Material dialog: an icon, a title, text and a row of buttons. The parts are laid out by the children's id: . On its own it is drawn where it stands (see the differences — it is different on the site).

    Type: multi-child

    @answer:""
    
    AlertDialog
      Text id:title "Delete the note?"
      Text id:content "This cannot be undone."
      TextButton id:action onPressed:(){answer = "cancel"}
        Text "Cancel"
      FilledButton id:action onPressed:(){answer = "delete"}
        Text "Delete"

    Child slots:

    id What it is
    title Title
    content Text or body of the dialog
    icon Icon above the title
    action A button in the bottom row; there can be several such children
    Parameter Type Default Description
    elevation double from the theme Shadow height
    backgroundColor color from the theme Surface color
    shadowColor color from the theme Shadow color
    surfaceTintColor color from the theme Surface tint
    #shape structure from the theme Surface shape
    #titlePadding structure from the theme Padding around the title ( #titlePadding all 24 )
    #contentPadding structure from the theme Padding around the text
    #actionsPadding structure from the theme Padding around the row of buttons
    actionsAlignment string — Button alignment: start , end , center , spaceBetween , spaceAround , spaceEvenly
    clipBehavior string none Clipping to the shape
    @answer:""
    
    AlertDialog backgroundColor:#ffffff actionsAlignment:end
      #shape rounded
        #borderRadius circular 16
      #contentPadding all 20
      Icon id:icon warning
      Text id:title "Warning"
      Text id:content "Your changes are not saved."
      TextButton id:action onPressed:(){answer = "ok"}
        Text "Got it"

    SimpleDialog

    A choice dialog: a title ( id:title ) and a list of options — all children without an id .

    Type: multi-child

    @city:""
    
    SimpleDialog
      Text id:title "Choose a city"
      TextButton onPressed:(){city = "London"}
        Text "London"
      TextButton onPressed:(){city = "Paris"}
        Text "Paris"
    Parameter Type Default Description
    elevation double from the theme Shadow height
    backgroundColor color from the theme Surface color
    shadowColor color from the theme Shadow color
    surfaceTintColor color from the theme Surface tint
    #shape structure from the theme Surface shape
    #titlePadding structure fromLTRB 24 24 24 0 Padding around the title
    #contentPadding structure fromLTRB 0 12 0 16 Padding around the list of options
    clipBehavior string none Clipping to the shape

    BottomSheet

    A Material bottom sheet surface. The element draws the sheet itself where it stands; it cannot slide up from the bottom on tap.

    Type: single-child

    BottomSheet enableDrag:false
      Padding
        #padding all 24
        Text "Bottom sheet"
    Parameter Type Default Description
    backgroundColor color from the theme Sheet color
    elevation double from the theme Shadow height
    shadowColor color from the theme Shadow color
    #shape structure from the theme Sheet shape (usually rounded top corners)
    clipBehavior string — Clipping to the shape
    enableDrag bool true Dragging the sheet with a gesture
    #constraints structure — Size constraints ( #constraints maxWidth:640 )

    Write enableDrag:false : with enableDrag:true (the default) dragging the sheet in the app fails with an error.

    BottomSheet backgroundColor:#f8fafc elevation:8 enableDrag:false
      #shape rounded
        #borderRadius only topLeft:16 topRight:16
      Padding
        #padding all 24
        Column spacing:8
          Text "Filters"
          Text "Price, rating, availability"

    SnackBar

    A short message at the bottom of the screen with one optional button. The text is a child with id:content ; the button is set with actionLabel and onAction , not with a child. The element is drawn in place, in the tree.

    Type: multi-child

    @undone:false
    
    SnackBar actionLabel:"Undo" onAction:(){undone = true}
      Text id:content "Note deleted"
    Parameter Type Default Description
    actionLabel string — Label of the action button. Without it there is no button
    onAction callback — Called when the action button is tapped
    duration duration 4s How long the snackbar is shown. The element is drawn right in the tree, and it does not hide when the time runs out
    backgroundColor color from the theme Background color
    elevation double from the theme Shadow height
    #padding structure from the theme Inner padding
    #shape structure from the theme Shape
    behavior string fixed fixed — full width, floating — floating with margins
    width double — Width; only together with behavior:floating
    showCloseIcon bool false Built-in close icon
    closeIconColor color from the theme Color of the close icon
    dismissDirection string down Swipe-to-dismiss direction: down , up , horizontal , vertical , startToEnd , endToStart , none
    @undone:false
    
    SnackBar backgroundColor:#1e1b4b behavior:floating actionLabel:"Undo" onAction:(){undone = true}
      #shape rounded
        #borderRadius circular 8
      Text id:content "Saved"
        #style color:#ffffff

    MaterialBanner

    A persistent Material banner: an icon, text and a row of buttons. The parts go by the children's id: .

    Type: multi-child

    @later:false
    
    MaterialBanner
      Icon id:leading info color:#0284c7
      Text id:content "An update is available"
      TextButton id:action onPressed:(){later = true}
        Text "Later"

    Child slots: content — the text (required; without it the banner is empty), leading — an icon before the text, action — a button; there can be several such children.

    Parameter Type Default Description
    backgroundColor color from the theme Background color
    elevation double — Shadow height (no shadow by default)
    #padding structure from the theme Padding around the text
    #leadingPadding structure from the theme Padding around the icon
    dividerColor color from the theme Color of the line under the banner
    forceActionsBelow bool false Buttons always below the text instead of on the right
    shadowColor color from the theme Shadow color
    surfaceTintColor color from the theme Surface tint

    Popover

    Content that floats above the layout and is attached to an element inside it. Visibility is a parameter , open : the element never opens or closes itself, the markup decides (for example, a list of suggestions opens from what is typed in a field). The id:anchor child stays in the layout and holds its place; id:content is drawn in a layer on top and takes no space, so opening it does not shift the page.

    Type: multi-child

    @open:false
    
    Popover open:$open onDismiss:{open = false}
      FilledButton id:anchor onPressed:(){open = !open}
        Text "Menu"
      Container id:content width:200 color:#ffffff
        Column
          Text "Item 1"
          Text "Item 2"
    Parameter Type Default Description
    open bool (reactive) false Whether the content is shown. The element never changes it itself
    anchor string below Where the content is relative to the anchor: below or above
    align string start Alignment across the anchor: start , center , end
    matchAnchorWidth bool false Content exactly as wide as the anchor
    #offset structure zero Content offset: #offset x:0 y:4
    #constraints structure — Size constraints of the content ( #constraints maxHeight:240 )
    onDismiss callback — A tap outside the anchor and the content; the markup decides what it means

    If nothing is said about the width of the content, it is as wide as the content asks, but not narrower than the anchor. A named width ( width: on id:content , #constraints minWidth/maxWidth ) wins, and then nothing is measured. A lazy list ( ListView.builder , GridView.builder ) cannot report its width: give such content a width or matchAnchorWidth:true . Without id:anchor the element draws nothing.

    Differences between the app and the site

    • Dialog id: on the site sits hidden in the HTML and is shown through navigator.showDialog with a dark backdrop; navigator.close() , Escape and a tap on the backdrop close it. A declared dialog has no surface on the site — the content is drawn right on the backdrop, so give the content its own background and rounding (as in the example above). Without id: the site draws only the child: backgroundColor , elevation , #shape are not read.
    • AlertDialog and SimpleDialog on the site are always drawn above the whole window , centered ( position: fixed ), even when they stand in the middle of a page; in the app they stay in their place in the tree. The styling parameters ( backgroundColor , #shape , elevation , paddings, actionsAlignment ) are not read on the site: a white card with a 28 radius, buttons on the right. On the site id:icon is output after the text, not above the title.
    • BottomSheet on the site sticks to the bottom of its block; backgroundColor and #shape are read, the shadow is its own.
    • SnackBar on the site: backgroundColor and behavior:floating are read; #shape , showCloseIcon , width are not. The actionLabel button is drawn, but onAction is not called on the site .
    • MaterialBanner on the site reads backgroundColor , dividerColor , the slots and the buttons; the buttons work.
    • Popover works on the site: open toggles the content, a tap outside the element calls onDismiss . anchor , align , #offset , matchAnchorWidth and maxHeight from #constraints are read; the site does not measure the width by the content — it comes out by itself, by the CSS rules.

    Bad practices

    A dialog on the page instead of a declared one

    // BAD — the dialog is always on screen; on the site it also covers the whole window
    AlertDialog
      Text id:title "Delete?"
    // GOOD — the content is declared and shown on tap
    Column
      Dialog id:"confirm"
        Padding
          #padding all 24
          Text "Delete?"
      TextButton onPressed:(){navigator.showDialog("confirm")}
        Text "Delete"

    See also