Custom Tab Bar Icon Colors

Custom tab bar icon colors

You can try this to tint selected icon :

// Custom the tab bar
[[UITabBar appearance] setSelectedImageTintColor:[UIColor whiteColor]];

and this to tint the non active icon :

[self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"item_seleted.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"item_unselected.png"]];

How to change color of tab's icon in selected and unselected state in flutter?

As per the directions given by Britannio, I have solved my problem but I want to share my solution so that it can help others. I am confused about one thing that I have to call setState() with empty body which is not recommended so if any one have a better solution then please comment. I'll update it.

     TabController _tabController;

@override
void initState() {
super.initState();
_tabController = new TabController(vsync: this, length: 3);
_tabController.addListener(_handleTabSelection);
}

void _handleTabSelection() {
setState(() {
});
}

TabBar(
controller: _tabController,
indicatorColor: Colors.grey,
labelColor: Colors.black,
unselectedLabelColor: Colors.grey,
tabs: [
Tab(
text: 'Sale',
icon: Icon(Icons.directions_car,
color: _tabController.index == 0
? Colors.black
: Colors.grey)),
Tab(
text: 'Latest',
icon: Icon(Icons.directions_transit,
color: _tabController.index == 1
? Colors.black
: Colors.grey)),
Tab(
text: 'Popular',
icon: Icon(Icons.directions_bike,
color: _tabController.index == 2
? Colors.black
: Colors.grey)),
],
)

Change tab icon color on selected

You need to provide a TabController for your selected tabs and on the basis of TabController's index, you can change the color.

 TabController _tabController;

@override
void initState() {
super.initState();
_tabController = new TabController(vsync: this, length: 3);
_tabController.addListener(_handleTabSelection);
}

void _handleTabSelection() {
setState(() {
});
}

TabBar(
controller: _tabController,
labelColor: Colors.white,
unselectedLabelColor: Colors.grey,
labelStyle: TextStyle(fontSize: 13.0),
indicator: BoxDecoration(
borderRadius: BorderRadius.circular(50), // Creates border
color: const Color(0xFF62A6E9)),
tabs: [
Tab(icon: SvgPicture.asset(
'assets/trending.svg',
color: _tabController.index == 0
? Colors.black
: Color(0xFF71768D),
width: 15,
height: 15
), text: "Популярное"),
Tab(icon: SvgPicture.asset(
'assets/new.svg',
color: _tabController.index == 1
? Colors.black
: Color(0xFF71768D),
width: 15,
height: 15
), text: "Новые"),
Tab(icon: SvgPicture.asset(
'assets/comingsoon.svg',
color: _tabController.index == 2
? Colors.black
: Color(0xFF71768D),
width: 15,
height: 15
), text: "Ожидаемые"),

EDIT:

Please dispose the TabController as well.

@override
void dispose() {
_tabController.dispose();
super.dispose();
}

How to change icon's color of selected tab bar item in SwiftUI?

You can use foregroundColor in this case.

Image("shoppingbasket")
.renderingMode(.template)
.foregroundColor(Color(.secondaryLabel))

how to change tabBar icon color in ios

This accomplishes what you're asking for:

[[UITabBar appearance] setSelectedImageTintColor:[UIColor redColor]];
[[UITabBar appearance] setAlpha:0.25];

TabBar icon color when tab is selected

To change the selected tab color, you just need to add this to TabBar :

labelColor: Colors.
unselectedLabelColor: Colors.white,

Full code:

DefaultTabController(
length: 2,
child: Scaffold(
appBar: AppBar(
bottom: TabBar(
labelColor: Colors.deepOrange,
unselectedLabelColor: Colors.white,
tabs: <Tab>[
Tab(
child: Row(
children: <Widget>[
Text("Select", textAlign: TextAlign.start),
SizedBox(
width: 24.0,
),
Icon(
Icons.arrow_drop_down,
color: Colors.blue.shade400,
),
],
),
),
Tab(
child: Row(
children: <Widget>[
Text("Select", textAlign: TextAlign.start),
SizedBox(
width: 24.0,
),
Icon(
Icons.arrow_drop_down,
color: Colors.blue.shade400,
),
],
),
),
],
),
),
body: Center(
child: Container(
child: Text("This is a page blahblah"),
),
),
),
)

EDIT:
If u want to only change the icon color, then add color to Text and remove the color from the Icon in Tab, Code:

DefaultTabController(
length: 2,
child: Scaffold(
appBar: AppBar(
bottom: TabBar(
labelColor: Colors.deepOrange,
unselectedLabelColor: Colors.white,
tabs: <Tab>[
Tab(
child: Row(
children: <Widget>[
Text("Select", textAlign: TextAlign.start, style: TextStyle(color: Colors.white),),
SizedBox(
width: 24.0,
),
Icon(
Icons.arrow_drop_down,
),
],
),
),
Tab(
child: Row(
children: <Widget>[
Text("Select", textAlign: TextAlign.start, style: TextStyle(color: Colors.white),),
SizedBox(
width: 24.0,
),
Icon(
Icons.arrow_drop_down,

),
],
),
),
],
),
),
body: Center(
child: Container(
child: Text("This is a page blahblah"),
),
),
),
)

EDIT #2
Now , This code changes the Icon color, but leaves the text color change as default (You can customize the change for color of text like the icon color). Code:

import 'package:flutter/material.dart';

class FirstPage extends StatefulWidget {
@override
_FirstPageState createState() => _FirstPageState();
}

class _FirstPageState extends State<FirstPage> {
int _currentIndex = 0;
@override
Widget build(BuildContext context) {
return DefaultTabController(
length: 2,
child: Scaffold(
appBar: AppBar(
bottom: TabBar(
onTap: (index){
setState(() {
_currentIndex = index;
});
},

tabs: <Tab>[
Tab(
child: Row(
children: <Widget>[
Text("Select", textAlign: TextAlign.start,),
SizedBox(
width: 24.0,
),
Icon(
Icons.arrow_drop_down,
color: _currentIndex == 0 ? Colors.deepOrange : Colors.white54
),
],
),
),
Tab(
child: Row(
children: <Widget>[
Text("Select", textAlign: TextAlign.start,),
SizedBox(
width: 24.0,
),
Icon(
Icons.arrow_drop_down,
color: _currentIndex == 1 ? Colors.deepOrange : Colors.white54,
),
],
),
),
],
),
),
body: Center(
child: Container(
child: Text("This is a page blahblah"),
),
),
),
);
}
}

Changing tab bar item image and text color iOS

From UITabBarItem class docs:

By default, the actual unselected and selected images are
automatically created from the alpha values in the source images. To
prevent system coloring, provide images with
UIImageRenderingModeAlwaysOriginal.

The clue is not whether you use UIImageRenderingModeAlwaysOriginal, the important thing is when to use it.

To prevent the grey color for unselected items, you will just need to prevent the system colouring for the unselected image. Here is how to do this:

var firstViewController:UIViewController = UIViewController()
// The following statement is what you need
var customTabBarItem:UITabBarItem = UITabBarItem(title: nil, image: UIImage(named: "YOUR_IMAGE_NAME")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal), selectedImage: UIImage(named: "YOUR_IMAGE_NAME"))
firstViewController.tabBarItem = customTabBarItem

As you can see, I asked iOS to apply the original color (white, yellow, red, whatever) of the image ONLY for the UNSELECTED state, and leave the image as it is for the SELECTED state.

Also, you may need to add a tint color for the tab bar in order to apply a different color for the SELECTED state (instead of the default iOS blue color). As per your screenshot above, you are applying white color for the selected state:

self.tabBar.tintColor = UIColor.whiteColor()

EDIT:

Sample Image



Related Topics



Leave a reply



Submit