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

    Dropdowns, menus and search

    Elements that open a list on tap: choosing a value from a list ( DropdownButton , DropdownMenu ), a menu of actions behind a button ( PopupMenuButton ) and a search bar ( SearchBar ). The list options are children with a special id ( id:item , id:entry ) and their own value .

    What to use: to choose the value of a form field — DropdownMenu (Material 3, a field with a label) or DropdownButton (compact, underlined); a set of commands like "Edit / Delete" behind a "⋮" icon — PopupMenuButton ; several options visible at once — SegmentedButton or Radio .

    On the site these elements do not open. Lists and the search bar are drawn as a static picture, menus — only as their button. Details are in "Differences between the app and the site".

    The classic Material dropdown: shows the chosen option with an arrow and opens the list on tap. Options are children with id:item and a value ; the hint shown while nothing is chosen is a child with id:hint .

    Type: multi-child

    @color:"green"
    DropdownButton value:$color onChanged:(value){color = value}
      Text id:item value:"red" "Red"
      Text id:item value:"green" "Green"
      Text id:item value:"blue" "Blue"
    Parameter Type Default Description
    value any — The chosen value. Must match the value of exactly one option or be empty
    onChanged callback — Called on selection; the value argument is the option's value: (value){…} . Without it the list is disabled
    isExpanded bool false Stretch the button to the full width of the parent
    elevation int 8 Shadow elevation of the open list
    iconSize double 24 Arrow size
    iconEnabledColor color — Arrow color of an enabled list
    iconDisabledColor color — Arrow color of a disabled list
    dropdownColor color — Background of the open list
    #style TextStyle — Text style of the chosen value
    #borderRadius BorderRadius — Corner radius of the open list

    Children:

    id Parameters Description
    item value A list option; the child itself is drawn
    hint — What to show while value is empty
    @city:""
    SizedBox width:280
      DropdownButton value:$city isExpanded:true dropdownColor:#f8fafc onChanged:(value){city = value}
        #borderRadius circular 12
        Text id:hint "Choose a city"
        Text id:item value:"msk" "Moscow"
        Text id:item value:"spb" "Saint Petersburg"

    A Material 3 dropdown: a field with a border and a floating label , and a menu of options on tap. Options are children with id:entry and a value ; the option text is the child's first positional argument or its label parameter. The child itself is not drawn: the menu shows only its text.

    Type: multi-child

    @color:"green"
    DropdownMenu label:"Color" width:240 initialSelection:"green" onSelected:(value){color = value}
      Text id:entry value:"red" "Red"
      Text id:entry value:"green" "Green"
      Text id:entry value:"blue" "Blue"
    Parameter Type Default Description
    onSelected callback — Called on selection; the value argument is the option's value: (value){…}
    label string — Field label
    hintText string — Hint in an empty field
    initialSelection any — Initially selected value
    width double — Field width
    menuHeight double — Maximum height of the open menu
    enabled bool true Whether the menu can be opened
    #textStyle TextStyle — Text style of the chosen value

    Parameters of an id:entry child:

    Parameter Type Description
    value any The option's value
    label string The option text, if the child has no positional argument

    PopupMenuButton

    A button that opens a popup menu. Items are children with id:item and a value ; choosing an item calls onSelected with its value . The default button is a "⋮" icon; your own icon is an id:icon child, your own whole button is an id:child child.

    Type: multi-child

    @last:""
    PopupMenuButton tooltip:"Actions" onSelected:(value){last = value}
      Icon id:icon moreVert
      Text id:item value:"edit" "Edit"
      Text id:item value:"delete" "Delete"
    Parameter Type Default Description
    onSelected callback — Called when an item is chosen; the value argument is its value: (value){…}
    enabled bool true Whether the menu can be opened
    tooltip string the system "Show menu" The button's tooltip. An empty string tooltip:"" — no tooltip (with id:child )
    iconSize double — Size of the button icon
    iconColor color — Color of the button icon
    #padding EdgeInsets 8 on all sides Padding around the button icon
    #borderRadius BorderRadius — Corner radius of the hover and press highlight around id:child
    color color — Menu background
    elevation double — Menu shadow elevation
    #shape OutlinedBorder — Menu shape
    position string under Where the menu opens: under — below the button, over — on top of it
    #offset Offset zero Menu offset: #offset x:0 y:8
    itemCount int 0 How many items the @Macros id:itemBuilder template produces
    values list — The value of each produced item, in order; without it an item returns its index

    Children:

    id Parameters Description
    item value , visible A menu item; the child itself is drawn. visible:false removes the item from the menu entirely
    icon — Your own button icon. The highlight is a circle, the default tooltip is the system one
    child — Your own whole button. The highlight follows #borderRadius ; with it id:icon is not read
    itemBuilder @Macros … index:0 A template for items from data, repeated itemCount times

    Items are read when the menu opens , so visible: on an item follows state the button itself knows nothing about: Visibility id:item value:"signout" visible:$signedIn with text inside. A hidden item is not drawn as an empty row but left out: an empty row would keep its height and its tap. (The analyzer does not yet know that the menu reads visible: here, and suggests replacing Visibility with if: .)

    Menu offset and position:

    @last:""
    PopupMenuButton position:over onSelected:(value){last = value}
      #offset x:0 y:8
      Text id:item value:"profile" "Profile"
      Text id:item value:"signout" "Sign out"

    Items from data go in markup order, mixed with fixed ones — a header on top, rows, actions at the bottom:

    @titles:["Draft", "Version 2"]
    @ids:["d1", "v2"]
    @open:""
    PopupMenuButton tooltip:"" itemCount:{length(titles)} values:$ids onSelected:(value){open = value}
      #borderRadius circular 8
      Container id:child
        Text "Versions"
      @Macros id:itemBuilder index:0
        Text {titles[index]}
      Text id:item value:"all" "All versions"

    A Material 3 search bar: a rounded field with a hint. Reports the text on every keystroke ( onChanged ) and on submit ( onSubmitted ).

    Type: leaf

    @query:""
    SearchBar hintText:"Search" onChanged:(value){query = value}
    Parameter Type Default Description
    hintText string — Hint in an empty field
    onChanged callback — Called on every text change; the value argument is the text: (value){…}
    onSubmitted callback — Called when the person submits the query (the Enter key); the value argument is the query text
    elevation double 6 Shadow elevation
    backgroundColor color — Background
    shadowColor color — Shadow color
    #padding EdgeInsets — Inner padding
    #shape OutlinedBorder — Shape
    #textStyle TextStyle — Style of the typed text
    @query:""
    @found:""
    SearchBar hintText:"Products" elevation:1 backgroundColor:#f1f5f9 onChanged:(value){query = value} onSubmitted:(value){found = value}
      #padding symmetric horizontal:16

    Differences between the app and the site

    On the site none of these elements opens or accepts input.

    • DropdownButton is drawn as an underlined field with a "▾" arrow and shows the option whose value equals a literally written value:"red" , otherwise — the first option . With value:$color the first option is always shown. A tap does nothing.
    • DropdownMenu is drawn as a bordered field of width width , with the label and the initialSelection option (also only when written literally). A tap does nothing.
    • PopupMenuButton on the site draws only its first child : id:child or id:icon if it comes first. Without them the first child is a menu item — and it is output as plain text. The menu does not open.
    • SearchBar is drawn as a rounded plate with the hintText , backgroundColor and a shadow by elevation ; there is no input field in it, and onChanged / onSubmitted are never called. For search on the site use TextField .

    See also