Flutter:在 iOS 中更改状态栏颜色

mr.*_*ris 6 dart flutter flutter-layout

我想用package:flutter/services.dart包更改状态栏颜色,但它不起作用。我正在使用 Mac 和 iOS 模拟器:

  • 莫哈韦沙漠 10.14.6
  • iOS 12.2 模拟器/Xr
  • Flutter 1.9.1+hotfix.2
  • 工具 • Dart 2.5.0
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {

    SystemChrome.setSystemUIOverlayStyle(
      SystemUiOverlayStyle(
        statusBarColor: Colors.red // <-- doesn't work
      )
    );

    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        // This is the theme of your application.
        //
        // Try running your application with "flutter run". You'll see the
        // application has a blue toolbar. Then, without quitting the app, try
        // changing the primarySwatch below to Colors.green and then invoke
        // "hot reload" (press "r" in the console where you ran "flutter run",
        // or simply save your changes to "hot reload" in a Flutter IDE).
        // Notice that the counter didn't reset back to zero; the application
        // is not restarted.
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}
... // other stuff
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

即使我把它放在main函数中:

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

void main() {
  SystemChrome.setSystemUIOverlayStyle(
    SystemUiOverlayStyle(
      statusBarColor: Colors.red,
    )
  );
  SystemChrome.setPreferredOrientations(
      [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown])
    .then((_) => runApp(MyApp()));
}
... // the rest code here
Run Code Online (Sandbox Code Playgroud)

因此,如果我想将appBar背景颜色更改为白色,我就会得到这个。

在此处输入图片说明

还没有在安卓上测试过。这个问题只与iOS模拟器有关吗?如何解决?

UPD

这个问题开始让我发疯。

Cop*_*oad 9

编辑:

appBar: AppBar(
  elevation: 0,
  brightness: Brightness.light, // this makes status bar text color black
  backgroundColor: Colors.white,
)
Run Code Online (Sandbox Code Playgroud)

输出:

在此处输入图片说明


statusBarColor只能在 Android 中更改,而不能在 iOS 中更改,如果您尝试通过某些解决方法尝试这样做,Apple 可能会拒绝您的应用程序,因为他们不希望您拥有不同的AppBar状态栏颜色。

AppBar(backgroundColor: Colors.red) // this changes both AppBar and status bar color in iOS
Run Code Online (Sandbox Code Playgroud)

苹果希望你坚持他们的设计,这就是为什么改变statusBarColor对 iOS 没有影响的原因。

  • 不幸的是一切都没有改变。 (2认同)
  • 谢谢。我复制了你的代码,但它没有改变,抱歉。 (2认同)
  • 好吧,我发现它在 Android 中运行良好,但在 iOS 中却不行。 (2认同)