'Style BottomNavigationBar in Flutter
I am trying out Flutter and I am trying to change the colour of the BottomNavigationBar
on the app but all I could achieve was change the colour of the BottomNavigationItem
(icon and text).
Here is where i declare my BottomNavigationBar
:
class _BottomNavigationState extends State<BottomNavigationHolder>{
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: null,
body: pages(),
bottomNavigationBar:new BottomNavigationBar(
items: <BottomNavigationBarItem>[
new BottomNavigationBarItem(
icon: const Icon(Icons.home),
title: new Text("Home")
),
new BottomNavigationBarItem(
icon: const Icon(Icons.work),
title: new Text("Self Help")
),
new BottomNavigationBarItem(
icon: const Icon(Icons.face),
title: new Text("Profile")
)
],
currentIndex: index,
onTap: (int i){setState((){index = i;});},
fixedColor: Colors.white,
),
);
}
Earlier I thought I had it figured out by editing canvasColor
to green on my main app theme but it messed up the entire app colour scheme:
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
canvasColor: Colors.green
),
home: new FirstScreen(),
);
}
}
Solution 1:[1]
There is no option to specify the background color of BottomNavigationBar
but to change the canvasColor
. One way you can achieve it without messing up the whole app would be by wrapping BottomNavigationBar
in a Theme
with desired canvasColor
.
Example:
bottomNavigationBar: new Theme(
data: Theme.of(context).copyWith(
// sets the background color of the `BottomNavigationBar`
canvasColor: Colors.green,
// sets the active color of the `BottomNavigationBar` if `Brightness` is light
primaryColor: Colors.red,
textTheme: Theme
.of(context)
.textTheme
.copyWith(caption: new TextStyle(color: Colors.yellow))), // sets the inactive color of the `BottomNavigationBar`
child: new BottomNavigationBar(
type: BottomNavigationBarType.fixed,
currentIndex: 0,
items: [
new BottomNavigationBarItem(
icon: new Icon(Icons.add),
title: new Text("Add"),
),
new BottomNavigationBarItem(
icon: new Icon(Icons.delete),
title: new Text("Delete"),
)
],
),
),
Hope that helps!
Solution 2:[2]
BottomNavigationBar
could be either fixed or moving (shifting).
It is fixed if there are 3 items and changes to shifting for 4 or more items. We can override this behavior by setting BottomNavigationBar.type
parameter.
Fixed
BottomNavigationBar
BottomNavigationBar( type: BottomNavigationBarType.fixed, // Fixed backgroundColor: Colors.black, // <-- This works for fixed selectedItemColor: Colors.greenAccent, unselectedItemColor: Colors.grey, items: [ BottomNavigationBarItem( icon: Icon(Icons.call), label: 'Call', ), BottomNavigationBarItem( icon: Icon(Icons.message), label: 'Message', ), ], )
Shifting
BottomNavigationBar
:BottomNavigationBar( type: BottomNavigationBarType.shifting, // Shifting selectedItemColor: Colors.white, unselectedItemColor: Colors.grey, items: [ BottomNavigationBarItem( icon: Icon(Icons.call), label: 'Call', backgroundColor: Colors.blue, // <-- This works for shifting ), BottomNavigationBarItem( icon: Icon(Icons.message), label: 'Message', backgroundColor: Colors.green, // <-- This works for shifting ), ], )
Solution 3:[3]
The accepted answer isn't entirely wrong. However, BottomNavigationBar
does in-fact have a property of backgroundColor
. As per the documentation
If type is BottomNavigationBarType.shifting and the itemss, have BottomNavigationBarItem.backgroundColor set, the item's backgroundColor will splash and overwrite this color.
What this means is that the BottomNavigation
's backgroundColor will be overriden by the individual items backgroundColor because the default type is BottomNavigationBarType.shifting
.
To fix this, simply add the following property to the declared BottomNavigationbar
widget.
type: BottomNavigationBarType.fixed,
Note: If you do, however, want the shifting effect you will have to declare colors for each item, or wrap the widget that allows the overriding of the child widget(s) background color.
i.e Something like Container
widget.
Solution 4:[4]
can change by setting colors to backgroundColor property if type is fixed.
BottomNavigationBar(
backgroundColor: Colors.red,
type: BottomNavigationBarType.fixed,
items: [
BottomNavigationBarItem(
icon:Icon(Icons.home, color: Color.fromARGB(255, 255, 255, 255)),
title: new Text('Home'),),
BottomNavigationBarItem(
icon: Icon(Icons.work,color: Color.fromARGB(255, 255, 255, 255)),
title: new Text('Self Help'),),
BottomNavigationBarItem(
icon:Icon(Icons.face, color: Color.fromARGB(255, 255, 255, 255)),
title: new Text('Profile'),),
]
)
If the type is shifting it will use color inside bottomNavigationBarItem.
BottomNavigationBar(
backgroundColor: Colors.red,
type: BottomNavigationBarType.shifting,
items: [
BottomNavigationBarItem(
icon:Icon(Icons.home, color: Color.fromARGB(255, 255, 255, 255)),
title: new Text('Home'),
backgroundColor: Colors.red),
BottomNavigationBarItem(
icon: Icon(Icons.work,color: Color.fromARGB(255, 255, 255, 255)),
title: new Text('Self Help'),
backgroundColor: Colors.blue),
BottomNavigationBarItem(
icon:Icon(Icons.face, color: Color.fromARGB(255, 255, 255, 255)),
title: new Text('Profile'),
backgroundColor: Colors.amber),
]
)
You can see even though I have set backgroundColor property it does not apply that colors and the background color inside BottomNavigationBarItem widget will override that.
Found from here
Solution 5:[5]
Set following properties to change the background, selected and unselected colors
bottomNavigationBar: BottomNavigationBar(
backgroundColor: Colors.blue,
selectedItemColor: Colors.black,
unselectedItemColor: Colors.white,
type: BottomNavigationBarType.fixed,
...
)
Solution 6:[6]
You can currently style them BottomNavigationBar
directly from the Theme
, like this:
ThemeData(
bottomNavigationBarTheme: BottomNavigationBarThemeData(
backgroundColor: Colors.grey[900],
elevation: 10,
selectedLabelStyle: TextStyle(
color: Color(0xFFA67926), fontFamily: 'Montserrat', fontSize: 14.0
),
unselectedLabelStyle: TextStyle(
color: Colors.grey[600], fontFamily: 'Montserrat', fontSize: 12.0
),
selectedItemColor: Color(0xFFA67926),
unselectedItemColor: Colors.grey[600],
showUnselectedLabels: true,
),
)
Solution 7:[7]
The title
is deprecated. We use label
instead.
For label
, we can use corresponding attributes: selectedLabelStyle, unselectedLabelStyle
.
For example:
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
selectedItemColor: Theme.of(context).accentColor,
selectedFontSize: 0,
unselectedFontSize: 0,
iconSize: 22,
elevation: 0,
backgroundColor: Colors.transparent,
selectedIconTheme: IconThemeData(size: 28),
unselectedItemColor: Theme.of(context).focusColor.withOpacity(1),
selectedLabelStyle: Theme.of(context).textTheme.bodyText1.merge(TextStyle(fontSize: 12)),
unselectedLabelStyle: Theme.of(context).textTheme.button.merge(TextStyle(fontSize: 11)),
showUnselectedLabels: true,
currentIndex: widget.currentTabIdx,
onTap: (int i) {
this._selectTab(i);
},
showSelectedLabels: true,
// this will be set when a new tab is tapped
items: [
BottomNavigationBarItem(
icon: SvgPicture.asset(IMAGE_ASSETS_ICONS_HOME) ,
activeIcon: SvgPicture.asset(IMAGE_ASSETS_ICONS_HOME, color: Theme.of(context).accentColor),
label: 'Home',
),
BottomNavigationBarItem(
label: 'Categories',
icon: SvgPicture.asset(IMAGE_ASSETS_ICONS_CATEGORY),
activeIcon: SvgPicture.asset(IMAGE_ASSETS_ICONS_CATEGORY, color: Theme.of(context).accentColor) ,
),
BottomNavigationBarItem(
icon: SvgPicture.asset(IMAGE_ASSETS_ICONS_ORDER_HISTORY, ) ,
activeIcon: SvgPicture.asset(IMAGE_ASSETS_ICONS_ORDER_HISTORY, color: Theme.of(context).accentColor ) ,
label: 'Order History',
),
BottomNavigationBarItem(
icon: SvgPicture.asset(IMAGE_ASSETS_ICONS_CART,) ,
activeIcon: SvgPicture.asset(IMAGE_ASSETS_ICONS_CART, color: Theme.of(context).accentColor) ,
label: 'Cart',
),
],
Solution 8:[8]
Try wrapping your BottomNavigationBar
in a Container
then set its color
.
Example:
@override
Widget build(BuildContext context) {
return Scaffold(
body: pages(),
bottomNavigationBar:new Container(
color: Colors.green,
child: BottomNavigationBar(
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: const Icon(Icons.home),
title: Text("Home")
),
BottomNavigationBarItem(
icon: const Icon(Icons.work),
title: Text("Self Help")
),
BottomNavigationBarItem(
icon: const Icon(Icons.face),
title: Text("Profile")
)
],
currentIndex: index,
onTap: (int i){setState((){index = i;});},
fixedColor: Colors.white,
),
);
);
};
Solution 9:[9]
Just follow the given below code to customize according to your requirements. You just need to set the parent of NavigationBar with Theme and set canvasColor to change the background color
bottomNavigationBar: Theme(
data: Theme.of(context).copyWith(
canvasColor: kOrangeMaterialColor
),
child: BottomNavigationBar(
type: BottomNavigationBarType.shifting,
currentIndex: _currentIndex,
onTap: _onTapItem,
items: [
BottomNavigationBarItem(icon: Icon(Icons.home,
color: kWhiteColor,),
label: ''),
BottomNavigationBarItem(icon: Icon(Icons.notifications,
color: kWhiteColor,),
label: ''),
// BottomNavigationBarItem(icon: Icon(Icons.favorite_border,
// color: kWhiteColor,),
// label: ''),
BottomNavigationBarItem(icon: Icon(Icons.account_circle,
color: kWhiteColor,),
label: ''),
BottomNavigationBarItem(icon: Icon(Icons.settings,
color: kWhiteColor,),
label: ''),
],
),
),
Solution 10:[10]
You can use this code :
BottomNavigationBar (
backgroundColor: Colors.red,
type: BottomNavigationBarType.fixed
)
Or warp BottomNavigation
with Theme
widget and change canvasColor
.
Theme(
data: Theme.of(context).copyWith(canvasColor: Colors.green),
child: BottomNavigationBar(
// add your code ...
)
],
),
),
Solution 11:[11]
Simply add the backgroundColor
property to BottomNavigationBar
widget.
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: null,
body: pages(),
bottomNavigationBar:new BottomNavigationBar(
items: <BottomNavigationBarItem>[
new BottomNavigationBarItem(
icon: const Icon(Icons.home),
title: new Text("Home")
),
new BottomNavigationBarItem(
icon: const Icon(Icons.work),
title: new Text("Self Help")
),
new BottomNavigationBarItem(
icon: const Icon(Icons.face),
title: new Text("Profile")
)
],
currentIndex: index,
onTap: (int i){setState((){index = i;});},
fixedColor: Colors.white,
backgroundColor: Colors.black45,
),
);
}
Solution 12:[12]
use
BottomNavigationBar (
backgroundColor: Colors.red,
)
If not changing color with this wrap with material widget.
Material(
child:
BottomNavigationBar (
backgroundColor: Colors.red,
),
)
Solution 13:[13]
// it will work like this backgound color
import 'package:flutter/material.dart';
import 'package:pedometer/widgets/const.dart';
import 'package:pedometer/widgets/const.dart';
import 'package:pedometer/widgets/const.dart';
import 'package:pedometer/widgets/icon.dart';
import 'dashbord.dart';
class MyStatefulWidget extends StatefulWidget {
const MyStatefulWidget({Key? key}) : super(key: key);
@override
State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
int _selectedIndex = 0;
static const TextStyle optionStyle =
TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
static const List<Widget> _widgetOptions = <Widget>[
DashbordScreen(),
DashbordScreen(),
DashbordScreen(),
DashbordScreen(),
];
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: _widgetOptions.elementAt(_selectedIndex),
),
bottomNavigationBar: BottomNavigationBar(
backgroundColor: const Color.fromARGB(255, 6, 17, 93),
type: BottomNavigationBarType.fixed,
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
// iconFun(path:Icons.home,context: context )
icon: Icon(Icons.home,color: Colors.white,size: 35,),
label: 'Home',
// backgroundColor: Colors.red,
),
BottomNavigationBarItem(
icon: Icon(Icons.auto_graph_outlined,color: Colors.white,size: 35),
label: 'Business',
backgroundColor: Colors.green,
),
BottomNavigationBarItem(
icon: Icon(Icons.health_and_safety,color: Colors.white,size: 35),
label: 'School',
// backgroundColor: Colors.purple,
),
BottomNavigationBarItem(
icon: Icon(Icons.settings,color: Colors.white,size: 35),
label: 'Settings',
// backgroundColor: Colors.pink,
),
],
currentIndex: _selectedIndex,
selectedItemColor: Colors.white,
onTap: _onItemTapped,
),
);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow