title | description | author | publishedAt | tags | logoImage | |
---|---|---|---|---|---|---|
Create A Flutter Bottom Navigation Bar |
Create a flutter bottom navigation bar using the BottomNavigationBar Flutter widget that works with more than 3 items! |
benjamin-carlson.mdx |
2021-07-16T00:00:00 |
|
flutter.png |
import 'package:flutter/material.dart';
import 'package:bottom_navigation_bar/nav.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Bottom Navigation Bar Tutorial',
home: Nav(),
);
}
}
import 'package:flutter/material.dart';
import 'package:bottom_navigation_bar/home_screen.dart';
class Nav extends StatefulWidget {
@override
_NavState createState() => _NavState();
}
class _NavState extends State<Nav> {
int _selectedIndex = 0;
List<Widget> _widgetOptions = <Widget>[
Home(),
Text('Messgaes Screen'),
Text('Profile Screen'),
];
void _onItemTap(int index) {
setState(() {
_selectedIndex = index;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Bottom Navigation Bar Tutorial'),
),
body: Center(
child: _widgetOptions.elementAt(_selectedIndex),
),
bottomNavigationBar: BottomNavigationBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(
Icons.home,
),
title: Text(
'Home',
),
),
BottomNavigationBarItem(
icon: Icon(
Icons.message,
),
title: Text(
'Messages',
),
),
BottomNavigationBarItem(
icon: Icon(
Icons.person,
),
title: Text(
'Profile',
),
),
],
currentIndex: _selectedIndex,
onTap: _onItemTap,
selectedFontSize: 13.0,
unselectedFontSize: 13.0,
),
);
}
}
import 'package:flutter/material.dart';
class Home extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Icon(Icons.ac_unit),
Icon(Icons.access_alarms),
],
),
);
}
}
- Inside of
main.dart
point your home to your nav component:home: Nav(),
- Inside of
nav.dart
add the Bottom Navigation Bar code. Your nav screens go inside the_widgetOptions
list. In this example we have aHome(),
screen.
If you need more than 3 Nav bar items, you will need to add the following property to the nav bar:
...
selectedFontSize: 13.0,
unselectedFontSize: 13.0,
type: BottomNavigationBarType.fixed, // addded line
...