How to Create a Beautiful Bottom Navigation Bar in Flutter

Sep 1, 2024 | Programming

Are you looking to create a visually appealing and functional bottom navigation bar for your Flutter app? Look no further! The BottomNavyBar package provides a seamless and captivating way to implement a customizable bottom navigation bar that perfectly fits your app’s theme and enhances user experience.

Getting Started with BottomNavyBar

To kick off your journey, you’ll need to add the BottomNavyBar dependency to your Flutter project. The process is simple:

dependencies:
  bottom_navy_bar: ^6.0.0

Basic Usage

Once you’ve added the dependency, integrating the BottomNavyBar into your app is a breeze. Here’s a step-by-step guide:

  • Define the BottomNavyBar widget within your scaffold’s bottom navigation bar.
  • Handle the selected index to manage the navigation behavior.
  • Customize the items in the BottomNavyBar according to your app’s needs.

Here’s an analogy to help you visualize how the code works: Think of the BottomNavyBar as a map guiding you through different destinations in a city. Each destination (or screen) is represented by a different icon. When you tap on an icon, it’s like taking a turn on the road — you smoothly transition from one location to another, all while the navigation bar gently updates to reflect your current position.

bottomNavigationBar: BottomNavyBar(
  selectedIndex: _selectedIndex,
  showElevation: true,
  onItemSelected: (index) {
      setState(() {
          _selectedIndex = index;
          _pageController.animateToPage(index,
              duration: Duration(milliseconds: 300), curve: Curves.ease);
      });
  },
  items: [
      BottomNavyBarItem(
          icon: Icon(Icons.apps),
          title: Text('Home'),
          activeColor: Colors.red,
      ),
      BottomNavyBarItem(
          icon: Icon(Icons.people),
          title: Text('Users'),
          activeColor: Colors.purpleAccent,
      ),
      BottomNavyBarItem(
          icon: Icon(Icons.message),
          title: Text('Messages'),
          activeColor: Colors.pink,
      ),
      BottomNavyBarItem(
          icon: Icon(Icons.settings),
          title: Text('Settings'),
          activeColor: Colors.blue,
      ),
  ],
)

Implementing with PageView and PageController

To make your navigation functional, you’ll want to pair the BottomNavyBar with a PageView. This allows you to swipe between different pages while also being able to tap on the bottom navigation items.

Here’s how to implement it:

class _MyHomePageState extends State {
  int _currentIndex = 0;
  PageController _pageController;

  @override
  void initState() {
      super.initState();
      _pageController = PageController();
  }

  @override
  void dispose() {
      _pageController.dispose();
      super.dispose();
  }

  @override
  Widget build(BuildContext context) {
      return Scaffold(
          appBar: AppBar(title: Text('Bottom Nav Bar')),
          body: SizedBox.expand(
              child: PageView(
                  controller: _pageController,
                  onPageChanged: (index) {
                      setState(() => _currentIndex = index);
                  },
                  children: [
                      Container(color: Colors.blueGrey,),
                      Container(color: Colors.red,),
                      Container(color: Colors.green,),
                      Container(color: Colors.blue,),
                  ],
              ),
          ),
          bottomNavigationBar: BottomNavyBar(
              selectedIndex: _currentIndex,
              onItemSelected: (index) {
                  setState(() => _currentIndex = index);
                  _pageController.jumpToPage(index);
              },
              items: [
                  BottomNavyBarItem(
                      title: Text('Item One'),
                      icon: Icon(Icons.home)
                  ),
                  BottomNavyBarItem(
                      title: Text('Item Two'),
                      icon: Icon(Icons.apps)
                  ),
                  BottomNavyBarItem(
                      title: Text('Item Three'),
                      icon: Icon(Icons.chat_bubble)
                  ),
                  BottomNavyBarItem(
                      title: Text('Item Four'),
                      icon: Icon(Icons.settings)
                  ),
              ],
          ),
      );
  }
}

Troubleshooting

If you encounter any challenges while implementing the BottomNavyBar, here are some troubleshooting tips:

  • Ensure that the dependency is correctly added to your pubspec.yaml file.
  • Verify that you have more than one item in your BottomNavyBar to avoid errors.
  • If the navigation is not functioning as expected, check your onItemSelected logic to ensure the state updates correctly.

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

Conclusion

Creating a bottom navigation bar with BottomNavyBar is not just practical; it’s also a chance to enrich your app’s user interface with creativity and customization. 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