AnimatedBottomNavigationBar

repository·master·Indexed 19 days ago

https://github.com/lanarsinc/animated-bottom-navigation-bar-flutter

A customizable Flutter widget providing animated tab bar transitions inspired by Dribbble designs. It supports notched layouts for FloatingActionButtons, customizable corner radii, and various notch smoothness levels. The library offers a standard constructor for IconData and a .builder constructor for custom tab views, supporting between 2 and 5 elements.

Tokens
3.8K
Snippets
10
Records
16
Agent score
66%

What's inside animated-bottom-navigation-bar-flutter

  1. Customize the notch and corner appearance

    master

    The navigation bar supports 2, 3, 4, or 5 elements and offers several customization properties to match your UI:

    • gapLocation: Determines where the notch is placed relative to the FloatingActionButton. Options include GapLocation.center and GapLocation.end.
    • notchSmoothness: Controls the visual style of the notch. Available values include:
      • NotchSmoothness.defaultEdge
      • NotchSmoothness.softEdge
      • NotchSmoothness.smoothEdge
      • NotchSmoothness.verySmoothEdge
    • leftCornerRadius / rightCornerRadius: Sets the rounding of the bar's outer corners.
    • icons: A list of icons used when using the standard AnimatedBottomNavigationBar constructor.
  2. Customize iOS Launch Screen Assets

    master

    To change the launch screen image for the iOS version of the application, you can either replace the image files directly in the example/ios/Runner/Assets.xcassets/LaunchImage.imageset/ directory or use Xcode for a more visual approach.

    Using Xcode:

    1. Open the iOS project in Xcode by running open ios/Runner.xcworkspace from your terminal.
    2. In the Xcode Project Navigator, navigate to Runner/Assets.xcassets.
    3. Drag and drop your desired images into the asset catalog to replace the existing launch screen assets.
    open ios/Runner.xcworkspace
  3. Drive navigation bar changes programmatically

    master

    To change the active tab, you must update the activeIndex property passed to the AnimatedBottomNavigationBar. This is typically done by calling setState() in a StatefulWidget, which triggers a re-render and allows the widget to run its built-in animations to the new index.

    class _MyAppState extends State<MyApp> {
      int activeIndex;
    
      /// Handler for when you want to programmatically change
      /// the active index. Calling `setState()` here causes
      /// Flutter to re-render the tree, which `AnimatedBottomNavigationBar`
      /// responds to by running its normal animation.
      void _onTap(int index) {
        setState((){
          activeIndex = index;
        });
      }
    
      Widget build(BuildContext context) {
        return AnimatedBottomNavigationBar(
          activeIndex: activeIndex,
          onTap: _onTap,
          // other params
        );
      }
    }
  4. Integrate AnimatedBottomNavigationBar into a Scaffold

    master

    To use the navigation bar, place either AnimatedBottomNavigationBar or AnimatedBottomNavigationBar.builder in the bottomNavigationBar slot of a Flutter Scaffold. The widget is designed to respect the FloatingActionButtonLocation (e.g., FloatingActionButtonLocation.centerDocked) to create notched effects.

    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
       ),
    );
  5. Animate the Notch and Corners

    master

    The AnimatedBottomNavigationBar supports custom animations for its notch and corner transitions. To achieve this, pass an Animation<double> to the notchAndCornersAnimation property. This is typically driven by an AnimationController within your StatefulWidget.

    // Inside your State class
    late AnimationController _animationController;
    late Animation<double> animation;
    
    @override
    void initState() {
      super.initState();
      _animationController = AnimationController(
        duration: Duration(seconds: 1),
        vsync: this,
      );
      // Define the animation curve and interval
      animation = Tween<double>(begin: 0, end: 1).animate(
        CurvedAnimation(parent: _animationController, curve: Curves.fastOutSlowIn),
      );
    }
    
    // In build method
    AnimatedBottomNavigationBar(
      // ... other properties
      notchAndCornersAnimation: animation,
    )
  6. Use AnimatedBottomNavigationBar.builder for custom tab views

    master

    If you need full control over how each tab is rendered, use the .builder constructor. This approach requires you to handle the active/inactive state of the tabs manually within the tabBuilder callback. The tabBuilder provides the current index and a bool isActive indicating if the tab is currently selected.

    Scaffold(
       body: Container(), // destination screen
       floatingActionButton: FloatingActionButton(
          // params
       ),
       floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
       bottomNavigationBar: AnimatedBottomNavigationBar.builder(
          itemCount: iconList.length,
          tabBuilder: (int index, bool isActive) {
            return 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
       ),
    );
  7. Customize appearance and animations

    master

    You can customize the visual style and animation behavior of the bar using the following properties:

    Colors and Styling:

    • backgroundColor: Background color of the bar. Default is Colors.white.
    • backgroundGradient: A Gradient for the background. If set, backgroundColor is ignored.
    • activeColor: Color of the selected icon. Default is Colors.deepPurpleAccent.
    • inactiveColor: Color of unselected icons. Default is Colors.black.
    • splashColor: Color of the selection animation splash. Default is Colors.purple.
    • borderColor / borderWidth: Custom border around the bar.
    • elevation / shadow: Controls the shadow and depth of the bar.

    Animations:

    • splashRadius: The maximum radius of the selection animation. Default is 24.
    • splashSpeedInMilliseconds: Speed of the splash animation. Default is 300.
    • scaleFactor: Scale effect factor for icons during animation. Default is 1.0.
    • hideAnimationController: An optional AnimationController to control the visibility (hide/show) of the bar.
    • hideAnimationCurve: The Curve used for the hide animation. Default is Curves.fastOutSlowIn.
  8. Enable blur effect on the navigation bar

    master

    To apply a blur effect to the navigation bar (useful when the background has transparency), set blurEffect to true and provide a blurFilter.

    If blurFilter is not provided, it defaults to ImageFilter.blur(sigmaX: 5, sigmaY: 10).

    AnimatedBottomNavigationBar(
      icons: [Icons.home, Icons.search, Icons.person],
      activeIndex: _currentIndex,
      onTap: (index) => setState(() => _currentIndex = index),
      backgroundColor: Colors.white.withOpacity(0.5),
      blurEffect: true,
      blurFilter: ImageFilter.blur(sigmaX: 10, sigmaY: 10),
    )
  9. Configure notch and gap settings

    master

    The navigation bar supports a notch (gap) to accommodate a FloatingActionButton.

    Key properties:

    • gapLocation: Determines where the gap is placed. Options are GapLocation.none, GapLocation.center, or GapLocation.end.
    • gapWidth: The width of the free space between items. Default is 72. For best results, set this to the width of your FloatingActionButton plus double the notchMargin.
    • notchMargin: The margin around the notch. Default is 8.
    • notchSmoothness: Controls the appearance of the notch edges. Options are NotchSmoothness.sharpEdge, NotchSmoothness.defaultEdge, NotchSmoothness.softEdge, NotchSmoothness.smoothEdge, and NotchSmoothness.verySmoothEdge.
    • notchAndCornersAnimation: An optional Animation<double> to animate the appearance of the notch and corners.
    AnimatedBottomNavigationBar(
      icons: [Icons.home, Icons.search, Icons.person],
      activeIndex: _currentIndex,
      onTap: (index) => setState(() => _currentIndex = index),
      gapLocation: GapLocation.center,
      gapWidth: 72.0,
      notchSmoothness: NotchSmoothness.smoothEdge,
    )
  10. Implement AnimatedBottomNavigationBar

    master

    To use the AnimatedBottomNavigationBar, include it in the bottomNavigationBar property of a Scaffold. You must provide a list of icons and manage the active index state manually via the onTap callback.

    Key properties:

    • icons: A List<IconData> representing the navigation items.
    • activeIndex: The currently selected index.
    • onTap: A callback function void Function(int) that returns the index of the tapped item. Use this to update your state.
    • backgroundColor: The color of the bar.
    • activeColor: The color of the active icon.
    • inactiveColor: The color of the inactive icons.
    • splashColor: The color of the splash effect when an icon is tapped.
    • notchAndCornersAnimation: An Animation<double> used to animate the notch and corner transitions.
    • notchSmoothness: Controls the smoothness of the notch (e.g., NotchSmoothness.defaultEdge).
    • gapLocation: Determines where the gap for the FloatingActionButton is located (e.g., GapLocation.center).
    • leftCornerRadius / rightCornerRadius: Controls the rounding of the bar's corners.
    AnimatedBottomNavigationBar(
      icons: iconList,
      backgroundColor: HexColor('#373A36'),
      activeIndex: _bottomNavIndex,
      activeColor: HexColor('#FFA400'),
      splashColor: HexColor('#FFA400'),
      inactiveColor: Colors.white,
      notchAndCornersAnimation: animation,
      splashSpeedInMilliseconds: 300,
      notchSmoothness: NotchSmoothness.defaultEdge,
      gapLocation: GapLocation.center,
      leftCornerRadius: 32,
      rightCornerRadius: 32,
      onTap: (index) => setState(() => _bottomNavIndex = index),
    )
  11. Use AnimatedBottomNavigationBar.builder for custom widgets

    master

    Use the .builder constructor when you want to render custom widgets instead of simple icons for each tab. You must provide an itemCount and a tabBuilder function.

    The tabBuilder signature: Widget Function(int index, bool isActive)

    • index: The index of the current tab.
    • isActive: A boolean indicating if this tab is currently selected.
    AnimatedBottomNavigationBar.builder(
      itemCount: 3,
      activeIndex: _currentIndex,
      onTap: (index) => setState(() => _currentIndex = index),
      tabBuilder: (index, isActive) {
        return MyCustomTabWidget(
          icon: _myIcons[index],
          isActive: isActive,
        );
      },
    )