Flutter TabBar and TabBarView

Tabs let users switch between related pages without leaving the screen. A music app uses tabs for Songs, Albums, and Artists. A shopping app uses them for Men, Women, and Kids. Flutter builds tabs from three cooperating widgets: a controller, a bar that shows the labels, and a view that shows the pages.

The Three Parts


  ┌──────────────────────────────────────┐
  │ AppBar                               │
  │  ┌────────┬────────┬────────┐        │
  │  │ Songs  │ Albums │ Artists│ ← TabBar
  │  └────────┴────────┴────────┘        │
  ├──────────────────────────────────────┤
  │                                      │
  │   Page for the selected tab          │ ← TabBarView
  │                                      │
  └──────────────────────────────────────┘
       TabController links the two

The TabController acts like a remote control. It remembers the selected tab and tells both the bar and the view to stay in step.

The Quick Method: DefaultTabController

Wrap a Scaffold in DefaultTabController and Flutter creates the controller for you.


class TabsDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 3,
      child: Scaffold(
        appBar: AppBar(
          title: Text('Music'),
          bottom: TabBar(
            tabs: [
              Tab(text: 'Songs'),
              Tab(text: 'Albums'),
              Tab(text: 'Artists'),
            ],
          ),
        ),
        body: TabBarView(
          children: [
            Center(child: Text('Songs page')),
            Center(child: Text('Albums page')),
            Center(child: Text('Artists page')),
          ],
        ),
      ),
    );
  }
}

The length value must match both the number of tabs and the number of pages. A mismatch throws an error at runtime.

Tab Labels with Icons

The Tab widget accepts text, an icon, or both.


Tab(icon: Icon(Icons.music_note), text: 'Songs')

Swiping Between Pages

Users swipe left and right across a TabBarView to change tabs. Tapping a label does the same. The bar and the view update each other without extra code.

Styling the Bar

PropertyEffect
isScrollableAllows many tabs that scroll sideways
indicatorColorColors the underline of the selected tab
labelColorColors the selected label
unselectedLabelColorColors the other labels
indicatorWeightSets the underline thickness

TabBar(
  isScrollable: true,
  indicatorColor: Colors.amber,
  labelColor: Colors.white,
  unselectedLabelColor: Colors.white70,
  tabs: [ /* tabs here */ ],
)

Set isScrollable to true when you have five or more tabs. Fixed tabs squeeze labels together on narrow phones.

Manual Control with TabController

Create your own controller when code must change tabs, or when you want to react to a tab change. The state class needs SingleTickerProviderStateMixin, a mixin that supplies the timing for the sliding animation.


class MyTabs extends StatefulWidget {
  @override
  State<MyTabs> createState() => _MyTabsState();
}

class _MyTabsState extends State<MyTabs> with SingleTickerProviderStateMixin {
  late TabController _controller;

  @override
  void initState() {
    super.initState();
    _controller = TabController(length: 3, vsync: this);
    _controller.addListener(() {
      print('Selected tab: ${_controller.index}');
    });
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        bottom: TabBar(
          controller: _controller,
          tabs: [Tab(text: 'A'), Tab(text: 'B'), Tab(text: 'C')],
        ),
      ),
      body: TabBarView(
        controller: _controller,
        children: [Text('A'), Text('B'), Text('C')],
      ),
    );
  }
}

Call _controller.animateTo(2) to jump to the third tab from a button press. Always call dispose to release the controller.

Keeping Tab State Alive

A TabBarView destroys pages that scroll out of view. A scrolled list resets to the top when the user returns. Add AutomaticKeepAliveClientMixin to the page state to keep it alive.


class _SongsPageState extends State<SongsPage> with AutomaticKeepAliveClientMixin {
  @override
  bool get wantKeepAlive => true;

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return ListView( /* items */ );
  }
}

Tabs at the Bottom or in the Body

Material design places tabs under the app bar. Bottom navigation suits top-level sections of an app, while tabs suit content categories inside one section. Place a TabBar anywhere in a Column when a design needs tabs in the middle of a page. Pair it with an Expanded TabBarView below.

Design Tips

  • Limit tabs to two through six labels.
  • Write short labels of one or two words.
  • Order tabs by importance, with the most used tab on the left.
  • Show the same type of content in every tab.

Accessibility Notes

Screen readers announce each tab label and its position, such as "Albums, tab 2 of 3". Short, clear labels make that announcement useful. Icons without text need a semanticLabel or tooltip, or blind users hear nothing meaningful.

Practice Ideas

Create a recipe app with tabs for Breakfast, Lunch, and Dinner. Show a ListView of dish names in each tab. Add a floating button that jumps to the Dinner tab through a manual controller. Test that the Lunch list keeps its scroll position after you switch away and return.

Combining Tabs with Navigation

Tabs and screen navigation solve different problems. Tabs switch between sibling views inside one screen, and the back button leaves the screen entirely. A detail page opened from a tab list pushes a new route on top of the tabs, and returning restores the same selected tab. Keep that separation in mind when planning an app: use tabs for peer categories and routes for drill-down content.