【问题标题】:Flutter change status bar brightness to darkFlutter 将状态栏亮度更改为暗
【发布时间】:2019-08-08 03:17:35
【问题描述】:

我尝试了几种方法使状态栏图标变暗,但是在主页按下并返回应用程序状态栏图标后是白色的! 它似乎是它的颤振错误。

但在 iOS 中它可以正常工作。

我尝试了这些方法:

android 应用风格:

<item name="android:windowLightStatusBar">false</item>

AppBar 亮度:

brightness: Brightness.dark

颤振 API:

SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.dark.copyWith(
        statusBarIconBrightness: Brightness.dark
    ));

flutter_statusbarcolor 包:

import 'package:flutter_statusbarcolor/flutter_statusbarcolor.dart';

FlutterStatusbarcolor.setStatusBarWhiteForeground(false);

【问题讨论】:

标签: dart flutter


【解决方案1】:

将以下内容添加到您的小部件树中:

AnnotatedRegion<SystemUiOverlayStyle>(
    value: SystemUiOverlayStyle.dark,
    child: ...
)

【讨论】:

  • 这款完美适用于 iOS 和 Android 设备。
【解决方案2】:

从 Flutter 2.4.* 开始,AppBar 亮度已被弃用。要实现状态栏亮度,请将systemOverlayStyle 添加到您的AppBar,如下所示。

appBar: AppBar(
  systemOverlayStyle: SystemUiOverlayStyle(
    statusBarBrightness: Brightness.dark
  ),
)

如果您希望在整个应用程序上应用深色状态栏图标,请将appBarTheme 添加到您的 MaterialApp 主题中,如以下代码所示。您的MaterialApp 小部件位于main.dart 文件中;

MaterialApp(
  ...
  theme: ThemeData(
    appBarTheme: AppBarTheme(
      systemOverlayStyle: SystemUiOverlayStyle(
        statusBarBrightness: Brightness.dark
      ),
    ),
  )
)

【讨论】:

    【解决方案3】:
        SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
      statusBarColor: Colors.white
    ));
    

    这仅在没有应用栏时有效。如果有应用栏,这将不起作用。所以你必须先改变appbar的亮度。

    appBar: new AppBar(
            brightness: Brightness.light, // Add this line
          ),
    

    【讨论】:

      【解决方案4】:

      用 SafeArea 包裹你的小部件,并在你的 appBar 中添加亮度,它就会起作用。

        void main()
      {
        SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(statusBarColor: Colors.deepPurple,
        statusBarBrightness: Brightness.dark,
        ));
        runApp(MyApp());
      }
          return SafeArea(child: Scaffold(
            appBar: AppBar(
                toolbarHeight: 50,
                centerTitle: true,
                brightness: Brightness.dark,
                backgroundColor: Colors.deepPurple,
                child: Text("Your Text"),),
      

      【讨论】:

        【解决方案5】:

        用途:-

        systemOverlayStyle:SystemUiOverlayStyle.dark,

        代替:

        亮度:Brightness.dark,

        【讨论】:

        • 这与this other answer 中的解决方案相同。 在回答已有答案的旧问题时,请确保提供新颖的解决方案或比现有答案更好的解释。
        【解决方案6】:

        如果您想整体更改应用程序的主题,我会推荐这个类似问题的答案:https://stackoverflow.com/a/62607827/15605135

        如果你想改变单个 AppBar,你可以尝试使用 systemOverlayStyle 属性:

          @override
          Widget build(BuildContext context) {
            return Scaffold(
              appBar: AppBar(
                systemOverlayStyle: SystemUiOverlayStyle.dark,
              ),
            );
          }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-01-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-12-10
          相关资源
          最近更新 更多