Flutter Tabs Ultimate Guidance

Flutter Tabs Ultimate Guidance

Flutter Tabs are precisely what they sound like. They’re a component of the user interface that, when clicked, takes the user to several destinations or pages. Tabs are commonly used in applications. Flutter makes it easy to use the material library to make tab layouts. 

Mobile navigation is the primary use case for tabs, which vary in appearance across various operating systems. For example, they appear at the very bottom of iOS devices’ screens yet at the very top of Android devices’ screens.

What is Tabbar in Flutter?

Apps developed for iOS and Android that adhere to the Material Design principles often have tabbed navigation. An easy method to create a tab layout is available in Flutter Tabs. To incorporate tabs into the application, we must first construct a TabBar and tie it to the TabController. The controller will synchronize both to achieve the desired behavior.

Here is a full example of how to set up an interface with tabs:

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {

  @override

  Widget build(BuildContext context) {

    return MaterialApp(

      title: 'Tabbed Navigation Demo',

      theme: ThemeData(

        primarySwatch: Colors.blue,

      ),

      home: MyHomePage(),

    );

  }

}

class MyHomePage extends StatefulWidget {

  @override

  _MyHomePageState createState() => _MyHomePageState();

}

class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin {

  TabController? _tabController;

  @override

  void initState() {

    super.initState();

    _tabController = TabController(length: 3, vsync: this);

  }

  @override

  void dispose() {

    _tabController?.dispose();

    super.dispose();

  }

  @override

  Widget build(BuildContext context) {

    return Scaffold(

      appBar: AppBar(

        title: Text('TabBar Example'),

        bottom: TabBar(

          controller: _tabController,

          tabs: <Widget>[

            Tab(text: 'Tab 1'),

            Tab(text: 'Tab 2'),

            Tab(text: 'Tab 3'),

          ],

        ),

      ),

      body: TabBarView(

        controller: _tabController,

        children: <Widget>[

          Center(child: Text('Content for Tab 1')),

          Center(child: Text('Content for Tab 2')),

          Center(child: Text('Content for Tab 3')),

        ],

      ),

    );

  }

}

This example shows how to use TabBar and TabBarView with a TabController to make a split interface in a Flutter app. Tabs are a flexible way to navigate, and they’re often used to organize information and make the user experience better in mobile apps.

Work With Flutter Tabs

Working with tabs is a common application pattern that adheres to the Material Design rules. Flutter, part of the material library, offers a handy method for designing tab layouts.

Generate a Tab Controller

If Flutter Tabs are to function, you must maintain the chosen tab and content sections in sync. The TabController handles this. Either manually or automatically, using a Default TabController widget, build a TabController. The easiest choice is to use DefaultTabController since it generates a TabController and makes it accessible to all descendant widgets.

When you use DefaultTabController, it’s easy to add tabs because it automatically creates and manages the TabController. This method works for simple situations where you don’t need to directly control the TabController.

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {

  @override

  Widget build(BuildContext context) {

    return MaterialApp(

      title: 'DefaultTabController Example',

      theme: ThemeData(

        primarySwatch: Colors.blue,

      ),

      home: MyHomePage(),

    );

  }

}

class MyHomePage extends StatelessWidget {

  @override

  Widget build(BuildContext context) {

    return DefaultTabController(

      length: 3, // Number of tabs

      child: Scaffold(

        appBar: AppBar(

          title: Text('DefaultTabController Example'),

          bottom: TabBar(

            tabs: <Widget>[

              Tab(text: 'Tab 1'),

              Tab(text: 'Tab 2'),

              Tab(text: 'Tab 3'),

            ],

          ),

        ),

        body: TabBarView(

          children: <Widget>[

            Center(child: Text('Content for Tab 1')),

            Center(child: Text('Content for Tab 2')),

            Center(child: Text('Content for Tab 3')),

          ],

        ),

      ),

    );

  }

}

Form The Flutter Tabs

Selecting a tab requires it to show contents. The TabBar widget lets you build tabs. In this case, put a three-tab bar created by three-tab widgets inside an AppBar.

The TabBar searches the widget tree by default for the closest DefaultTabController. Send the Tab Controller you are hand-building to the TabBar. Example:

We’ll make an flutter tabs with a flutter tabs that has three tabs in this case. We’ll directly manage the TabController to make sure that the tabs are in sync with their content.

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {

  @override

  Widget build(BuildContext context) {

    return MaterialApp(

      title: 'Custom TabController Example',

      theme: ThemeData(

        primarySwatch: Colors.blue,

      ),

      home: MyHomePage(),

    );

  }

}

class MyHomePage extends StatefulWidget {

  @override

  _MyHomePageState createState() => _MyHomePageState();

}

class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin {

  late TabController _tabController;

  @override

  void initState() {

    super.initState();

    // Initialize the TabController with 3 tabs

    _tabController = TabController(length: 3, vsync: this);

  }

  @override

  void dispose() {

    // Dispose of the TabController when the widget is removed from the tree

    _tabController.dispose();

    super.dispose();

  }

  @override

  Widget build(BuildContext context) {

    return Scaffold(

      appBar: AppBar(

        title: Text('Custom TabController Example'),

        bottom: TabBar(

          // Use the manually created TabController

          controller: _tabController,

          tabs: <Widget>[

            Tab(text: 'Tab 1'),

            Tab(text: 'Tab 2'),

            Tab(text: 'Tab 3'),

          ],

        ),

      ),

      body: TabBarView(

        // Use the same TabController for the TabBarView

        controller: _tabController,

        children: <Widget>[

          Center(child: Text('Content for Tab 1')),

          Center(child: Text('Content for Tab 2')),

          Center(child: Text('Content for Tab 3')),

        ],

      ),

    );

  }

}

In this example, we make and control a TabController by hand so that it can handle tab selection and keep TabBar and TabBarView in sync flutter tabs. This method gives you fine-grained control over how tabs behave and is useful when you need to change more than just the normal DefaultTabController in flutter forums.

Conclusion

The easiest way to handle tabs in Flutter Tabs is to use DefaultTabController, which takes care of the creation and lifecycle of TabControllers automatically. You can make a TabController by hand and handle it directly if you want more control. Both methods make sure that tabs and information are always in sync, which makes switching between views in your app easy.

Similar Posts