如何根据设置的主题更改flutter中的状态栏图标和文本颜色?

pri*_*oo7 7 user-interface android statusbar ios flutter

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

在我的主题课程中,我有一个函数,我正在尝试下面的代码,但尚未实现结果:

目前的主题代码:


// 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,
      ),

  );

Run Code Online (Sandbox Code Playgroud)

主题更改代码:


// 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();
   }


Run Code Online (Sandbox Code Playgroud)

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

状态栏中图标的颜色(Flutter)

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

Hem*_*Raj 13

您可以通过调用setSystemUIOverlayStylewith required 主题来设置状态栏主题。

SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.dark);
Run Code Online (Sandbox Code Playgroud)

或者

SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light);
Run Code Online (Sandbox Code Playgroud)

更新:

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

SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
  statusBarColor: Colors.white
));
Run Code Online (Sandbox Code Playgroud)

在此处检查可用属性。


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

希望有帮助!


Abh*_*bhi 5

注意:
如果集合SystemUiOverlayStyle没有被其他小部件(如AppBar.

如果AppBar使用了,请尝试使用,

appBar: AppBar(
    title: const Text('Title text'),
    brightness: Brightness.dark,
),
Run Code Online (Sandbox Code Playgroud)

对于默认lightdark主题。


Cop*_*oad 5

更新(颤振2.4.0)

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

  • 更少的定制:

    AppBar(
      systemOverlayStyle: SystemUiOverlayStyle.dark, // Both iOS and Android (dark icons)
    )
    
    Run Code Online (Sandbox Code Playgroud)
  • 更多定制:

    AppBar(
      systemOverlayStyle: SystemUiOverlayStyle(
        statusBarBrightness: Brightness.light, // For iOS: (dark icons)
        statusBarIconBrightness: Brightness.dark, // For Android: (dark icons)
        statusBarColor: ...,
      ),
    )
    
    Run Code Online (Sandbox Code Playgroud)