Navigation
Navigation widgets move the user between the main screens of your app. They show destinations and report which one was tapped. Swapping the content is up to you.

Which one to use
| You want | Use |
|---|---|
| A title bar with a menu or back icon and actions | TopAppBar |
| 3 to 5 main destinations along the bottom, on a phone | NavigationBar |
| 3 to 7 destinations down the side, on a tablet or desktop | NavigationRail |
| Many destinations in a panel that slides in | NavigationDrawer |
| A bottom bar with actions and a FAB | BottomAppBar |
| Sections of one screen | tabs: container.NewAppTabs (see Layout) |
Navigation bar and rail
NavigationBar
and NavigationRail
take a list of items, each with a Label and an Icon, and a function that
receives the index of the tapped item. A tap also updates Selected, which
highlights the item. Set Selected yourself (then call Refresh) when the
destination changes some other way. The rail shows labels under its icons by
default; set ShowLabels to false for icons only.
var bar *widget.NavigationBar
var rail *widget.NavigationRail
show := func(i int) {
content.SetText(pages[i])
bar.Selected, rail.Selected = i, i
bar.Refresh()
rail.Refresh()
}
bar = widget.NewNavigationBar([]*widget.NavigationBarItem{
{Label: "Home", Icon: theme.HomeIcon()},
{Label: "Search", Icon: theme.SearchIcon()},
{Label: "Profile", Icon: theme.AccountIcon()},
}, show)
rail = widget.NewNavigationRail([]*widget.NavigationRailItem{
{Label: "Home", Icon: theme.HomeIcon()},
{Label: "Search", Icon: theme.SearchIcon()},
{Label: "Profile", Icon: theme.AccountIcon()},
}, show)Place a bar at the bottom of a container.NewBorder and a rail at its
leading side. A common pattern is the bar on narrow windows and the rail on
wide ones; this example shows both so you can compare them.
Top app bar and navigation drawer
drawer := widget.NewNavigationDrawer([]*widget.NavigationDrawerItem{
{Label: "Home", Icon: theme.HomeIcon()},
{Label: "Search", Icon: theme.SearchIcon()},
{Label: "Profile", Icon: theme.AccountIcon()},
{IsDivider: true},
{Label: "About", Icon: theme.InfoIcon()},
}, func(i int) {
// i indexes Items, dividers included, so "About" is 4.
if i < len(pages) {
show(i)
return
}
content.SetText("About")
})
top := widget.NewTopAppBar("My app", theme.MenuIcon(), func() {
drawer.Toggle(w.Canvas())
})
// On a narrow window, labelled actions that don't fit fold into a ⋮
// menu, starting from the last. Pinned keeps Search on the bar.
top.Actions = []widget.TopAppBarAction{
{Icon: theme.SearchIcon(), Label: "Search", Pinned: true, OnTapped: func() { content.SetText("Search") }},
{Icon: theme.SettingsIcon(), Label: "Settings", OnTapped: func() { content.SetText("Settings") }},
{Icon: theme.InfoIcon(), Label: "About", OnTapped: func() { content.SetText("About") }},
}TopAppBar
shows a Title, a leading navigation icon that calls OnNavigation, and
trailing Actions, each a
TopAppBarAction
with an icon and a function.
Give each action a Label. Screen readers read it as the button’s name, and
it lets the action move into an overflow menu when the bar is narrow:
- When the actions would leave the title less than three button widths, the
bar folds actions into a menu behind a ⋮ button. The menu lists each one by
its label and icon, and choosing one calls its
OnTapped. - Actions fold from the last one, so put the most important first. An action
with no
Labelnever folds, and neither does one withPinnedset. - A single action is never folded on its own, since its ⋮ would take the same room.
- On a 360 dp phone with a navigation icon and five labelled actions, the bar shows two actions and the ⋮.
A title that doesn’t fit in the room the actions leave is cut short with an ellipsis before the first action. Screen readers still read the whole title. On a disabled bar the ⋮ still opens its menu, with every item disabled.
NavigationDrawer
is a side panel that you open over the content with Show(canvas) or
Toggle(canvas), and close with Hide(). IsOpen() tells you whether it
is open. It is modal by default: the rest of the window is dimmed while it is
open, and a tap outside the drawer closes it. Tapping a destination calls
OnChanged and closes the drawer. When animations are on, the drawer slides
in from its edge and back out to it.
- An item with
IsDivider: truedraws a separator line. Dividers count as items, so indexes passed toOnChangedinclude them. Headeradds content, such as an account name, above the items.
// Open from the end edge: the right in a left-to-right layout.
drawer.Side = widget.DrawerSideEnd
// Show an X on the bar's button while the drawer is open.
drawer.OnOpenChanged = func(open bool) {
if open {
top.NavigationIcon = theme.CancelIcon()
} else {
top.NavigationIcon = theme.MenuIcon()
}
top.Refresh()
}Sideis the edge the drawer opens against.DrawerSideStart, the default, is the left in a left-to-right layout and the right in a right-to-left one.DrawerSideEndis the other edge.Topis the canvas y where the drawer and its dimmed background begin. Zero, the default, covers the whole height. Set it to open the drawer below a header: taps aboveTopreach the widgets there, such as the header’s buttons, and leave the drawer open.OnOpenChangedruns each time the drawer opens or closes, whatever caused it: your code, a destination, a tap outside, or Escape. It runs on a change only, so showing an open drawer again does not call it.
Bottom app bar
BottomAppBar
is a bar of icon actions with an optional FAB on its right. Pass nil
for the FAB if you do not need one.
fab := widget.NewFAB(theme.ContentAddIcon(), func() { status.SetText("New") })
bottom := widget.NewBottomAppBar([]*widget.BottomAppBarAction{
{Icon: theme.SearchIcon(), OnTapped: func() { status.SetText("Search") }},
{Icon: theme.DeleteIcon(), OnTapped: func() { status.SetText("Delete") }},
}, fab)Place it at the bottom of a container.NewBorder, like a navigation bar.