【问题标题】:How to change status bar icon and text color in flutter based on theme been set?如何根据设置的主题更改颤动中的状态栏图标和文本颜色?
【发布时间】:2020-01-19 14:05:23
【问题描述】:

如何在没有任何第三方插件的情况下更新状态栏图标的颜色?

在我的主题类中,我有一个函数,我正在尝试使用下面的代码,但目前尚未取得结果:

目前的主题代码:


// custom light theme for app
  static final customLightTheme = ThemeData.light().copyWith(
    brightness: Brightness.light,
    primaryColor: notWhite,
    accentColor: Colors.amberAccent,
    scaffoldBackgroundColor: notWhite,
    primaryTextTheme: TextTheme(
      title: TextStyle(
        color: Colors.black
      ),

    ),
    appBarTheme: AppBarTheme(
      iconTheme: IconThemeData(color: Colors.black),
      elevation: 0.0,
    )
  );

  // custom dark theme for app
  static final customDarkTheme = ThemeData.dark().copyWith(
    brightness: Brightness.dark,
    primaryColor: Colors.black,
      accentColor: Colors.orange,
      scaffoldBackgroundColor: Colors.black87,
      primaryTextTheme: TextTheme(
        title: TextStyle(
            color: Colors.white
        ),

      ),
      appBarTheme: AppBarTheme(
        iconTheme: IconThemeData(color: Colors.white),
        elevation: 0.0,
      ),

  );


主题更换器代码:


// set theme for the app
   setTheme(ThemeData theme) {
     _themeData = theme;

     if(_themeData == ThemeChanger.customLightTheme){
       SystemChrome.setSystemUIOverlayStyle(
         const SystemUiOverlayStyle(
           statusBarColor: Colors.white,
           systemNavigationBarColor: Colors.white,
           systemNavigationBarDividerColor: Colors.black,
           systemNavigationBarIconBrightness: Brightness.dark,
         ),
       );
     } else {
       SystemChrome.setSystemUIOverlayStyle(
         const SystemUiOverlayStyle(
           statusBarColor: Colors.blue,
           systemNavigationBarColor: Colors.blue,
           systemNavigationBarDividerColor: Colors.red,
           systemNavigationBarIconBrightness: Brightness.light,
         ),
       );
     }

     notifyListeners();
   }


这不是我想要的,因为我不想要第三方解决方案。

Icon's color in status bar (Flutter)

目前我正在获得白色/浅色主题中的黑色图标,以及主题更改时深色/黑色主题中的黑色图标(应该是白色图标)。休息一切正常。

【问题讨论】:

标签: android ios user-interface flutter statusbar


【解决方案1】:

您可以通过使用所需主题调用setSystemUIOverlayStyle来设置状态栏主题。

SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.dark);

SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light);

更新:

您可以指定自己的属性,例如,

SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
  statusBarColor: Colors.white
));

检查可用属性here


注意:对于应用程序的light 主题,使用dark 覆盖,反之亦然。还要确保你import 'package:flutter/services.dart';

希望有帮助!

【讨论】:

  • 顺便说一句,如果我想修改 setSystemUIOverlayStyle 中的颜色怎么办。我上面的代码不起作用,因为你可以看到我尝试使用蓝色和红色。
  • 所以我应该删除 const 字,只使用其余的?好吧,明天试试看是否有效?
  • 新手,问题是在哪里写?
【解决方案2】:

注意:
如果集合SystemUiOverlayStyle 没有被AppBar 等其他小部件覆盖,则接受的答案有效。

更新

由于很多人都有这个问题,所以添加一个涵盖我能想到的所有案例的答案。

对于所有情况,请酌情使用systemOverlayStyle

轻主题

SystemUiOverlayStyle.light

深色主题

SystemUiOverlayStyle.dark

自定义

SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
  systemNavigationBarColor: Color(0xFF000000),
  systemNavigationBarIconBrightness: Brightness.light,
  systemNavigationBarDividerColor: null,
  statusBarColor: Color(0xFFD59898),
  statusBarIconBrightness: Brightness.light,
  statusBarBrightness: Brightness.dark,
));

仅覆盖所需的属性。前 3 个用于导航栏,其余 3 个用于状态栏。

1。默认情况

在整个应用程序中保持不变的系统覆盖样式,没有应用栏和其他干扰系统 UI 覆盖样式的小部件。

将此代码添加到build 方法内的应用程序的根小部件

SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light);

2。具有恒定系统覆盖样式的应用

在整个应用中使用应用栏的恒定主题。

如果有没有应用栏的屏幕,请添加选项 1 中给出的代码,然后将其也添加。

MaterialApp(
    theme: ThemeData(
        appBarTheme: const AppBarTheme(
            systemOverlayStyle: SystemUiOverlayStyle.light,
        ),
    ),
),

3。具有不同系统覆盖样式的应用栏的应用

每个应用栏都有不同的系统覆盖样式。

appBar: AppBar(
    title: const Text('Title text'),
    systemOverlayStyle: SystemUiOverlayStyle.light,
)

2.4.0-0.0以下版本的旧答案。

brightness 属性在 AppBar 中已弃用。

此功能在 v2.4.0-0.0.pre 之后被弃用

appBar: AppBar(
    title: const Text('Title text'),
    brightness: Brightness.dark,
),

根据需要使用Brightness.lightBrightness.dark

【讨论】:

  • 它在颤振 v2.5 中已弃用
  • @SoloWolf93,更新了答案?
【解决方案3】:

更改状态栏图标颜色(变为白色)。

将此 SystemChrome... 添加到您的 void main()。在此之前不要忘记import 'package:flutter/services.dart';

void main() {
  SystemChrome.setSystemUIOverlayStyle(
    SystemUiOverlayStyle(
      statusBarIconBrightness: Brightness.light,
    ),
  );
  runApp(MyApp());
}

在此之后,如果您将Scaffold() 用于屏幕/页面,则在AppBar() 中,像这样添加此行brightness: Brightness.dark,

return Scaffold(
      appBar: AppBar(
        brightness: Brightness.dark,

然后完成。

【讨论】:

    【解决方案4】:

    这个改变主题的答案对我不起作用,但我用这个。我知道两者是相同的东西,但通过这种方式,我们也可以使用颜色和图标主题。

     SystemChrome.setSystemUIOverlayStyle(
                SystemUiOverlayStyle(
                    statusBarColor: Color(0xFFdde6e8),
                    statusBarIconBrightness: Brightness.dark),
              );
    

    【讨论】:

      【解决方案5】:

      更新(Flutter 2.4.0)

      不要使用 AnnotatedRegionAppBar.brightness,因为它们已被弃用,请改用:

      • 更少的自定义:

        AppBar(
          systemOverlayStyle: SystemUiOverlayStyle.dark, // Both iOS and Android (dark icons)
        )
        
      • 更多自定义:

        AppBar(
          systemOverlayStyle: SystemUiOverlayStyle(
            statusBarBrightness: Brightness.light, // For iOS: (dark icons)
            statusBarIconBrightness: Brightness.dark, // For Android: (dark icons)
            statusBarColor: ...,
          ),
        )
        

      【讨论】:

        【解决方案6】:

        如果您使用的是 AppBar,则有 brightness 属性。您可以将亮度属性设置为dark,这样状态栏的图标就会变成白色。

        AppBar(
            brightness: Brightness.dark
        )
        

        【讨论】:

        • 感谢您的回答,但它看起来或多或少类似于 @Abhimanyu 的 answer
        【解决方案7】:

        如果您为整个应用程序使用主题生成器,则有一种新方法可以将亮度(图标颜色)更改为白色或黑色,如下所示

        class MyApp extends StatelessWidget {
        
          // This widget is the root of your application.
          @override
          Widget build(BuildContext context) {
            return MaterialApp(
              title: 'My App',
              debugShowCheckedModeBanner: false,
              builder: (context, navigator) {
                return Theme(
                  data: ThemeData(
                      primaryColor: AppColors.primary,
                      accentColor: AppColors.accent,
                      appBarTheme: AppBarTheme(systemOverlayStyle: 
               SystemUiOverlayStyle.light) ,//this is new way to change icons color of status bar
                      fontFamily: 'Poppins'
                  ),
                  child: navigator,
                );
              },
              
            );
          }
        }
        

        【讨论】:

          猜你喜欢
          • 2021-10-11
          • 2021-11-27
          • 2022-01-20
          • 1970-01-01
          • 2021-08-03
          • 2016-02-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多