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(),
);
}
}
can change by setting colors to backgroundColor property if type is fixed.
If the type is shifting it will use color inside bottomNavigationBarItem.
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
Try wrapping your
BottomNavigationBar
in aContainer
then set itscolor
.Example:
Earlier there was no direct way to set the colors, but now you can use.
Simply add the
backgroundColor
property toBottomNavigationBar
widget.The accepted answer isn't entirely wrong. However,
BottomNavigationBar
does in-fact have a property ofbackgroundColor
. As per the documentationWhat this means is that the
BottomNavigation
's backgroundColor will be overriden by the individual items backgroundColor because the default type isBottomNavigationBarType.shifting
.To fix this, simply add the following property to the declared
BottomNavigationbar
widget.Note: If you do, however, want the shifting effect you'll have to declare colors for each item, or wrap the widget that allows the overriding of the child widget(s) background color.
i.e Somethign like
Container
widget.There is no option to specify the background color of
BottomNavigationBar
but to change thecanvasColor
. One way you can achieve it without messing up the whole app would be by wrappingBottomNavigationBar
in aTheme
with desiredcanvasColor
.Example:
Hope that helps!