Flutter-如何在暗模式下更改状态栏文本颜色?

Ale*_*eng 10 text colors statusbar flutter

我希望在 iOS 13 Dark Mode 中控制状态栏文本颜色。当不打开暗模式时,我可以通过设置 Scaffold 的 AppBar 属性“亮度”来更改状态栏颜色。代码如下:

return Scaffold(
      appBar: AppBar(
        brightness: Brightness.light,  //<--Here!!!
        title: Text(widget.title),
      ),
...
Run Code Online (Sandbox Code Playgroud)

努力是这样的:

灯光亮度: 在此处输入图片说明

暗亮度: 在此处输入图片说明

但是当我启用模拟器的暗模式时,该方法不起作用。打开模拟器的“Dark Appearance”: 在此处输入图片说明

打开“Dark Appearance”后,状态栏文字颜色无法通过该方法改变,只是白色(亮度模式)。 在此处输入图片说明

我尝试过这些方法来更改状态栏文本颜色:

方法一:

void main() {
  SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light);
  SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown
  ]);
  runApp(MyApp());
}
Run Code Online (Sandbox Code Playgroud)

方法二:

return AnnotatedRegion<SystemUiOverlayStyle>(
      value: SystemUiOverlayStyle.light,
      child: Material(
        child: Scaffold(
          appBar: AppBar(
            brightness: Brightness.light,
            title: Text(widget.title),
          ),
          body: Center(
Run Code Online (Sandbox Code Playgroud)

但他们都无法工作。

希望您的帮助!谢谢你。

小智 6

首先进入 ios/Runner 文件夹。接下来打开 info.plist 并将以下几行添加到 Dict 部分。

<key>UIUserInterfaceStyle</key>
<string>Light</string>
<key>UIViewControllerBasedStatusBarAppearance</key>
<true/>
Run Code Online (Sandbox Code Playgroud)

下一个。确保在 MaterialApp 的主题设置中有这些行:

...
MaterialApp(
    themeMode: ThemeMode.light, // Change it as you want
    theme: ThemeData(
        primaryColor: Colors.white,
        primaryColorBrightness: Brightness.light,
        brightness: Brightness.light,
        primaryColorDark: Colors.black,
        canvasColor: Colors.white,
        // next line is important!
        appBarTheme: AppBarTheme(brightness: Brightness.light)),
    darkTheme: ThemeData(
        primaryColor: Colors.black,
        primaryColorBrightness: Brightness.dark,
        primaryColorLight: Colors.black,
        brightness: Brightness.dark,
        primaryColorDark: Colors.black,      
        indicatorColor: Colors.white,
        canvasColor: Colors.black,
        // next line is important!
        appBarTheme: AppBarTheme(brightness: Brightness.dark)),
...
Run Code Online (Sandbox Code Playgroud)

祝你好运!

PS 你不必在这里设置亮度!!不再:)

Scaffold(
    appBar: AppBar(
    brightness: Brightness.light,  //<--Here!!!
    title: Text(widget.title),
),
Run Code Online (Sandbox Code Playgroud)