Attractive Tab Controller for Flutter App

Introduction

The tabs are mostly used for navigating on mobile devices. Tabs are styled differently on different operating systems. For example, on Android devices, it is at the top of the screen, whereas on iOS devices, it is towards the bottom.

Working with tabs is a popular technique in Material Design-compliant Android and iOS applications. Flutter makes it simple to construct a tabbed layout. Create a TabBar and TabBarView and attach them to the TabController to add tabs to the project. The controller will synchronize both so that we may achieve the desired behavior.

A example code to get a simple Tabbar up and running from the ground up is:

DefaultTabController(
  length: 3,
  child: Scaffold(
    appBar: AppBar(
      bottom: TabBar(
        tabs: [
          Tab(icon: Icon(Icons.name)),
          Tab(icon: Icon(Icons.roll_no)),
          Tab(icon: Icon(Icons.class_section)),
        ],
      ),
      title: Text('Tabs Demo'),
    ),
    body: TabBarView(
      children: [
        Icon(Icons.name, size: 350),
        Icon(Icons.roll_no, size: 350),
        Icon(Icons.class_section, size: 350),
      ],
    ),
  ),
);

Source Code

  • account.dart
import 'package:flutter/material.dart';

class Account extends StatefulWidget {
  const Account({Key? key}) : super(key: key);

  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Account> {
  int value = 0;
  final first = TextEditingController();
  final second = TextEditingController();

  @override
  void dispose() {
    first.dispose();
    second.dispose();
    super.dispose();
  }



  @override
  Widget build(BuildContext context) {
    return Scaffold(

      body: Column(
        children: [
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: TextField(
              controller: first,
              keyboardType: TextInputType.number,
              decoration: InputDecoration(
                border: OutlineInputBorder(),
                hintText: 'First Number'
              ),
            ),
          ),
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: TextField(
              controller: second,
              keyboardType: TextInputType.number,
              decoration: InputDecoration(
                border: OutlineInputBorder(),
                hintText: 'Second Number'
              ),
            ),
          ),

          Padding(
            padding: const EdgeInsets.all(18.0),
            child: Text(
                "$value",
                style:TextStyle(
                fontSize: 24,
                fontWeight: FontWeight.bold
              )
            ),
          ),
          Padding(
            padding: const EdgeInsets.all(18.0),
            child: InkWell(
              child: Text(
                  "Calculate",
                  style:TextStyle(
                      fontSize: 34,
                      fontWeight: FontWeight.bold
                  )
              ),
              onTap: (){
                setState(() {
                  if(int.parse(first.text) > int.parse(second.text)){
                    value = int.parse(second.text);
                  }else{
                    value = int.parse(first.text);
                  }
                });
              },
            ),
          ),
        ],
      ),
    );
  }
}
  • home.dart
import 'package:flutter/material.dart';

class Home extends StatefulWidget {
  const Home({Key? key}) : super(key: key);

  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(backgroundColor: Colors.black,);
  }
}
  • main.dart
import 'package:flutter/material.dart';
import 'package:tabbar/account.dart';
import 'package:tabbar/home.dart';
import 'package:tabbar/message.dart';
import 'package:tabbar/notification.dart';


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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.red,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin{

  late TabController _tabController;
  int _currentPosition=0;

  final _pages = [
    Home(),
    Messages(),
    Notificaitons(),
    Account(),

  ];

  @override
  void initState() {
    _tabController = TabController(length: 4, vsync: this);
    super.initState();
  }
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar:AppBar(
        title:Text("Tab Bar Demo"),
        bottom: TabBar(
          controller: _tabController,
          tabs: [
            Tab(
              icon: Icon(Icons.home),
              text: "Home",
            ),
            Tab(
              icon: Icon(Icons.message),
              text: "Message",
            ),
            Tab(
              icon: Icon(Icons.notifications),
              text: "Notifications",
            ),
            Tab(
              icon: Icon(Icons.account_balance),
              text: "Account",
            ),

          ],
          onTap: (position){
            setState(() {
              _currentPosition = position;
              print(position);
            });
          },
        ),
      ),
      drawer: Drawer(),
      body: _pages[_currentPosition],
    );
  }
}
  • message.dart
import 'package:flutter/material.dart';

class Messages extends StatefulWidget {
  const Messages({Key? key}) : super(key: key);

  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Messages> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(backgroundColor: Colors.pink,);
  }
}
  • notification.dart
import 'package:flutter/material.dart';

class Notificaitons extends StatefulWidget {
  const Notificaitons({Key? key}) : super(key: key);

  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Notificaitons> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(backgroundColor: Colors.yellow,);
  }
}

Tab Bar for Flutter Demo

first
second
third
fourth

GitHub

Source Code: Attractive Tab Controller for Flutter App.

SHARE Attractive Tab Controller for Flutter App

You may also like...

Leave a Reply

Your email address will not be published.

Share