How to Create a Motion Tab Bar in Flutter 3 with RTL Support

May 4, 2022 | Programming

In this article, we will explore how to integrate a beautiful animated Motion Tab Bar into your Flutter apps. This widget not only enhances your app’s aesthetics but also provides robust functionality, including support for RTL layouts. Let’s dive right in!

Getting Started

To use the Motion Tab Bar in your Flutter application, you’ll first need to add the necessary plugin.

  • Open your `pubspec.yaml` file.
  • Add the following dependency:
dependencies:
  motion_tab_bar: ^2.0.4

Basic Usage

With the plugin added, you can start using the Motion Tab Bar. Below are the steps to implement it.

1. Initialize Controllers

You can utilize either the default TabController or the MotionTabBarController. The latter is particularly useful for programmatically changing tabs.


TabController _tabController;
MotionTabBarController? _motionTabBarController;

@override
void initState() {
  super.initState();
  _tabController = TabController(
    initialIndex: 1,
    length: 4,
    vsync: this,
  );

  // Using MotionTabBarController for programmatically changing the tab
  _motionTabBarController = MotionTabBarController(
    initialIndex: 1,
    length: 4,
    vsync: this,
  );
}

@override
void dispose() {
  super.dispose();
  _tabController.dispose();
  _motionTabBarController!.dispose();
}

2. Add the Motion Tab Bar

Next, embed the Motion Tab Bar within your Scaffold’s bottomNavigationBar.


bottomNavigationBar: MotionTabBar(
  controller: _motionTabBarController,
  initialSelectedTab: Home,
  labels: const [Dashboard, Home, Profile, Settings],
  icons: const [Icons.dashboard, Icons.home, Icons.people_alt, Icons.settings],
  badges: [
    const MotionBadgeWidget(
      text: 10+, 
      textColor: Colors.white,
      color: Colors.red, 
      size: 18,
    ),
    Container(
      color: Colors.black,
      padding: const EdgeInsets.all(2),
      child: const Text(
        11,
        style: TextStyle(fontSize: 14, color: Colors.white),
      ),
    ),
    null,
    const MotionBadgeWidget(
      isIndicator: true,
      color: Colors.blue,
      size: 5,
      show: true,
    ),
  ],
  tabSize: 50,
  tabBarHeight: 55,
  textStyle: const TextStyle(fontSize: 12, color: Colors.black, fontWeight: FontWeight.w500),
  tabIconColor: Colors.blue[600],
  tabIconSize: 28.0,
  tabIconSelectedSize: 26.0,
  tabSelectedColor: Colors.blue[900],
  tabIconSelectedColor: Colors.white,
  tabBarColor: Colors.white,
  onTabItemSelected: (int value) {
    setState(() {
      _tabController!.index = value;
      _motionTabBarController!.index = value;
    });
  },
)

3. Add TabBarView

For the body of your Scaffold, include a TabBarView that contains your desired widgets.


body: TabBarView(
  physics: NeverScrollableScrollPhysics(), // Swipe navigation handling is not supported
  controller: _tabController,
  controller: _motionTabBarController,
  children: [
    const Center(child: Text(Dashboard)),
    const Center(child: Text(Home)),
    const Center(child: Text(Profile)),
    const Center(child: Text(Settings)),
  ],
)

Changing Tabs Programmatically

If you’d like to change tabs via buttons, you can set the MotionTabBarController’s index when the button is pressed:


ElevatedButton(
  onPressed: () => _motionTabBarController.index = 0,
  child: const Text(Dashboard Page),
),
ElevatedButton(
  onPressed: () => _motionTabBarController.index = 1,
  child: const Text(Home Page),
),

Troubleshooting

If you encounter any issues while integrating the Motion Tab Bar, here are some troubleshooting tips:

  • Ensure that the motion_tab_bar package is correctly added to your pubspec.yaml file and you have run flutter pub get.
  • If the tab states do not update as expected, check whether the controllers are disposed of correctly in your dispose method.
  • For a seamless experience with RTL layouts, verify you are adhering to the properties set for RTL compatibility.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Final Thoughts

With just a few simple steps, you can incorporate an animated Motion Tab Bar into your Flutter applications. This widget not only enhances the user experience but also makes your app visually appealing.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox