Flutter - 如何在AppBar不存在时设置状态栏颜色

Raf*_*san 30 statusbar dart flutter

如何在AppBar不存在时设置状态栏颜色.

我试过这个但没有用.

Widget build(BuildContext context) {
    SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.dark);
    return new Scaffold(
        body: new Container(
        color: UniQueryColors.colorBackground,
        child: new ListView.builder(
           itemCount: 7,
           itemBuilder: (BuildContext context, int index){
             if (index == 0){
               return addTopInfoSection();
             }
           },
        ),
       ),
    );
}
Run Code Online (Sandbox Code Playgroud)

输出看起来像这样:

在此输入图像描述

Han*_* .T 61

简单地把它放在你的应用程序的构建功能中:

import 'package:flutter/services.dart';
Run Code Online (Sandbox Code Playgroud)

此外,您可以设置有用的变量,如:services,statusBarIconBrightnesssystemNavigationBarColor

  • 这对于导航来说效果不佳,或者在某些情况下似乎根本不起作用。我用“Jordy Sinke”的答案添加了更好的结果,这也让人感觉更飘逸。 (6认同)

Jor*_*rdy 24

如果您掠夺了AppBar 的源代码,则可以看到它们使用了AnnotatedRegion小部件。

据我了解,当包装在其中的小部件被状态栏/导航栏覆盖时,此小部件会自动设置状态栏/导航栏颜色。

您可以像这样包装小部件:

import 'package:flutter/services.dart';

...    

Widget build(BuildContext context) {
   return Scaffold(
      body: AnnotatedRegion<SystemUiOverlayStyle>(
         value: SystemUiOverlayStyle.light,                
         child: ...,
      ),
   );
}
Run Code Online (Sandbox Code Playgroud)

  • 这应该是公认的答案,因为它与框架处理它的方式一致 (4认同)
  • 替换它以更改状态栏颜色 `value: SystemUiOverlayStyle.light.copyWith( statusBarColor: Theme.of(context).primaryColor, )` (4认同)
  • 同意 - 如果上一页使用了不同的值,它还可以处理页面转换回来 (2认同)

小智 8

正如已经提到的解决方案,我正在以不同的方法实施它。遵循的方法是删除AppBar并使用Container小部件更改状态栏的颜色。

void main() {
  runApp(
    MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Test',
      home: Scaffold(
        primary: true,
        appBar: EmptyAppBar(),
        body: MyScaffold(),
      ),
    ),
  );
}

class MyScaffold extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Text(
        'Test',
      ),
    );
  }
}

class EmptyAppBar extends StatelessWidget implements PreferredSizeWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.black,
    );
  }

  @override
  Size get preferredSize => Size(0.0, 0.0);
}
Run Code Online (Sandbox Code Playgroud)
  • 在这里,我使用EmptyAppBar类来删除默认情况下存在于Scaffold 中AppBar
  • EmptyAppBar类中,我们可以在容器小部件中选择所需的颜色。
  • 之后,您将拥有自己的自定义MyScaffold类来创建您的小部件。在我的代码中,我创建了一个文本。

参考:GitHub 问题

  • 我发现对于将屏幕与“AppBar”结合起来的应用程序来说,这是最佳解决方案。“SystemUiOverlayStyle”方法会覆盖“AppBar”设置,并且在离开屏幕时不会重置。 (2认同)
  • 这是唯一对我有用的解决方案,谢谢 (2认同)

小智 7

在搜索 SystemChrome 时,我发现了这个:https ://docs.flutter.io/flutter/services/SystemChrome/setSystemUIOverlayStyle.html

示例代码的正上方是一段关于AppBar.brightness.

您应该能够像这样添加一个 AppBar:

Widget build(BuildContext context) {
    SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.dark);
    return new Scaffold(
        appBar: new AppBar(
            title: new Text('Nice title!'),
            brightness: Brightness.light,
        ),
        body: new Container(
        color: UniQueryColors.colorBackground,
        child: new ListView.builder(
           itemCount: 7,
           itemBuilder: (BuildContext context, int index){
             if (index == 0){
               return addTopInfoSection();
             }
           },
        ),
       ),
    );
}
Run Code Online (Sandbox Code Playgroud)

这里是关于亮度的信息


zha*_*eng 6

你应该通过两种方式解决这个问题:

  1. 你没有设置 appBar 那么你就这样写:
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.dark.copyWith(
  statusBarColor: Colors.black, 
));

Run Code Online (Sandbox Code Playgroud)

或者

SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light.copyWith(
  statusBarColor: Colors.white, 
));
Run Code Online (Sandbox Code Playgroud)
  1. 你总是设置 appBar 所以你应该设置 appBar 但不是在代码上:
Scaffold(
  appBar: AppBar(
    brightness: Brightness.light,
  )
)
Run Code Online (Sandbox Code Playgroud)

或者

Scaffold(
  appBar: AppBar(
    brightness: Brightness.dark,
  )
)
Run Code Online (Sandbox Code Playgroud)


Shy*_*hil 5

在Android上,在对super.onCreate(savedInstanceState)的调用之后,将以下内容添加到MainActivity.java中的 onCreate :

getWindow()。setStatusBarColor(0x00000000);

或者您可以使用flutter_statusbarcolor插件

   changeStatusColor(Color color) async {
    try {
      await FlutterStatusbarcolor.setStatusBarColor(color);
    } on PlatformException catch (e) {
      print(e);
    }
  }
Run Code Online (Sandbox Code Playgroud)

样例项目


Cop*_*oad 5

使用

AppBar(
  systemOverlayStyle: SystemUiOverlayStyle(statusBarColor: Colors.orange),
)
Run Code Online (Sandbox Code Playgroud)

或者

AppBar(
  systemOverlayStyle: SystemUiOverlayStyle(statusBarColor: Colors.orange),
)
Run Code Online (Sandbox Code Playgroud)


小智 5

我尝试了很多方法,但它们没有\xe2\x80\x99 工作。我找到了另一种方法,使用 safeArea ,AnnotatedRegion 和 Scaffold

\n\n
AnnotatedRegion(\n  // status icon and text color, dark:black  light:white\n  value: SystemUiOverlayStyle.dark,\n  child: Scaffold(\n     // statusbar color\n     backgroundColor: Colors.white,\n     body : SafeArea(****)\n  )\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

代码实现白色状态栏和黑色文本

\n