简体   繁体   English

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

[英]Flutter-How to change the Statusbar text color in Dark Mode?

I hope to control the statusbar text color in iOS 13 Dark Mode.我希望在 iOS 13 Dark Mode 中控制状态栏文字颜色。 I could change the statusbar color by setting the Scaffold's AppBar property "brightness" When not open the Dark Mode.当不打开暗模式时,我可以通过设置脚手架的 AppBar 属性“亮度”来更改状态栏颜色。 Codes just like below:代码如下:

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

The effort just like this:努力是这样的:

light brightness:灯光亮度: 在此处输入图像描述

dark brightness:暗亮度: 在此处输入图像描述

But when I enable the simulator's Dark Mode , the method is not working.但是当我启用模拟器的Dark Mode时,该方法不起作用。 Open the simulator's "Dark Appearance":打开模拟器的“Dark Appearance”: 在此处输入图像描述

After opening the "Dark Appearance", the statusbar text color couldn't change any more by the method, it's just in white color(lightness mode).打开“Dark Appearance”后,状态栏文字颜色无法再通过该方法改变,只是白色(亮度模式)。 在此处输入图像描述

I have tried those method to change statusbar text color:我已经尝试过这些方法来更改状态栏文本颜色:

Method 1:方法一:

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

Method 2:方法二:

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

But neither of them could work.但他们都无法工作。

Hope your help.希望你的帮助。 Thank you.谢谢你。

At first go to ios/Runner folder.首先 go 到 ios/Runner 文件夹。 Next open info.plist and add the following lines into the Dict section.接下来打开 info.plist 并将以下行添加到 Dict 部分。

<key>UIUserInterfaceStyle</key>
<string>Light</string>
<key>UIViewControllerBasedStatusBarAppearance</key>
<true/>

Next.下一个。 Make sure you have these lines in Theme settings of your MaterialApp:确保在 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)),
...

Good luck!祝你好运!

PS You don't have to set brightness Here:! PS你不必在这里设置亮度:! anymore :)不再:)

Scaffold(
    appBar: AppBar(
    brightness: Brightness.light,  //<--Here!!!
    title: Text(widget.title),
),

I found my question just the same as the flutter Issue#41067 ---" Flutter not automatically changing the status bar icons to black on devices running iOS 13.0 in Dark Mode it only does so when Dark Mode on iOS 13 is turned off #41067 " I found my question just the same as the flutter Issue#41067 ---" Flutter not automatically changing the status bar icons to black on devices running iOS 13.0 in Dark Mode it only does so when Dark Mode on iOS 13 is turned off #41067 "

And the Issue state is Opening, so just hope it will be resolved as soon as possible.并且问题 state 正在打开,所以希望尽快解决。 The issue link just below: flutter issue#41067问题链接如下: flutter issue#41067

Under ios/Runner in Info.plist add在 Info.plist 中的 ios/Runner 下添加

<key>UIUserInterfaceStyle</key>
<string>Light</string>
<key>UIViewControllerBasedStatusBarAppearance</key>
<true/>

it worked for me.它对我有用。

If your app is a materialApp you can set the darkTheme property of your MaterialApp:如果您的应用是 materialApp,您可以设置 MaterialApp 的darkTheme属性:

return new MaterialApp(
  darkTheme: ThemeData.dark(),
);

Here is the link to the doc:https://api.flutter.dev/flutter/material/MaterialApp-class.html这是文档的链接:https://api.flutter.dev/flutter/material/MaterialApp-class.html

If it don't solve your problem you can create your own Dark theme using:如果它不能解决您的问题,您可以使用以下方法创建自己的深色主题:

return new MaterialApp(
  darkTheme: ThemeData(
    // Your theme config
  ),
);

Link to ThemeData doc: https://api.flutter.dev/flutter/material/ThemeData-class.html链接到 ThemeData 文档: https://api.flutter.dev/flutter/material/ThemeData-class.html

import 'package:flutter_statusbarcolor/flutter_statusbarcolor.dart';

This worked for me.这对我有用。 If you want black text use:如果您想要黑色文本,请使用:

@override
  Widget build(BuildContext context) {
    FlutterStatusbarcolor.setStatusBarWhiteForeground(false);
    return MaterialApp();
  } 

For white text use:对于白色文本使用:

@override
  Widget build(BuildContext context) {
    FlutterStatusbarcolor.setStatusBarWhiteForeground(true);
    return MaterialApp();
  } 

change status bar text color/icon brightness.flutter 2.2.3更改状态栏文本颜色/图标亮度。flutter 2.2.3

MaterialApp( title: 'Flutter Demo', theme: ThemeData( appBarTheme: Theme.of(context).appBarTheme.copyWith( brightness: Brightness.dark, systemOverlayStyle: SystemUiOverlayStyle( statusBarIconBrightness: Brightness.light, ), ), primarySwatch: Colors.blue, ), home: MyHomePage(title: 'Flutter Demo Home Page'), ); MaterialApp(标题:'Flutter Demo',主题:ThemeData(appBarTheme:Theme.of(context).appBarTheme.copyWith(亮度:Brightness.dark,systemOverlayStyle:SystemUiOverlayStyle(statusBarIconBrightness:Brightness.light,),),primarySwatch:Colors。 blue, ), home: MyHomePage(title: 'Flutter Demo Home Page'), );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM