How to Create a Customizable Animated Bottom Navigation Bar in Flutter

Jan 29, 2023 | Programming

Welcome to the exciting world of Flutter, where creating stunning user interfaces is a breeze! Today, we’ll dive into AnimatedBottomNavigationBar, a delightful widget inspired by a dribble shot. This widget allows you to customize your bottom navigation bars with smooth animations, enhancing the user experience of your app. Let’s get started!

Getting Started

To utilize the AnimatedBottomNavigationBar, place it in the bottomNavigationBar slot of the Scaffold. This widget respects the location of the FloatingActionButton, providing flexibility in design.

Scaffold(
   body: Container(), // destination screen
   floatingActionButton: FloatingActionButton(
      // params
   ),
   floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
   bottomNavigationBar: AnimatedBottomNavigationBar(
      icons: iconList,
      activeIndex: _bottomNavIndex,
      gapLocation: GapLocation.center,
      notchSmoothness: NotchSmoothness.verySmoothEdge,
      leftCornerRadius: 32,
      rightCornerRadius: 32,
      onTap: (index) => setState(() => _bottomNavIndex = index),
      // other params
   ),
);

Building a Navigation Bar with Builder

If you’re looking for more flexibility, you can also customize the bottom navigation bar using the builder method.

Scaffold(
   body: Container(), // destination screen
   floatingActionButton: FloatingActionButton(
      // params
   ),
   floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
   bottomNavigationBar: AnimatedBottomNavigationBar.builder(
      itemCount: iconList.length,
      tabBuilder: (int index, bool isActive) => Icon(
          iconList[index],
          size: 24,
          color: isActive ? colors.activeNavigationBarColor : colors.notActiveNavigationBarColor,
      ),
      activeIndex: _bottomNavIndex,
      gapLocation: GapLocation.center,
      notchSmoothness: NotchSmoothness.verySmoothEdge,
      leftCornerRadius: 32,
      rightCornerRadius: 32,
      onTap: (index) => setState(() => _bottomNavIndex = index),
      // other params
   ),
);

Customization Options

The AnimatedBottomNavigationBar allows for customization with 2, 3, 4, or even 5 navigation elements! Here’s how you can set it up:

Scaffold(
   bottomNavigationBar: AnimatedBottomNavigationBar(
      icons: iconList,
      activeIndex: _bottomNavIndex,
      onTap: (index) => setState(() => _bottomNavIndex = index),
      // other params
   ),
);

Driving Navigation Bar Changes

To programmatically change the active navigation bar tab, you simply have to pass a new activeIndex to the AnimatedBottomNavigationBar widget:

class _MyAppState extends State {
   int activeIndex; // current active index
   
   void _onTap(int index) {
      setState(() => activeIndex = index); // Update the active index
   }
   
   Widget build(BuildContext context) {
      return AnimatedBottomNavigationBar(
         activeIndex: activeIndex,
         onTap: _onTap,
         // other params
      );
   }
}

Troubleshooting Tips

  • Issue: The icons in the navigation bar are not showing up.
    Solution: Ensure that the iconList is properly defined and populated with the appropriate icons.
  • Issue: The active index does not change upon tapping.
    Solution: Check if the setState is being called correctly in your onTap method.
  • Issue: The UI does not look as expected.
    Solution: Double-check the parameters like gapLocation and notchSmoothness to ensure they meet your design requirements.

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

Conclusion

In this article, we explored the power of the AnimatedBottomNavigationBar in Flutter, offering a way to enhance the bottom navigation of your applications with elegance and ease. Your users will appreciate the smooth transitions and intuitive design!

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