Flutter中bottomNavigationBarTheme无法修改Label文字颜色问题
问题根源:BottomNavigationBar的文字颜色优先级规则为:
selectedItemColor/unselectedItemColor>selectedLabelStyle/unselectedLabelStyle中的color属性。若你在主题或BottomNavigationBar组件本身配置了selectedItemColor、unselectedItemColor,labelStyle里设置的颜色会被直接覆盖,这就是字体大小生效但颜色不生效的原因。可行解决方案:
- 方案一:删除
BottomNavigationBarThemeData中的selectedItemColor和unselectedItemColor配置,同时确保BottomNavigationBar组件上没有直接设置这两个属性,此时selectedLabelStyle、unselectedLabelStyle里的颜色设置会正常生效。 - 方案二:若需要保留
selectedItemColor/unselectedItemColor,直接通过这两个属性控制文字颜色即可,无需在labelStyle中重复设置颜色。
- 方案一:删除
示例代码:
ThemeData( bottomNavigationBarTheme: BottomNavigationBarThemeData( // 移除selectedItemColor和unselectedItemColor配置 selectedLabelStyle: TextStyle( color: Colors.red, // 颜色设置生效 fontSize: 16, ), unselectedLabelStyle: TextStyle( color: Colors.grey, // 颜色设置生效 fontSize: 14, ), ), )
内容的提问来源于stack exchange,提问作者neketta300
相关产品推荐
相关产品推荐

