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

    Navigation and tabs

    Elements for switching between sections: the bottom bars BottomNavigationBar and NavigationBar , the side rail NavigationRail , and tabs TabBar + TabBarView under a shared DefaultTabController .

    Navigation bars do not switch the screen themselves : they draw the highlight by index and report a tap. What to show is decided by the markup: state with an index, a Switcher or navigator.show(…) in the handler.

    BottomNavigationBar

    A Material 2 bottom navigation bar: icons with labels in a row. Items are children with id:item ; the child itself becomes the item's icon, its label: parameter — the label. Usually placed in the id:bottomNavigationBar slot of a Scaffold .

    Type: multi-child (items by id:item )

    @tab:0
    BottomNavigationBar currentIndex:$tab onTap:(index){tab = index}
      Icon id:item label:"Home" home
      Icon id:item label:"Search" search
      Icon id:item label:"Profile" person

    Children without id:item are not drawn; without a single item there is no bar.

    Parameter Type Default Description
    currentIndex int 0 Index of the selected item (from zero)
    onTap callback — Receives the index of the tapped item: (index){…}
    type string — fixed — all items the same width, labels visible; shifting — the selected item is wider and only it has a label
    backgroundColor color from the theme Bar background
    selectedItemColor color the theme's primary color Icon and label color of the selected item
    unselectedItemColor color from the theme Color of the other items
    iconSize double 24.0 Icon size
    elevation double — Elevation (shadow)
    selectedFontSize double 14.0 Font size of the selected item's label
    unselectedFontSize double 12.0 Font size of the other labels
    showSelectedLabels bool — Show the selected item's label
    showUnselectedLabels bool — Show the other items' labels

    Parameter of an item (on a child with id:item ):

    Parameter Type Description
    label string Item label

    A Material 3 bottom navigation bar: icons with labels and a "pill" under the selected icon. Items are children with id:destination and label: .

    Type: multi-child (items by id:destination )

    @tab:0
    NavigationBar selectedIndex:$tab onDestinationSelected:(index){tab = index}
      Icon id:destination label:"Home" home
      Icon id:destination label:"Search" search
      Icon id:destination label:"Profile" person

    Without onDestinationSelected the bar looks alive, but nothing happens on tap: selectedIndex only draws the highlight.

    Parameter Type Default Description
    selectedIndex int 0 Index of the selected item
    onDestinationSelected callback — Receives the index of the tapped item: (index){…}
    labelBehavior string alwaysShow Labels: alwaysShow , alwaysHide , onlyShowSelected
    backgroundColor color from the theme Bar background
    indicatorColor color from the theme Color of the "pill" under the selected icon
    shadowColor color — Shadow color
    surfaceTintColor color — Surface tint when elevated
    elevation double — Elevation
    height double — Bar height
    animationDuration duration — Duration of the item change animation: 300ms

    Example: a bar that switches the route

    Each screen places its own bar with the index of its own item; a tap opens the route of another screen. This is the bar on the profile screen:

    NavigationBar selectedIndex:1 onDestinationSelected:(index){navigator.show(index == 0 ? "/home" : "/profile")}
      Icon id:destination label:"Home" home
      Icon id:destination label:"Profile" person

    Example: what both bottom bars look like

    Column spacing:24
      BottomNavigationBar currentIndex:0 selectedItemColor:#4f46e5 unselectedItemColor:#94a3b8
        Icon id:item label:"Home" home
        Icon id:item label:"Search" search
        Icon id:item label:"Profile" person
      NavigationBar selectedIndex:1 indicatorColor:#c7d2fe
        Icon id:destination label:"Home" home
        Icon id:destination label:"Search" search
        Icon id:destination label:"Profile" person

    A vertical navigation rail on the side — for wide screens (tablet, desktop). Items are children with id:destination and label: ; above the items you can put id:leading (a logo, a button), below them — id:trailing .

    Type: multi-child (items by id:destination + leading , trailing slots)

    @tab:0
    Row
      NavigationRail selectedIndex:$tab labelType:all onDestinationSelected:(index){tab = index}
        Icon id:destination label:"Home" home
        Icon id:destination label:"Search" search
        Icon id:destination label:"Settings" settings
      Expanded
        Center
          Text $"Section ${tab}"
    Parameter Type Default Description
    selectedIndex int 0 Index of the selected item
    onDestinationSelected callback — Receives the index of the tapped item: (index){…}
    extended bool false Extended rail: labels to the right of the icons
    labelType string none Labels in the collapsed rail: none , selected , all
    backgroundColor color from the theme Rail background
    indicatorColor color from the theme Highlight color of the selected item
    selectedIconThemeColor color — Icon color of the selected item
    unselectedIconThemeColor color — Icon color of the other items
    elevation double — Elevation
    minWidth double — Minimum width of the collapsed rail
    minExtendedWidth double — Minimum width of the extended rail
    groupAlignment double — Vertical position of the item group: from -1.0 (top) to 1.0 (bottom)

    DefaultTabController

    Shared tab state for the TabBar and TabBarView inside it: which tab is selected. Without it TabBar and TabBarView do not work in the app.

    Type: wrapper

    DefaultTabController length:2
      Column
        TabBar
          Text "Inbox"
          Text "Sent"
        Expanded
          TabBarView
            Center
              Text "Incoming mail"
            Center
              Text "Sent mail"
    Parameter Type Default Description
    length int 0 Number of tabs. Must match the number of children of TabBar and TabBarView
    initialIndex int 0 The tab selected at the start

    TabBar

    A row of tabs with the selected one underlined. Every child becomes a tab (usually a Text or an Icon ). A tap selects the tab, and the TabBarView under the same controller pages to its page.

    Type: multi-child

    DefaultTabController length:3
      TabBar labelColor:#4f46e5 indicatorColor:#4f46e5 unselectedLabelColor:#94a3b8
        Text "Day"
        Text "Week"
        Text "Month"
    Parameter Type Default Description
    isScrollable bool false Tabs as wide as their content, and they scroll (for many tabs)
    tabAlignment string fill Tab layout: fill , center (for a non-scrolling row), start , startOffset (for a scrolling one)
    indicatorColor color from the theme Underline color of the selected tab
    indicatorWeight double 2.0 Underline thickness
    labelColor color from the theme Color of the selected tab
    unselectedLabelColor color from the theme Color of the other tabs
    labelStyle #labelStyle — Text style of the selected tab
    unselectedLabelStyle #unselectedLabelStyle — Text style of the other tabs
    labelPadding #labelPadding — Padding around each tab
    padding #padding — Padding around the whole row
    dividerColor color from the theme Color of the line under the tab row
    dividerHeight double — Thickness of the line under the row

    TabBarView

    The tab pages: child number N is shown when tab N is selected. Pages can also be swiped. It takes all the space it is given — inside a Column put it in an Expanded or give it a height.

    Type: multi-child

    DefaultTabController length:2
      SizedBox height:240
        TabBarView
          Center
            Text "First page"
          Center
            Text "Second page"
    Parameter Type Default Description
    clipBehavior string hardEdge Clipping of pages while paging: none , hardEdge , antiAlias , antiAliasWithSaveLayer

    Differences between the app and the site

    • BottomNavigationBar and NavigationBar on the site are a row of <nav> buttons; a tap calls onTap / onDestinationSelected with the item index, so state and a Switcher do switch. The selected-item highlight does not move on the site : it is computed once from the initial currentIndex / selectedIndex and stays on the old item after a tap. Of the colors, backgroundColor , selectedItemColor , unselectedItemColor are read; NavigationBar has no "pill" ( indicatorColor is not read), label font sizes are fixed (14 and 12 px), and labelBehavior , type , iconSize , height are not read.
    • NavigationRail is not supported on the site (an unsupported widget warning): only the item icons come out, without labels, highlight or taps.
    • TabBar and TabBarView do not switch on the site: TabBar draws a row of tabs with the first one selected ( initialIndex is not read), TabBarView — only the first page. The tab colors are fixed (purple #6200ee ); labelColor , indicatorColor and the other parameters are not read. For switchable sections on the site use state with an index, a row of buttons and a Switcher .
    • DefaultTabController on the site draws only its child.

    Bad practices

    length does not match the number of tabs

    // BAD — length:2, but there are three tabs: a controller error in the app
    DefaultTabController length:2
      TabBar
        Text "One"
        Text "Two"
        Text "Three"
    // GOOD
    DefaultTabController length:3
      TabBar
        Text "One"
        Text "Two"
        Text "Three"

    See also

    • Screen scaffold — the bottomNavigationBar slot of Scaffold
    • Switcher — showing a section by the selected index
    • Icon — item icons
    • Expanded — TabBarView inside a Column