mr.*_*ris 6 dart flutter flutter-layout
我想用package:flutter/services.dart包更改状态栏颜色,但它不起作用。我正在使用 Mac 和 iOS 模拟器:
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
这个问题开始让我发疯。
编辑:
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 没有影响的原因。
| 归档时间: |
|
| 查看次数: |
6100 次 |
| 最近记录: |