简体   繁体   English

Flutter 选择时更改 NavigationBarThemeData labelTextStyle 颜色

[英]Flutter Change NavigationBarThemeData labelTextStyle color when selected

I used the NavigationBarTheme in my bottomNavigationBar.我在底部导航栏中使用了 NavigationBarTheme。 I would like to ask if its possible to change the labelTextStyle when selected?我想问一下是否可以在选择时更改 labelTextStyle? Currently, I only have the grey color.目前,我只有灰色。

 bottomNavigationBar: NavigationBarTheme( data: NavigationBarThemeData( height: 65, indicatorColor: Colors.transparent, backgroundColor: Colors.white, labelTextStyle: MaterialStateProperty.all( const TextStyle( fontSize: 13.0, fontWeight: FontWeight.w700, color: Colors.grey, letterSpacing: 1.0, ), ), ),

Hy there is property called selectedLableStyle for BottomNavigationBar , you can use it like below - Hy 有一个名为selectedLableStyleBottomNavigationBar属性,您可以像下面这样使用它 -

bottomNavigationBar: BottomNavigationBar(
  // this is the property use to style lable
  selectedLabelStyle: TextStyle(fontSize: 22),
  selectedItemColor: Colors.red,
  items: const <BottomNavigationBarItem>[
    BottomNavigationBarItem(
      icon: Icon(Icons.home),
      label: 'Home',
    ),
    BottomNavigationBarItem(
      icon: Icon(Icons.business),
      label: 'Business',
    ),
  ],
),

if you wants to use NavigationBarTheme as in your question then use MaterialStateProperty.resolveWith instead of MaterialStateProperty.all like below -如果您想在问题中使用NavigationBarTheme ,请使用MaterialStateProperty.resolveWith而不是 MaterialStateProperty.all,如下所示 -

bottomNavigationBar: NavigationBarTheme(
        data: NavigationBarThemeData(
          height: 65,
          indicatorColor: Colors.transparent,
          backgroundColor: Colors.white,
          labelTextStyle: MaterialStateProperty.resolveWith((states) {
            if (states.contains(MaterialState.selected)) {
              return const TextStyle(
                fontSize: 13.0,
                fontWeight: FontWeight.w700,
                color: Colors.red,
                letterSpacing: 1.0,
              );
            }
            return const TextStyle(
              fontSize: 13.0,
              fontWeight: FontWeight.w700,
              color: Colors.grey,
              letterSpacing: 1.0,
            );
          }
          ),
        ),

For more info - BottomNavigationBar , MaterialStateProperty欲了解更多信息 - BottomNavigationBarMaterialStateProperty

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM