How to create BottomNavigationBar in flutter?

This Article is posted by seven.srikanth at 28-05-2019 19:17:36

In this article, I'm going to share the code to create a ButtonNavigationBar as shown in below image.



Below is the code to create this App. All you need to do is to copy this code into your main.dart file in your flutter project.

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
static const String _title = 'Flutter Code Sample';

@override
Widget build(BuildContext context) {
return MaterialApp(
title: _title,
home: MyStatefulWidget(),
);
}
}

class MyStatefulWidget extends StatefulWidget {
MyStatefulWidget({Key key}) : super(key: key);

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

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
int _selectedIndex = 0;
static const double IconSize = 200;
static List<Widget> _widgetOptions = <Widget>[
HomePage(),
BusinessPage(),
SchoolPage(),
];

void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('BottomNavigationBar Sample'),
),
body: Center(
child: _widgetOptions.elementAt(_selectedIndex),
),
bottomNavigationBar: BottomNavigationBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
title: Text('Home'),
),
BottomNavigationBarItem(
icon: Icon(Icons.business),
title: Text('Business'),
),
BottomNavigationBarItem(
icon: Icon(Icons.school),
title: Text('School'),
),
],
currentIndex: _selectedIndex,
selectedItemColor: Colors.amber[800],
showUnselectedLabels: false,
onTap: _onItemTapped,
),
);
}
}

class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Icon(
Icons.home, size: 100
);
}
}

class BusinessPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Icon(
Icons.business, size: 100
);
}
}

class SchoolPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Icon(
Icons.school, size: 100
);
}
}

Hope this example is useful to you. 

Thanks,
Srikanth

Tags: BottomNavigationBar



0 Comments
Login to comment.
Recent Comments

Be the first to Comment. You can ask a Query or Share your toughts or Just say thanks.



Recent Articles


How to deploy flutter web site on GitHub using peanut

Posted by nikhilchaudhary12155 on 13 Oct 2019 Article


AnimatedPositioned Widget Example in Flutter

Posted by seven.srikanth on 10 Oct 2019 Article


Flutter Open-Source and directory lists

Posted by nishantkotatech on 09 Oct 2019 Article


Flutter Travelling Couple Leading Page - Flutter Web Series 3

Posted by nikhilchaudhary12155 on 05 Oct 2019 Article



© 2018 - Fluttercentral | Email to me - Seven.srikanth@gmail.com