Flutter 状态栏颜色与 iOS 中的 SafeArea

问题描述

我正在使用以下代码Flutter 中的状态栏着色。

void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
SystemChrome.setPreferredOrientations(
    [DeviceOrientation.portraitUp,DeviceOrientation.portraitDown]);
return AnnotatedRegion<systemUIOverlayStyle>(
  value: systemUIOverlayStyle(statusBarColor: Colors.blue),child: MaterialApp(
    title: 'My App',theme: ThemeData(
        primarySwatch: Colors.blue),home: MyHomeScreen(),navigatorKey: navigatorKey,),);
}}

class MyHomeScreen extends StatelessWidget {
const MyHomeScreen({Key key}) : super(key: key);

@override
Widget build(BuildContext context) {
return Scaffold(
  body: SafeArea(
    child: Header(),// my own custom header
  ),);
}
}

使用上面的代码,我在 Android 中得到了想要的结果,但在 iOS 中,状态栏仍然显示为白色。 我已经尝试过 this link,但我没有得到理想的 iOS 解决方案。

解决方法

我也有同样的问题。我用 this 找到了解决方案。

试试那个包。

,

将以下行添加到您的代码中,

 SystemChrome.setSystemUIOverlayStyle(
      SystemUiOverlayStyle(statusBarColor: any Color),);
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitUp,DeviceOrientation.portraitDown,]);

它会改变整个应用程序的状态栏颜色:)

更新:

你可以用这个技巧在Ios中实现状态栏颜色

@override
Widget build(BuildContext context) {
return Container(
       color: any color,child: SafeArea(
       child: Scaffold(
  body: Header(),// my own custom header
     ),),);
}