无法在Flutter应用程序中更改状态栏图标亮度?

6

在 main.dart 中

void main() {
SystemChrome.setSystemUIOverlayStyle(
SystemUiOverlayStyle(
  statusBarColor: Colors.black.withOpacity(0), //top bar color
  statusBarIconBrightness: Brightness.dark, //top bar icons
  systemNavigationBarColor: Colors.black, //bottom bar color
  systemNavigationBarIconBrightness: Brightness.light, //bottom bar icons
),
);
runApp(MyApp());
}

class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
 Widget build(BuildContext context) {
return MaterialApp(
  theme: ThemeData(
    //primaryColor: Color.fromRGBO(113, 201, 206, 1),
    fontFamily: 'Montserrat',
    textTheme: TextTheme(
      headline: TextStyle(
        fontSize: 40,
        fontWeight: FontWeight.w500,
      ),
      title: TextStyle(
        fontWeight: FontWeight.w700,
        fontSize: 16,
      ),
      body1: TextStyle(
        fontSize: 12,
      ),
    ),
  ),
  home: Test(),
  routes: {
    MainScreen.routeName: (context) => MainScreen(),
  },
);
}
}

我正在使用上述代码来改变状态栏的颜色,但是statusBarIconBrightness: Brightness.dark没有效果。 它会变成黑色,但几秒钟后又会切换回 `Brightness.light'。 问题出在哪里?

在 Android 模拟器上运行。 我不希望在 Test() 小部件中有appBar


你的代码很完美,但在返回脚手架之前,请尝试在MyApp类中使用你上面的代码。 - Jay Gadariya
你是在模拟器上试吗?还是真实设备上。如果是模拟器的话,是安卓还是苹果。这只能在安卓上运行。 - HaSnen Tai
问题已更新。 - rahul Kushwaha
4个回答

8

这是因为您在Test页面中没有显式更改AppBarbrightness。运行此代码并查看差异:

void main() {
  SystemChrome.setSystemUIOverlayStyle(
    SystemUiOverlayStyle(
      statusBarColor: Colors.black.withOpacity(0), //top bar color
       // statusBarIconBrightness: Brightness.dark, // don't use this
    ),
  );
  runApp(MyApp2());
}

class MyApp2 extends StatelessWidget {
// This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text("AppBar"),
          brightness: Brightness.light, // use this instead
        ),
      ),
    );
  }
}

输出-1(Brightness.dark):

输入图像描述

输出-2(Brightness.light):

输入图像描述


我的 test widget 中没有 appBar,但它仍然无法工作! - rahul Kushwaha
你的,不工作,Test小部件是一个非常复杂的小部件,没有appBar。还有一件事,最初设置为Brightness.light,但当我在应用程序的根目录中设置PrimaryColor时,它会更改为Brightnesss.dark并且不会更改。 - rahul Kushwaha
我不确定为什么我的解决方案没有起作用,它非常简单和直接, 请不要做任何更改,只需放入并运行它。它会起作用的。 - CopsOnRoad
@CopsOnRoad,这个解决方案只适用于包含应用栏的情况吗?如果我们不需要应用栏怎么办? - B.shruti

1
我也尝试了类似的东西,但状态栏是透明的而不是蓝色的。 我添加了以下内容,然后它就起作用了:
theme: ThemeData(
  appBarTheme: AppBarTheme(
    brightness: Brightness.light,

0

我认为是 systemNavigationBarColor 导致 statusBarIconBrightness 没有效果。


0

@CopsOnRoad的答案对我有用,只是稍微有些变化。你知道,有时候Brightness类会以一种有趣的方式造成混淆。也就是说,如果你想让状态栏图标颜色为白色,你必须选择Brightness.dark而不是Brightness.light。我就是这样做的,然后它就开始工作了。只需采用@CopsOnRoad的答案,并将light更改为dark。至少对我来说是有效的。

我得出结论,在这种特殊情况下,设置Brightness.light意味着“将状态栏图标的颜色更改为适合浅色状态栏(白色或类似颜色的状态栏背景)的某种颜色”,这意味着系统将将图标设置为黑色,以便用户能够看到图标。
希望这可以帮助到你!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接