Custom Bottombar Animation In Flutter

Custom Bottombar Animation In Flutter :

Screenshot :

Custom Bottombar Animation In Flutter

bar_item.dart

import 'package:flutter/material.dart';

class AnimatedBottomBar extends StatefulWidget {
  

final List<BarItem> barItems;
  final Duration duration;
  final Function onBarTap;
  final 

BarStyle barStyle;

  AnimatedBottomBar(
      {this.barItems, this.duration, this.onBarTap, 

this.barStyle});

  @override
  _AnimatedBottomBarState createState() => 

_AnimatedBottomBarState();
}

class _AnimatedBottomBarState extends 

State<AnimatedBottomBar>
    with TickerProviderStateMixin {
  int selectedBarIndex = 0;

  

@override
  Widget build(BuildContext context) {
    return Material(
        elevation: 10,
        

child: Padding(
            padding: EdgeInsets.only(
              bottom: 5.0,
              top: 5.0,
      

        left: 5.0,
              right: 5.0,
            ),
            child: Row(
                mainAxisSize: 

MainAxisSize.max,
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
          

      children: _buildBarItem())));
  }

  List<Widget> _buildBarItem() {
    List<Widget> 

_barItems = List();
    for (int i = 0; i < widget.barItems.length; i++) {
      BarItem item = 

widget.barItems[i];
      bool isSelected = selectedBarIndex == i;
      _barItems.add(InkWell(
 

         splashColor: Colors.transparent,
          onTap: () {
            setState(() {
              

selectedBarIndex = i;
              widget.onBarTap(selectedBarIndex);
            });
          },
        

  child: AnimatedContainer(
              padding:
                  const EdgeInsets.symmetric

(horizontal: 16.0, vertical: 8.0),
              duration: widget.duration,
              decoration: 

BoxDecoration(
                  color: isSelected
                      ? item.color.withOpacity(0.2)
     

                 : Colors.transparent,
                  borderRadius: BorderRadius.all

(Radius.circular(30))),
              child: Row(children: <Widget>[
                Icon(item.icon,
    

                color: isSelected ? item.color : Colors.black,
                    size: 

widget.barStyle.iconSize),
                SizedBox(width: 10),
                AnimatedSize(
              

      duration: widget.duration,
                    curve: Curves.easeInOut,
                    vsync: 

this,
                    child: Text(isSelected ? item.title : '',
                        style: TextStyle(
          

                  color: item.color,
                            fontWeight: widget.barStyle.fontWeight,
      

                      fontSize: widget.barStyle.fontSize)))
              ]))));
    }
    return _barItems;
  

}
}

class BarStyle {
  final double fontSize, iconSize;
  final FontWeight fontWeight;

  BarStyle

(
      {this.fontSize = 18,
      this.fontWeight = FontWeight.w600,
      this.iconSize = 32});
}

class BarItem {
  final String title;
  final IconData icon;
  final Color color;
  final Widget child;

 

 BarItem({
    @required this.title,
 

bottom_bar.dart

import 'package:flutter/material.dart';
import 'bar_item.dart';

class BottomBar extends StatefulWidget {
  final List<BarItem> barItems = [
    BarItem(
        title: 'Home',
        icon: Icons.home,
        color: Colors.indigo,
        child: Container(color: Colors.indigo)),
    BarItem(
        title: 'Likes',
        icon: Icons.favorite_border,
        color: Colors.pinkAccent,
        child: Container(color: Colors.pinkAccent)),
    BarItem(
        title: 'Profile',
        icon: Icons.person_outline,
        color: Colors.teal,
        child: Container(color: Colors.teal)),
  ];
  @override
  State<StatefulWidget> createState() {
    return _BottomBarState();
  }
}

class _BottomBarState extends State<BottomBar> {
  int selectedBarIndex = 0;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: AnimatedContainer(
          duration: const Duration(milliseconds: 400),
          child: widget.barItems[selectedBarIndex].child,
        ),
        bottomNavigationBar: AnimatedBottomBar(
            barItems: widget.barItems,
            duration: const Duration(milliseconds: 150),
            barStyle: BarStyle(fontSize: 20.0, iconSize: 30.0),
            onBarTap: (index) {
              setState(() {
                selectedBarIndex = index;
              });
            }));
  }
}

main.dart

import 'package:flutter/material.dart';
import 'bottom_bar.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Flutter Tutorial',
        debugShowCheckedModeBanner: false,
        home: BottomBar());
  }
}

The flutter tutorial  is a website that bring you the latest and amazing resources of code. All the languages codes are included in this website. The languages like flutter, android, java,kotlin etc.with the help of this languages any user can develop the beautiful application

For more information about Flutter. visit www.fluttertutorial.in