我想用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
即使将其放在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模拟器有关吗?如何解决?
U.P.D。
这个问题开始使我发疯。
尝试一下
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text('Title'),
backgroundColor: Colors.red,
flexibleSpace: SafeArea(
child: Container(
color: Colors.blue,
),
),
),
body: Container(),
),
);
}
}
编辑:
appBar: AppBar(
elevation: 0,
brightness: Brightness.light, // this makes status bar text color black
backgroundColor: Colors.white,
)
输出:
[statusBarColor
只能在Android中进行更改,而不能在iOS中进行更改,并且如果您尝试通过某些变通办法进行更改,则Apple可能会拒绝您的应用程序,因为它们不希望您使用不同的AppBar
和状态栏颜色。] >
AppBar(backgroundColor: Colors.red) // this changes both AppBar and status bar color in iOS
Apple希望您坚持自己的设计,这就是为什么更改
statusBarColor
对iOS没有影响的原因。
尝试: