Flutter:ThemeData中bottomNavigationBar的常见深色和浅色主题

时间:2019-11-12 11:03:55

标签: flutter dart material-design flutter-layout

我想在深色和浅色主题模式下都更改bottomNavigationBar的unselectedItemColor和selectedItemColor颜色。

我想有一个通用的bottomNavigationBar主题,但是在ThemeData中没有bottomNavigationBar主题的属性。

现在我正在像这样在运行时检查暗光模式,

var brightness = MediaQuery.of(context).platformBrightness;

BottomNavigationBar(
 unselectedItemColor: brightness == Brightness.light
                  ? AppColors.colorHint
                  : Colors.white70,
  selectedItemColor: brightness == Brightness.light
                  ? AppColors.themeColor
                  : AppColors.themeColor.shade200,
);

但是我想在main.dart中使用专用的BottomNavigationBar主题, 就像我在这里声明appBarTheme一样,我想同时为黑暗和明亮模式声明BottomNavigationBar主题。

  ThemeData _buildDarkTheme() {
    final ThemeData base = ThemeData(
      brightness: Brightness.dark,
      appBarTheme: AppBarTheme(
          textTheme: TextTheme(title: AppStyle.titleDark)
      ),

    );
    return base;
  } 

  final ThemeData kLightTheme = _buildLightTheme();
  final ThemeData kDarkTheme = _buildDarkTheme();

   runApp(
      MaterialApp(
      theme: kLightTheme,
      darkTheme: kDarkTheme,
      ) 
    );

1 个答案:

答案 0 :(得分:1)

您可以在

使用此代码
bottomNavigationBar: new Theme(
  data: Theme.of(context).copyWith(),  
  child: new BottomNavigationBar(items: [  new BottomNavigationBarItem(),] ),

)