【问题标题】:Flutter: Common Dark and Light theme for bottomNavigationBar in ThemeDataFlutter:ThemeData 中bottomNavigationBar 的常用明暗主题
【发布时间】:2020-03-08 02:01:18
【问题描述】:

我想在深色和浅色主题模式下更改 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 一样,我也想为 Dark 和 Light 模式声明 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,
      ) 
    );

【问题讨论】:

    标签: flutter dart material-design flutter-layout


    【解决方案1】:

    您可以在

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

    【讨论】:

    • 是的,我知道,我希望我们可以在 MaterialApp 中声明这一点,new 关键字也是可选的,Effective Dart guidelines 不鼓励这样做。
    猜你喜欢
    • 2022-07-19
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    • 2020-09-12
    • 2020-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多