我想为每个屏幕设置状态栏颜色。例如。屏幕A状态栏中的颜色为红色,屏幕B中的状态为蓝色。我尝试了许多解决方案。但是每个人都有自己的问题。像:
AnnotatedRegion(
value: SystemUiOverlayStyle(
systemNavigationBarColor: navigationBarColor,
statusBarColor: statusBarColor,
),
child: child,
);
或在构建方法中返回之前的此:
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
statusBarColor: Colors.white
));
我最后的尝试:
static void pushNamed({
BuildContext context,
String route,
Object arguments,
Color statusBarColor,
Color navigationBarColor = Colors.black,
}) async {
if (statusBarColor != null) {
await FlutterStatusbarcolor.setStatusBarColor(statusBarColor);
}
await Navigator.pushNamed(
context,
route,
arguments: arguments,
);
}
但有时会失败。这个解决方案的另一个问题是使用后退按钮时,颜色没有改变(我在appbar中为后退按钮编写了代码)。甚至在热重装时,颜色也会更改为以前的颜色。我该如何解决这个问题?
您可以使用AppBar,但将其高度和高度设置为0,以便它仅覆盖状态栏并且没有阴影。
appBar: PreferredSize(
preferredSize: Size.fromHeight(0),
child: new AppBar(
brightness: Brightness.light,
backgroundColor: backgroundColour,
elevation: 0,
)
),
然后在initState()
中,您可以使状态栏透明,以免变色。