How to Implement a Persistent Bottom Navigation Bar in Flutter

Jul 1, 2022 | Programming

If you’re developing a Flutter app and want to enhance user experience with seamless navigation, a persistent bottom navigation bar might be your best bet. In this article, we’ll explore how to implement the Persistent Bottom Navigation Bar for Flutter, making your app both functional and visually appealing.

Getting Started with Persistent Bottom Navigation Bar

The first step to implementing a persistent bottom navigation bar is to add the necessary dependency to your Flutter project. Here’s how you can do it:

dependencies:
  persistent_bottom_nav_bar: any

Importing the Package

After adding the dependency, import the package in your Dart file:

import 'package:persistent_bottom_nav_bar/persistent_bottom_nav_bar.dart';

Setting Up the Persistent Tab Controller

The persistent bottom navigation bar uses a controller called PersistentTabController. Here’s how to declare it:

PersistentTabController _controller;
_controller = PersistentTabController(initialIndex: 0);

Integrating the PersistentTabView Widget

The main widget to include is the PersistentTabView. It serves as the container for the bottom navigation bar and is responsible for managing the screens and items within. There’s a lot of customization you can do here:

class MyApp extends StatelessWidget {
  const MyApp(Key key) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return PersistentTabView(
      context,
      controller: _controller,
      screens: _buildScreens(),
      items: _navBarsItems(),
      handleAndroidBackButtonPress: true,
      resizeToAvoidBottomInset: true,
      stateManagement: true,
      hideNavigationBarWhenKeyboardAppears: true,
      popBehaviorOnSelectedNavBarItemPress: PopActionScreensType.all,
      padding: const EdgeInsets.only(top: 8),
      backgroundColor: Colors.grey.shade900,
      isVisible: true,
      animationSettings: const NavBarAnimationSettings(
        navBarItemAnimation: ItemAnimationSettings(
          duration: Duration(milliseconds: 400),
          curve: Curves.ease,
        ),
        screenTransitionAnimation: ScreenTransitionAnimationSettings(
          animateTabTransition: true,
          duration: Duration(milliseconds: 200),
          screenTransitionAnimationType: ScreenTransitionAnimationType.fadeIn,
        ),
      ),
      confineToSafeArea: true,
      navBarHeight: kBottomNavigationBarHeight,
      navBarStyle: _navBarStyle,  // Choose the nav bar style here.
    );
  }
}

Unpacking the Code: An Analogy

Think of your Flutter app as a modern multi-story building. Each floor represents a different screen. The persistent bottom navigation bar acts like a responsive elevator panel located on every floor. No matter what floor (screen) you are on, the panel (navigation bar) allows you to easily select another floor without needing to take the stairs (extra navigation). The PersistentTabView is crucial here, providing the elevator to transport users between floors, complete with stylish buttons (nav items) to press.

Navigator Functions

Utilizing the navigation bar properly involves using the associated Navigator functions.

PersistentNavBarNavigator.pushNewScreen(
  context,
  screen: MainScreen(),
  withNavBar: true,
  pageTransitionAnimation: PageTransitionAnimation.cupertino,
);

Custom Navigation Styling

Want to add your unique style? Here’s how to declare a custom widget for your navigation bar:

class CustomNavBarWidget extends StatelessWidget {
  final int selectedIndex;
  final List items;
  final ValueChanged onItemSelected;

  CustomNavBarWidget(Key key, this.selectedIndex, @required this.items, this.onItemSelected);

  Widget _buildItem(PersistentBottomNavBarItem item, bool isSelected) {
    // Your Implementation
  }

  @override
  Widget build(BuildContext context) {
    // Your Implementation
  }
}

Troubleshooting Ideas

If you run into any issues during implementation, consider reviewing these troubleshooting tips:

  • Ensure that all dependencies are properly installed and updated.
  • Check if the controller is properly initialized and attached to the PersistentTabView.
  • Look out for any errors in the console that could indicate issues with your screen transitions or item properties.

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

Conclusion

By integrating a persistent bottom navigation bar in your Flutter application, you vastly improve user navigation and overall experience. With highly customizable options and support for animated icons, you can tailor it to fit the unique aesthetic and functional requirements of your app.

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