我选择和未选择的BottomNavigationBarItems的标签颜色不会改变。。。我做错了什么?这是代码:
bottomNavigationBar: BottomNavigationBar(
selectedLabelStyle: TextStyle(color: Colors.black),
unselectedLabelStyle: TextStyle(color: Colors.black),
backgroundColor: Colors.white,
onTap: onTabTapped,
currentIndex: _currentIndex, // this will be set when a new tab is tapped
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home, color: Colors.black,),
label: 'Home',
),
BottomNavigationBarItem(
icon: Icon(Icons.search, color: Colors.black,),
label: 'Messages',
),
BottomNavigationBarItem(
icon: Icon(Icons.person, color: Colors.black,),
label: 'Profile'
)
],
),
更改此
selectedLabelStyle: TextStyle(color: Colors.black),
unselectedLabelStyle: TextStyle(color: Colors.black),
对此:
selectedItemColor: Colors.black,
unselectedItemColor: Colors.black,
您可以使用子属性并添加文本类,在其中您可以轻松地设置文本样式
将其添加到底部导航栏样式中:
showUnselectedLabels: true,
常见的错误可能是将_currentIndex
放入构建方法中。遵循
int _currentIndex = 0;
@override
Widget build(BuildContext context) {
return Scaffold(
bottomNavigationBar: BottomNavigationBar(
selectedLabelStyle: TextStyle(color: Colors.black),
unselectedLabelStyle: TextStyle(color: Colors.black),
backgroundColor: Colors.white,
onTap: (v) {
_currentIndex = v;
print(v);
setState(() {});
},
currentIndex: _currentIndex,
items: [
使用下面的简单解决方案更改selectedLabelStyle和未选中标签样式-(标签文本的颜色(:
BottomNavigationBarItem(
activeIcon: Image.asset(
"assets/images/race_icon_fill.png",
height: 30,
width: 30,
fit: BoxFit.cover,
color: appThemeColor,
),
icon: Image.asset(
"assets/images/race_icon.png",
height: 30,
width: 30,
fit: BoxFit.cover,
color: appLightGrayColor,
),
label: 'My Races',
),
==========================================
selectedItemColor: Colors.black,
unselectedItemColor: Colors.grey,
selectedLabelStyle: const TextStyle(
fontWeight: FontWeight.w500, fontFamily: AlbertSans),
unselectedLabelStyle: const TextStyle(
fontWeight: FontWeight.w300, fontFamily: AlbertSans, color: Colors.deepPurple),