Flutter Nested TabBar

Flutter Nested Tabs Tutorial

This is a nested tabs example project.

This is great if you want tabs with probably a header on top of them. Normally tabs are displayed at the bottom of the appBar. What about if you want to include say an image or some card between the tabbar and appbar. This is what we create.

We will in the process understand around 4 classes that are mandatory for creating tabs:

  1. TabController/DefaultTabController.
  2. TabBar
  3. TabView
  4. Tab

Let’s start.

Video tutorial(ProgrammingWizards TV Channel)

Well we have a video tutorial as an alternative to this. If you prefer tutorials like this one then it would be good you subscribe to our YouTube channel. Basically we have a TV for programming where do daily tutorials especially android.

Flutter Nested Tabs Example

This is a flutter nested tabs example. We will have a container on top of our TabBar. That container can of course contain anything like say an image or some text.

Here’s the example:

Flutter Nested Tabs

Flutter Nested Tabs


Then below it we have our tabs using the DefaultTabController. We will give them a dark them.

Here are the things to note.

  1. The container on top of our tabbar can contain any widget.
  2. Our Tabs will be clickable and swipeable.

Dart Concepts we Learn in This Tutorial

You can use this tutorial even if you have never tasted Dart before.

Here are the important concepts to get you started:

1. Importing Packages in Flutter/Dart

But first we need to import some packages and we do that using the import keyword in dart.

We do that using the import keyword, similar to java.

You use import to specify how a namespace from one library is used in the scope of another library.
Remember every Dart app is a library,even if it doesn’t use a library directive.

To import a given library, the only required argument is a URI specifying the library.

If the library is a built-in/standard one, then the URI has the special dart: scheme. For other libraries, you can use a file system path or the package: scheme. The package: scheme specifies libraries provided by a package manager such as the pub tool.

For in our example here:

import 'package:flutter/material.dart';
import 'package:flutter_date_picker/flutter_date_picker.dart';

Here are the packages we are importing:

  1. material.dart : will give us material design widgets and theme.
  2. flutter_date_picker.dart : Will give us DatePicker to work get and set dates with.

2. Classes and Object in Flutter/Dart

In Dart like in most C style programming languages you create a class using the class keyword. Classes are encapsulation mechanism in Object Oriented Programming languages.

An object on the other hand is an instance of a class and all classes descend from Object.

To create an object, you can use the new keyword with a constructor for a class. Constructor names can be either ClassName or ClassName.identifier:

var myApp = new MyApp(); // Create a MyApp object using MyApp().

In this case we create three classes:

  1. class MyApp...
  2. class Home...

4. Class Inheritance in Dart

Inheritance is an object oriented pillar, one of the main ones. And Dart definitely supports it fully. Inheritance provides alot of power to programming languages like Dart as it allows one class to derive properties and methods of another.

In Dart to implement inheritance you use the extends keyword just like in java.
Here are the keywords you may see in this tutorial:

  1. extends – creates a subclass.
  2. super – refers to the superclass or parent class.

Here are examples

  1. class MyApp extends StatelessWidget {..}
  2. class Home extends StatefulWidget {..}

3. Class-Level vs Top Level Functions

Class Level Functions, typically known as methods, are methods confined inside a class and belonging either to the instance of the class or the class itself. Top Level functions are not confined to any class.

Dart supports different function types like:

  • Top level functions like the main()),
  • Static Functions – That is functions tied to a class.
  • Instance Methods -Functions tied to an object.
  • Nested or Local Functions : functions within functions.

Here’s our top level main function:

void main() => runApp(MyApp());

4. The main method in Dart

In Dart, like in Java and C#, the entry point to an application is the famous main() method. When your application starts, the main() method gets invoked first and when it finishes the app also finishes.

In our case we run our app in the main method.

void main() => runApp(MyApp());

5. Fat Arrow Shorthand Syntax in Dart

Dart is a language that allows you write short concise and compact code. One of the features it provides is the fat arror shorthand syntax.

For functions that contain just one expression, you can use this shorthand syntax.

For example instead of this:

void main() {

You can write this:

void main() => runApp(MyApp());

Only an expression and not a statement can appear between the arrow
(=>) and the semicolon (;).

6. In Dart Functions are First-Class Objects

This means you can pass a function as a parameter to another function.

For example:

showNumber(myNumber) {
var myNumbers = [1,2,3];
myNumbers.forEach(showNumber); // Pass showNumber as a parameter.

7. In Dart All functions return a value.

Yeah. If no return value is specified, the statement return null; is implicitly appended to the function body.

8. If and Else in Dart
Dart supports if statements with optional else statements.

if (engineRunning()) {
} else if (engineStopped()) {
} else {

9. For Loops in Dart
Like many languages, in dart you can iterate with the standard for loop.

for (int i = 0; i < galaxies.length; i++) {

Closures inside of Dart’s for loops capture the value of the index, avoiding a co
pitfall found in JavaScript. For example, consider:

var callbacks = [];
for (var i = 0; i < 2; i++) {
    callbacks.add(() => print(i));
callbacks.forEach((c) => c());

The output is 0 and then 1, as expected. In contrast, the example would print 2 and then 2 in JavaScript.

If the object that you are iterating over is a Collection, you can use the forEach() method. That is if you don’t need to know the current iteration counter:

galaxies.forEach((galaxy) => galaxy.getName());

Collections also support the for-in form of iteration:

var collection = [0, 1, 2];
for (var planet in planets) {

Flutter API’s we use in this Project

We are using Dart specifically to write flutter apps. You can target either Android or iOS.

Let’s look at some of the APIs we will be using in this project

1. Widget

A Widget is an abstract class in flutter package that describes the configuration for an Element.

Widgets are the central class hierarchy in the Flutter framework. A widget is an immutable description of part of a user interface. Widgets can be inflated into elements, which manage the underlying render tree.

We will be implementing two Widget sub-classes in this class:

  1. StatelessWidget
  2. StatefulWidget

This this will allow us override the build() method hence returning a Widget object:

class MyApp extends SomeWidget
  Widget build(BuildContext context) {
      return our_widget;

The build() method describes the part of the user interface represented by this widget.

Read more about Widget here.

2. StatelessWidget

A statelesswidget widget is an abstract class that represents a widget that does not require mutable state.

Read more about StatelessWidget here.

3. BuildContext

A BuildContext is a handle to the location of a widget in the widget tree.

It’s an abstract class that presents a set of methods that can be used from StatelessWidget.build methods and from methods on State objects.

Read more about BuildContext here.

5. MaterialApp

MaterialApp is a class that represents an application that uses material design.

Read more about MaterialApp here.

6. ThemeData

ThemeData is a class that holds the color and typography values for a material design theme.

Read more about ThemeData here.

7. DefaultTabController

DefaultTabController is the TabController for descendant widgets that don’t specify one explicitly.

DefaultTabController derives from the StatefulWidget and has a mutable state.

Read more about DefaultTabController here.

8. TabBar

TabBar is a material design widget that displays a horizontal row of tabs.

Read more about TabBar here.

9. TabBarView

TabBarView is a page view that displays the widget which corresponds to the currently selected tab. Typically used in conjunction with a TabBar.

Read more about TabBarView here.

10. Tab

A material design TabBar tab.

Read more about Tab here.

Here are the files we explore:

  1. pubspec.yaml
  2. lib/main.dart

(a). pubspec.yaml

Flutter supports using shared packages contributed by other developers to the Flutter and Dart ecosystems. This allows you to quickly build your app without having to develop everything from scratch.

We will be adding packages under this file.

  1. Open the pubspec.yaml file located inside your app folder, and add dependencies under dependencies.
  2. If it is a third party then you have to install it.
    From the terminal: Run flutter packages get


From Android Studio/IntelliJ: Click ‘Packages Get’ in the action ribbon at the top of pubspec.yaml
From VS Code: Click ‘Get Packages’ located in right side of the action ribbon at the top of pubspec.yaml

flutter is always our sdk dependency as we use it to develop our ios and android apps.

Here’s our pubspec.yaml file:

name: nested_tabbar
description: A new Flutter project.

    sdk: flutter

  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^0.1.2

    sdk: flutter

# For information on the generic Dart part of this file, see the
# following page: https://www.dartlang.org/tools/pub/pubspec

# The following section is specific to Flutter.

  # The following line ensures that the Material Icons font is
  # included with your application, so that you can use the icons in
  # the material Icons class.
  uses-material-design: true

Take note we are not using any third party dependency in our app.

(b). main.dart

This is where we write our flutter code in dart programming language. In Dart it is very common to write several classes in a single file. So this simple file will have three classes.

Full Code

here’s the full main.dart code:

import 'package:flutter/material.dart';

class MyApp extends StatelessWidget {
  Widget build(BuildContext context) {
    return new MaterialApp(
        title: 'Mr Nested TabBar',
        theme: ThemeData(brightness: Brightness.dark),
        home: Scaffold(body: HomePage()));

class HomePage extends StatelessWidget {
  Widget build(BuildContext context) {
    return ListView(children: [
          color: Colors.orangeAccent,
          height: 150.0,
          child: Center(child: Text('Something'))),
          length: 2,
          initialIndex: 0,
          child: Column(
            children: [
              TabBar(tabs: [Tab(text: 'Home'), Tab(text: 'News')]),
                  height: 300.0, 
                  child: TabBarView(
                    children: [
                      Center(child: Text('Home here')),
                      Center(child: Text('News here')),

// Our top level main function
void main() => runApp(new MyApp());

(c) Running Flutter Applications

Just make sure you device or emulator is running and click the run button in android studio, it will automatically pick the device and install the app.

Aletrnative you can use the terminal or command prompt. Navigate/Cd over to project root page and type this:

flutter.bat build apk

This will build the APK which you can then drag and install onto your device. The build process in my machine takes around three minutes which is not bad.


You can download full source code below.

No. Location Link
1. GitHub Direct Download
2. GitHub Browse



Share an Example

Share an Example

What is the capital of Egypt? ( Cairo )