How to create BottomNavigationBar in flutter?

This Article is posted by seven.srikanth at 5/28/2019 7:17:36 PM



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; flutter bottom navigation bar;








0 Comments
Login to comment.
Recent Comments












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