How to Create a Circular Bottom Navigation in Flutter

Oct 11, 2024 | Programming

Welcome to an exciting journey into the fluttering world of Flutter! Today, we will explore how to implement a delightful circular bottom navigation bar (or tab bar) in your Flutter application. This visually appealing interface provides an easy way for users to navigate through various sections of your app.

Let’s Get Started

In this section, we will go through the initial setup and steps to create your circular bottom navigation.

1 – Install and Import

Start by adding the circular_bottom_navigation package to your Flutter project.

To import it in your Dart code, you can use:

import 'package:circular_bottom_navigation/circular_bottom_navigation.dart';
import 'package:circular_bottom_navigation/tab_item.dart';

2 – CheatSheet

This cheat sheet will help you grasp the essentials of using the circular bottom navigation efficiently.

Circular Bottom Navigation CheatSheet

3 – Use it like a Charm

Now, let’s define the tab items that will appear in your circular bottom navigation.

List tabItems = List.of([
    TabItem(Icons.home, 'Home', Colors.blue, labelStyle: TextStyle(fontWeight: FontWeight.normal)),
    TabItem(Icons.search, 'Search', Colors.orange, labelStyle: TextStyle(color: Colors.red, fontWeight: FontWeight.bold)),
    TabItem(Icons.layers, 'Reports', Colors.red, circleStrokeColor: Colors.black),
    TabItem(Icons.notifications, 'Notifications', Colors.cyan),
]);

In this example, the TabItem constructor is used to create individual items, with icons, labels, and colors corresponding to different sections of your app.

4 – Integrate the Component

Now that we have our tabItems, let’s integrate it into the Circular Bottom Navigation widget:

CircularBottomNavigation(
    tabItems,
    selectedCallback: (int selectedPos) {
        print('Clicked on $selectedPos');
    },
);

As you can see, the callback function captures the index of the selected tab, allowing you to execute corresponding actions upon selection.

5 – Support for RTL Designs

If you want to enable right-to-left (RTL) support, simply wrap your widget in a Directionality widget and set the text direction:

MaterialApp(
    title: 'Circular Bottom Navigation Demo',
    theme: ThemeData(
        primarySwatch: Colors.blue,
    ),
    home: Directionality(
        textDirection: TextDirection.rtl,
        child: MyHomePage(title: 'circular_bottom_navigation'),
    ),
);

6 – Using CircularBottomNavigationController

The CircularBottomNavigationController allows you to set and read the current tab position while ensuring smooth transitions without rebuilding the widget tree:

CircularBottomNavigationController _navigationController =
    new CircularBottomNavigationController(selectedPos);

CircularBottomNavigation(
    tabItems,
    controller: _navigationController,
);

// Set a new position with animation
_navigationController.value = 0;

// Read the latest value
int latest = _navigationController.value;

Understanding Code with an Analogy

Imagine the circular bottom navigation as a merry-go-round in a playground. Each seat on the merry-go-round represents a TabItem. When a child wants to ride, they can choose any seat they like (the items), and upon getting on, they can spin around to have a 360-degree view of their surroundings (the navigation). The circular_bottom_navigation package is your playground equipment designer, ensuring it’s built sturdy, colorful, and fun for everyone!

Troubleshooting Tips

  • Ensure that you have the package dependency correctly listed in your pubspec.yaml file.
  • If the navigation doesn’t render, check for any misspellings in the import statements.
  • Make sure that your MaterialApp widget is properly set up as shown above.
  • If you encounter layout issues, verify the constraints around your navigation widget.

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

Conclusion

Now you have a nifty circular bottom navigation ready to enhance your Flutter app’s user experience! Navigate smoothly and keep your users delighted every step of the way.

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