Fancybar In Flutter

Fancybar In Flutter :

Screenshot :

fancy_bar.dart

library fancy_bar;
import 'package:flutter/material.dart';

class FancyBottomBar extends StatefulWidget {
  final List<FancyItem> items;
  final selectedIndex;
  final ValueChanged<int> onItemSelected;

  FancyBottomBar({
    this.items,
    this.selectedIndex,
    @required this.onItemSelected,
  }) {
    assert(items != null);
    assert(items.length >= 2 && items.length <= 4);
    assert(onItemSelected != null);
  }

  @override
  _FancyBottomBarState createState() => _FancyBottomBarState();
}

class _FancyBottomBarState extends State<FancyBottomBar> {
  int pos = 0;

  void setItem(int _pos) {
    setState(() {
      pos = _pos;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      child: BottomAppBar(
//        shape: CircularNotchedRectangle(),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: widget.items.map((_item) {
            FancyItem item = _item;
            var index = widget.items.indexOf(_item);
            return InkWell(
              onTap: () {
                setItem(index);
                widget.onItemSelected(index);
              },
              child: _ItemVWidget(
                item: item,
                isSelected: index == pos,
                length: widget.items.length,
              ),
            );
          }).toList(),
        ),
      ),
    );
  }
}

class _ItemVWidget extends StatelessWidget {
  final FancyItem item;
  final bool isSelected;
  final int length;

  const _ItemVWidget({Key key, this.item, this.isSelected, this.length})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
        width: MediaQuery.of(context).size.width / length,
        height: kBottomNavigationBarHeight,
        alignment: Alignment.center,
        child: AnimatedSwitcher(
          transitionBuilder: (Widget child, Animation<double> animation) {
            return ScaleTransition(
              scale: animation,
              child: child,
            );
          },
          duration: Duration(milliseconds: 250),
          child: isSelected
              ? Text(
                  '${item.title}',
                  style: TextStyle(
                      color: item.textColor ?? Theme.of(context).primaryColor,
                      fontWeight: FontWeight.bold,
                      fontSize: 16),
                )
              : item.icon,
        ));
  }
}

class FancyItem {
  Color textColor;
  String title;
  Widget icon;

  FancyItem({
    @required this.textColor,
    @required this.title,
    @required this.icon,
  });
}

main.dart

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: Text(widget.title),
      ),
      bottomNavigationBar: FancyBottomBar(
        items: [
          FancyItem(
            textColor: Colors.orange,
            title: 'Home',
            icon: Icon(Icons.home),
          ),
          FancyItem(
            textColor: Colors.red,
            title: 'Trending',
            icon: Icon(Icons.trending_up),
          ),
          FancyItem(
            textColor: Colors.green,
            title: 'Search',
            icon: Icon(Icons.search),
          ),
          FancyItem(
            textColor: Colors.brown,
            title: 'Settings',
            icon: Icon(Icons.settings),
          ),
        ],
        onItemSelected: (index) {
          print(index);
        },
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

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