我正在尝试改变状态栏的颜色为白色。我偶然发现了这家酒吧。我尝试在我的dart文件中使用示例代码。


当前回答

对于那些有iOS和Android颜色不一致的问题的人来说,这个方法对我来说很管用:

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

return AppBar(
      systemOverlayStyle: (Platform.isIOS)
          ? SystemUiOverlayStyle.light
          : const SystemUiOverlayStyle(
            statusBarColor: pbsBlue,
            statusBarIconBrightness: Brightness.light,
            statusBarBrightness: Brightness.light,
          ),
      backgroundColor: Colors.blue,
      title: Text('Home'),
    );

请注意,这是AppBar可重用小部件的摘录。

其他回答

 return Scaffold(
      backgroundColor: STATUS_BAR_COLOR_HERE,
      body: SafeArea(
        child: scaffoldBody(),
      ),
);

现有的解决方案对我没有帮助,因为我不使用AppBar,而且我不想在用户切换应用程序主题时做出声明。我需要一种反应式的方式来切换亮暗模式,发现AppBar使用了一个名为Semantics的小部件来设置状态栏的颜色。

基本上,我是这样做的:

return Semantics(
  container: false,  // don't make it a new node in the hierarchy
  child: AnnotatedRegion<SystemUiOverlayStyle>(
    value: SystemUiOverlayStyle.light,  // or .dark
    child: MyApp(),  // your widget goes here
  ),
);

语义从package:flutter/material.dart导入。 SystemUiOverlayStyle从package:flutter/services.dart导入。

在主要的。飞镖文件 导入服务如下

  import 'package:flutter/services.dart';

在build方法中,在返回之前添加这一行

SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
    statusBarColor: Colors.orange
)); 

是这样的:

@override
 Widget build(BuildContext context) {
   SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
       statusBarColor: CustomColors.appbarcolor
    ));
    return MaterialApp(
    home: MySplash(),
    theme: ThemeData(
      brightness: Brightness.light,
      primaryColor: CustomColors.appbarcolor,
    ),
  );
 }

最好的办法就是不用包装

Scaffold(
      extendBodyBehindAppBar: true,
      appBar: AppBar(
        toolbarHeight: 0,
        backgroundColor: Colors.transparent,
        elevation: 0,
        systemOverlayStyle: const SystemUiOverlayStyle(
          statusBarColor: Colors.transparent, // <-- SEE HERE
          statusBarIconBrightness:
              Brightness.dark, //<-- For Android SEE HERE (dark icons)
          statusBarBrightness: Brightness.light,
        ),
      ),.......

我通过改变整个背景颜色来解决这个问题,如下所示:

在主界面:

return Scaffold(
      backgroundColor: Colors.black,
      body: SafeArea(
     ),
);