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(),
);
}
}
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"),
)
],
),
),
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
),
],
)
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.
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
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,
...
)
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,
),
)
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',
),
],
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,
),
);
);
};
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: ''),
],
),
),
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 ...
)
],
),
),
Simply add the backgroundColor property to BottomNavigationBarwidget.
#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,
),
);
}
// 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,
),
);
}
}
use
BottomNavigationBar (
backgroundColor: Colors.red,
)
If not changing color with this wrap with material widget.
Material(
child:
BottomNavigationBar (
backgroundColor: Colors.red,
),
)
Related
I need to change color of selected BottomNavigationBarItem to yellow, but it doesn't work.
It seems because i set BottomNavigationBarItem default colors as black, but otherwise they will be set as white, cause i created more than 3 items, and i don't know hot to fix them either
If you know how to fix this or white color bug, please inform me
body: Center(
child: _widgetopt.elementAt(_selind),
),
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
selectedItemColor: Colors.redAccent,
backgroundColor: Colors.white,
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(
Icons.add_box_outlined,
color: Colors.black,
),
label: ''),
BottomNavigationBarItem(
icon: Icon(
Icons.location_on_outlined,
color: Colors.black,
),
label: ''),
BottomNavigationBarItem(
icon: Icon(
Icons.heart_broken_outlined,
color: Colors.black,
),
label: ''),
BottomNavigationBarItem(
icon: Icon(
Icons.person_outline_outlined,
color: Colors.black,
),
label: ''),
],
currentIndex: _selind,
onTap: OnBeingTapped,
),
),
title: 'Stadium',
);
}
}
Rest of code:
class _HomePageState extends State<HomePage> {
int _selind = 0;
List<Widget> _widgetopt = <Widget>[
Text('Index 1'),
Text('Index 2'),
Text('Index 3'),
Text('Index 4'),
];
void OnBeingTapped(int index) {
setState(() {
_selind = index;
});
}
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text(
'Стадионы',
style: TextStyle(color: Colors.black),
),
backgroundColor: Colors.white,
toolbarHeight: 100,
actions: [
IconButton(
onPressed: () {},
icon: Icon(
Icons.menu_outlined,
color: Colors.black,
)),
IconButton(
onPressed: () {},
icon: Icon(
Icons.text_rotation_down_outlined,
color: Colors.black,
)),
],
),
You forgot to add selectedItemColor property of BottomNavigationBarItem widget. Just add below line in your code and you'll be able to see selected item color yellow from black.
currentIndex: _selind,
selectedItemColor: Colors.yellow, // Add this line :)
onTap: OnBeingTapped,
I have 2 different BottomNavigationBar's defined within my app. One for authenticated users and another for unauthenticated users.
Listed below are the variables relevant to the BottomNavigationBar:
int selectedIndex = 0;
final authenticatedScreens = [
HomePage(),
SearchPage(),
CoursesPage(),
ProfilePage(),
];
final unAuthenticatedScreens = [
HomePage(),
SearchPage(),
CoursesPage(),
SignIn(),
];
Listed below is the code present within the build method:
.
.
.
return user == null
? Container(
child: Scaffold(
bottomNavigationBar: buildUnAuthenticatedBottomNavigationBar(context),
body: SafeArea(child: unAuthenticatedScreens[selectedIndex]),
),
)
: Container(
child: Scaffold(
bottomNavigationBar: buildAuthenticatedBottomNavigationBar(context),
body: SafeArea(child: authenticatedScreens[selectedIndex]),
),
);
.
.
.
Listed below is the code for authenticated and unauthenticated BottomNavigationBar's:
BottomNavigationBar buildAuthenticatedBottomNavigationBar(BuildContext context) {
return BottomNavigationBar(
currentIndex: selectedIndex,
onTap: (index) => setState(() => selectedIndex = index),
type: BottomNavigationBarType.fixed,
selectedLabelStyle: TextStyle(
fontSize: 8.sp,
fontWeight: FontWeight.w600,
color: Theme.of(context).iconTheme.color,
),
unselectedLabelStyle: TextStyle(
fontSize: 8.sp,
fontWeight: FontWeight.w400,
color: Theme.of(context).iconTheme.color!.withOpacity(0.4),
),
showSelectedLabels: true,
showUnselectedLabels: true,
selectedItemColor: Theme.of(context).iconTheme.color,
unselectedItemColor: Theme.of(context).iconTheme.color!.withOpacity(0.4),
items: [
BottomNavigationBarItem(
label: "Home",
icon: Icon(
Icons.home,
),
),
BottomNavigationBarItem(
label: "Search",
icon: Icon(
Icons.search,
),
),
BottomNavigationBarItem(
label: "Courses",
icon: Icon(
Icons.auto_stories_outlined,
),
activeIcon: Icon(
Icons.auto_stories,
),
),
BottomNavigationBarItem(
label: "Account",
icon: Icon(
Icons.person,
),
),
],
);
}
BottomNavigationBar buildUnAuthenticatedBottomNavigationBar(BuildContext context) {
return BottomNavigationBar(
currentIndex: selectedIndex,
onTap: (index) => setState(() => selectedIndex = index),
type: BottomNavigationBarType.fixed,
selectedLabelStyle: TextStyle(
fontSize: 8.sp,
fontWeight: FontWeight.w600,
color: Theme.of(context).iconTheme.color,
),
unselectedLabelStyle: TextStyle(
fontSize: 8.sp,
fontWeight: FontWeight.w400,
color: Theme.of(context).iconTheme.color!.withOpacity(0.4),
),
showSelectedLabels: true,
showUnselectedLabels: true,
selectedItemColor: Theme.of(context).iconTheme.color,
unselectedItemColor: Theme.of(context).iconTheme.color!.withOpacity(0.4),
items: [
BottomNavigationBarItem(
label: "Home",
icon: Icon(
Icons.home,
),
),
BottomNavigationBarItem(
label: "Search",
icon: Icon(
Icons.search,
),
),
BottomNavigationBarItem(
label: "Courses",
icon: Icon(
Icons.auto_stories_outlined,
),
activeIcon: Icon(
Icons.auto_stories,
),
),
BottomNavigationBarItem(
label: "Account",
icon: Icon(
Icons.person,
),
),
],
);
}
When I navigate to another screen from any of the 4 screens which are part of the BottomNavigationBar. The BottomNavigationBar disappears, I don't want the BottomNavigationBar to disappear. I want the BottomNavigationBar to remain persistent for certain screens and disappear for certain screens.
I did explore few solutions but didn't work out for me. I wouldn't like to make use of the Cupertino widgets.
How can I fix this?
With using this package Presistant_bottom_nav_bar.so you can use bottomnavbar on every screen or you can do the above method
PersistentTabController _controller =PersistentTabController(initialIndex: 1);
//Screens for each nav items.
List<Widget> _NavScreens() {
return [
OfferScreen(),
HelpScreen(),
HomeScreen(),
ProfileScreen(),
CartViewScreen(),
];
}
List<PersistentBottomNavBarItem> _navBarsItems() {
return [
PersistentBottomNavBarItem(
icon: Icon(Icons.home),
title: ("Home"),
activeColor: CupertinoColors.activeBlue,
inactiveColor: CupertinoColors.systemGrey,
),
PersistentBottomNavBarItem(
icon: Icon(Icons.favorite),
title: ("OFFERS"),
activeColor: CupertinoColors.activeGreen,
inactiveColor: CupertinoColors.systemGrey,
),
PersistentBottomNavBarItem(
icon: Icon(Icons.person_pin),
title: ("Help"),
activeColor: CupertinoColors.systemRed,
inactiveColor: CupertinoColors.systemGrey,
),
PersistentBottomNavBarItem(
icon: Icon(Icons.local_activity),
title: ("ProfileScreen"),
activeColor: CupertinoColors.systemIndigo,
inactiveColor: CupertinoColors.systemGrey,
),
PersistentBottomNavBarItem(
icon: Icon(Icons.shop_cart),
title: ("Cart"),
activeColor: CupertinoColors.systemIndigo,
inactiveColor: CupertinoColors.systemGrey,
),
];
}
#override
Widget build(BuildContext context) {
return Center(
child: PersistentTabView(
controller: _controller,
screens: _NavScreens(),
items: _navBarsItems(),
confineInSafeArea: true,
backgroundColor: Colors.white,
handleAndroidBackButtonPress: true,
resizeToAvoidBottomInset: true,
hideNavigationBarWhenKeyboardShows: true,
decoration: NavBarDecoration(
borderRadius: BorderRadius.circular(10.0),
),
popAllScreensOnTapOfSelectedTab: true,
navBarStyle: NavBarStyle.style9,
),
);
}
Also you can use navigation bottombar in certain screen by using navigator-functions instead of flutter navigator.push(); use following pushNewScreen
for more details checkout persistent_bottom_nav_bar#navigator-functions
pushNewScreen(
context,
screen: MainScreen(),
withNavBar: false, // OPTIONAL VALUE. True by default.
pageTransitionAnimation: PageTransitionAnimation.cupertino,
);
Yes I agree there are some package out there but doesn't provide full control over it
What you can do is Use Navigator and all your taps and you need to manage s separate navigation stack for all taps.
Here is a great article by Andrea Bizzotto
https://medium.com/coding-with-flutter/flutter-case-study-multiple-navigators-with-bottomnavigationbar-90eb6caa6dbf
Was messing around with the bottomNavigationBarTheme and noticed that the label was showing on the selected item even though showSelectedLabels was set to false. Is this a bug or am I missing something?
This behavior only holds true as long as the property is declared in the ThemeData when moved to the bottomNavigationBar it behaves as expected.
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'app',
themeMode: ThemeMode.dark,
darkTheme: ThemeData(
brightness: Brightness.dark,
primarySwatch: Colors.blueGrey,
canvasColor: Color.fromRGBO(52, 58, 70, 1),
bottomNavigationBarTheme: BottomNavigationBarThemeData(
selectedIconTheme: IconThemeData(
color: Color.fromRGBO(113, 124, 152, 1),
size: 28,
),
unselectedIconTheme: IconThemeData(
color: Color.fromRGBO(196, 201, 212, 1),
size: 28,
),
type: BottomNavigationBarType.fixed,
showSelectedLabels: false,
showUnselectedLabels: false,
),
cardColor: Color.fromRGBO(52, 58, 70, 1)
),
home: HomePage(),
);
}
}
class HomePage extends StatefulWidget {
HomePage({Key key}) : super(key: key);
#override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
int _selectedIndex = 0;
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('appbar'),
),
body: Center(
child: Text('Text here'),
),
bottomNavigationBar: BottomNavigationBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.access_time),
title: Text('a'),
),
BottomNavigationBarItem(
icon: Icon(Icons.view_day),
title: Text('a'),
),
BottomNavigationBarItem(
icon: Icon(Icons.equalizer),
title: Text('a'),
),
BottomNavigationBarItem(
icon: Icon(Icons.notifications_active),
title: Text('a'),
),
BottomNavigationBarItem(
icon: Icon(Icons.account_circle),
title: Text('a'),
),
],
currentIndex: _selectedIndex,
onTap: _onItemTapped,
),
);
}
}
That has to do with the BottomNavigationBarType.
if you are using more than 3 Items in BottonNavigationBar, you have to set the type to BottomNavigationBarType.fixed
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
this has been already discussed on the Flutter github issue page:
https://github.com/flutter/flutter/issues/13642#issuecomment-353945439
This is the right way and worked for me and that too with type BottomNavigationBarType.shifting
showUnselectedLabels: true, unselectedItemColor: Colors.black,
you can do this by multiple way
use Text("");
remove title
You should not be using title, instead of that you should use label without Text() as shown below:
bottomNavigationBar: BottomNavigationBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.access_time),
label:'a',
),
BottomNavigationBarItem(
icon: Icon(Icons.view_day),
label: 'a',
),
BottomNavigationBarItem(
icon: Icon(Icons.equalizer),
label: 'a',
),
BottomNavigationBarItem(
icon: Icon(Icons.notifications_active),
label: 'a',
),
BottomNavigationBarItem(
icon: Icon(Icons.account_circle),
label: 'a',
),
],
currentIndex: _selectedIndex,
onTap: _onItemTapped,
),
U should use this code :
bottomNavigationBar: BottomNavigationBar(
//use both properties
type: BottomNavigationBarType.fixed,
showUnselectedLabels: true,
//-----------
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.icon1),
label:'item 1',
),
BottomNavigationBarItem(
icon: Icon(Icons.icon2),
label: 'item 2',
),
],
)
when you new the BottomNavigationBarItem, add the "tooltip:''"
In both a tabbar and nav bar, I would like the selected tab to have a different background to the rest of the bar.
For example: https://imgur.com/a/jxD8MTg
Is this possible in flutter?
If you are using stateful Widget, you could just set the state of the currentIndex in the on Tap method
#override
void initState() {
super.initState();
currentIndex = 0;
}
BottomNavigationBar(
backgroundColor: Colors.white,
type: BottomNavigationBarType.fixed,
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
backgroundColor: currentIndex == 0 ? Colors.green : Colors.black,
title: Text('0'),
),
BottomNavigationBarItem(
backgroundColor: currentIndex == 1 ? Colors.green : Colors.black,
title: Text('1'),
),
],
currentIndex: currentIndex,
onTap: (int index) {
setState(() {
currentIndex = index;
});
_navigateToPage(index);
},
);
import 'package:flutter/material.dart';
class ChangeBottomNavBarTextColor extends StatefulWidget {
#override
ChangeBottomNavBarTextColorState createState() {
return new ChangeBottomNavBarTextColorState();
}
}
class ChangeBottomNavBarTextColorState
extends State<ChangeBottomNavBarTextColor> {
String text = 'Home';
var curIndex = 0;
_onTap(int index) {
setState(() {
curIndex = index;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Change Bottom Nav Bar Text Color Example"),
),
body: Center(
child: Text(text,
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
)),
),
bottomNavigationBar: BottomNavigationBar(
selectedItemColor: Colors.red,
currentIndex: curIndex,
type: BottomNavigationBarType.fixed,
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(
Icons.home,
),
title: Text("Home", style: TextStyle(color: Colors.teal)),
),
BottomNavigationBarItem(
icon: Icon(
Icons.favorite,
),
title: Text("Favorite", style: TextStyle(color: Colors.pink)),
),
BottomNavigationBarItem(
icon: Icon(
Icons.person,
),
title: Text("Profile", style: TextStyle(color: Colors.brown)),
),
BottomNavigationBarItem(
activeIcon: Icon(
Icons.info,
color: Colors.red,
),
icon: Icon(
Icons.settings,
),
title: Text("Settings", style: TextStyle(color: Colors.amber)),
),
],
onTap: _onTap,
),
);
}
}
You can use something like that. When you clicked, _onTap function will change currentIndex. Then selectedItemColor will define the color of the selected index. You can play with the colors whatever you want.
I am struggling to set a background color and selected item color for BottomNavigationBar.
This is my Widget:
Widget BottomMenu(selectedIndex, onItemTapped) {
return BottomNavigationBar(
type: BottomNavigationBarType.fixed,
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(backgroundColor: Colors.indigo, icon: Icon(Icons.search), title: Text('Search')),
BottomNavigationBarItem(
icon: Icon(Icons.location_searching), title: Text('Settings')),
BottomNavigationBarItem(
icon: Icon(Icons.message), title: Text('Messages')),
],
currentIndex: selectedIndex,
fixedColor: Colors.redAccent,
onTap: onItemTapped,
);
}
However this is not working. All I get is white background with red font and icons. I want the whole navigation bar to be in indigo color.
Anyone?
For background color. You can use Theme widget to wrap your BottomNavigationBar. And set canvasColor in the ThemeDate. Here is the code you want.
bottomNavigationBar: Theme(
data: ThemeData(
canvasColor: Colors.lightBlue
),
child: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
items: <BottomNavigationBarItem>[
BottomNavigationBarItem(
backgroundColor: Colors.indigo,
icon: Icon(Icons.search),
title: Text('Search')),
BottomNavigationBarItem(
icon: Icon(Icons.location_searching), title: Text('Settings')),
BottomNavigationBarItem(
icon: Icon(Icons.message), title: Text('Messages')),
],
fixedColor: Colors.redAccent,
),
),
Inorder to change bottom navigation bar background color.
return BottomNavigationBar(
type: BottomNavigationBarType.fixed,
backgroundColor: Theme.of(context).primaryColor,
onTap: (value) {
},
selectedItemColor: HexColor(iconColor[0]),
unselectedItemColor: HexColor(iconColor[1]),
items: [
BottomNavigationBarItem(
),
BottomNavigationBarItem(
),
BottomNavigationBarItem(
),
BottomNavigationBarItem(
),
],
);
Add colors to your theme like that.
class ThemeDataConfig {
ThemeDataConfig._();
static final ThemeData lightTheme = ThemeData(
scaffoldBackgroundColor: HexColor("${Constants.backgroundColorLight}"),
backgroundColor: HexColor("#"),
brightness: Brightness.light,
textTheme: lightTextTheme,
primaryColor: HexColor("#"),
accentColor: HexColor("#"),
cardColor: HexColor("#"),
bottomAppBarColor: HexColor("#").withOpacity(.25), // bottom Nav bar pop up color
buttonColor: HexColor("#"),// Button Border Color used in bottom nav button
);
}
I faced the same the issue. One thing you can do is create a color as a const and then use it in backgroundColor in the items:
const kBottomBarColor = Colors.green,
And then you can use it in icons backgroundColor:
BottomNavigationBarItem(
icon: Icon(Icons.search),
label: 'Business',
backgroundColor: kBottomBarColor,
),