Custom Tab Bar in Flutter

This post will discuss custom tab bars and show you how to make tab bars inside of tab bars. Making a tab bar is extremely simple; all you need to do is return the default controller, specify the length, and build the tab bar. You can also refer this article of Work with Tabs.

While Flutter comes with a default TabBar widget, there may be times when you want to design or programme an own tab bar. This tutorial will show you how to design a unique tab bar in Flutter.

However, in this post, we’ll look at how to make a tab bar, customise it to suit your needs, and add a subtab bar to it.

The tabbar is one of the most popular widgets in the flutter app, and most businesses favour using it in their software.

Open the lib/main.dart file and replace its contents with the following code:

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Custom Tab Bar',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MainCustomTab(),
    );
  }
}

In this code, we’ve created a basic MyApp widget that sets up the Flutter app’s structure. The MainCustomTab widget is set as the home screen.

Implementing the Custom Tab Bar

Create a new file called main_custom_tab.dart in the lib directory and add the following code:

import 'package:flutter/material.dart';

class MainCustomTab extends StatefulWidget {
  @override
  State<MainCustomTab> createState() => _MyCustomState();
}

class _MyCustomState extends State<MainCustomTab> {
  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
      length: 2,
      child: Scaffold(
        backgroundColor: Colors.white,
        appBar: AppBar(
          title: Text("Custom Tab Bar"),
          automaticallyImplyLeading: false,
        ),
        body: Padding(
          padding: const EdgeInsets.all(8.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(5),
                  color: Colors.green,
                ),
                child: TabBar(
                  indicator: BoxDecoration(
                    color: Colors.green[800],
                    borderRadius: BorderRadius.circular(5),
                  ),
                  labelColor: Colors.black,
                  tabs: [
                    Tab(
                      child: Text("Tab 1"),
                      icon: Icon(
                        Icons.abc_sharp,
                        color: Colors.black,
                      ),
                    ),
                    Tab(
                      child: Text("Tab 2"),
                      icon: Icon(
                        Icons.ac_unit_sharp,
                        color: Colors.black,
                      ),
                    ),
                  ],
                ),
              ),
              Expanded(
                child: TabBarView(children: [
                  MyTabOne(),
                  MyTabTwo(),
                ]),
              )
            ],
          ),
        ),
      ),
    );
  }
}

class MyTabOne extends StatelessWidget {
  const MyTabOne({super.key});

  @override
  Widget build(BuildContext context) {
    return Center(
        child: Text(
      "This is Tab One",
      style: TextStyle(fontSize: 20),
    ));
  }
}

class MyTabTwo extends StatelessWidget {
  const MyTabTwo({super.key});

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      margin: EdgeInsets.fromLTRB(0, 50, 0, 0),
      child: Column(
        children: [
          Center(
              child: Text(
            "This is Tab Two",
            style: TextStyle(fontSize: 20),
          )),
          Container(
            padding: EdgeInsets.fromLTRB(5, 0, 5, 0),
            margin: EdgeInsets.fromLTRB(5, 5, 5, 0),
            child: TextButton(
              style: TextButton.styleFrom(
                shadowColor: Colors.black,
                textStyle: TextStyle(fontSize: 20),
                primary: Colors.white,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(10.0),
                ),
                backgroundColor: Colors.grey,
              ),
              onPressed: () async {
                print("Click Here");
              },
              child: Text(
                "Click Here",
                style: TextStyle(color: Colors.white, fontSize: 12),
              ),
            ),
          ),
        ],
      ),
    );
  }
}

I’ve written all of my code in this file, but you could make a separate file for that.

Output:

In this article, we learnt how to use the TabBar and TabBarView widgets to build a custom tab bar in Flutter.

We looked into altering the tab bar’s visual style and showed various content on each tab. Feel free to play around with various layouts and improve the usability of your personalised tab bar.

Related Posts

Bottom Tab Bar in Flutter

Tabbar with image in flutter

Simple Tabbar in Flutter

Progress Bars Example in Flutter

Cardview In Androidx – Material Design

There are so many properties in Android Material Design. In this tutorial we discuss about cardview. In Material Design Cardview we can design so many things at creative way.

Cardview – It is a material design component in 2014. It is very easy to use and understand for android developers. The main pupose of cardview is a looking good interface in Listview and all other feature like images and text. You can also changed some color in cardview with allegant background and use in proper manner.

How to Implement Cardview in Android App?

First of all you have to add cardview support library in your build.gradle file. And then click on sync button in Android Studio. So After successfull added you can use cardview on your app. Below code of support library.

implementation 'androidx.cardview:cardview:1.0.0'

Now you can add cardview in your XML File.

<androidx.cardview.widget.CardViewxmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_Data"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

Now there are so many properties of cardview.

Give Effect of Cardview clickevent. Code given Below

android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"

if you want to change cardview background color then use this code for the cardview.

card_view:cardBackgroundColor="#FFF" // (XML)

In JAVA

cardView.setCardBackgroundColor(Color.WHITE); // (JAVA)

Give Cardview Corner shape. Use this code

card_view:cardCornerRadius="7dp" // (XML)

In JAVA

[php] cardView.setRadius(0); // (JAVA) [/php]

If you want to give elevation of cardview. Then use this code

[php] card_view:cardElevation=”7dp” // (XML) [/php]

In JAVA

[php] cardView.setCardElevation(2.1f); // (JAVA) [/php]

Give content padding in cardview. Use below code.

[php] card_view:contentPadding=”7dp” // (XML) [/php]

In JAVA

[php] cardView.setContentPadding(30, 30, 30, 0); // (JAVA) [/php]