我想用package:flutter/services.dart
软件包改变状态栏的颜色,但它不起作用。我正在使用Mac和iOS模拟器:
- 莫哈韦10.146
- iOS 12.2模拟器/ Xr
- Flutter1.91+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
即使我把它放在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
因此,如果我想将appBar
背景色更改为白色,则会得到此结果。
我还没有在Android上测试过。这个问题是否仅与iOS模拟器有关?怎么修?
联合警察
这个问题开始让我抓狂了。
5条答案
按热度按时间0md85ypi1#
输出:
statusBarColor
只能在Android中更改,而不能在iOS中更改,如果您尝试通过一些变通方法这样做,Apple可能会拒绝您的应用程序,因为他们不希望您使用不同的AppBar
和状态栏颜色。苹果希望你坚持他们的设计,这就是为什么改变
statusBarColor
对iOS没有影响。ljsrvy3e2#
试试这个
wd2eg0qa3#
试试他们:
bbuxkriu4#
所有带有
SystemUiOverlayStyle
的答案在热重新加载时都不起作用找到了这个答案:
https://github.com/flutter/flutter/issues/65632#issuecomment-714535817
只有设置
statusBarBrightness
似乎在本地iOS运行时起作用:6ovsh4lw5#
对于每个人谁不是在AppBar工作风格设置,尝试这个