简体   繁体   English

如何使用 AnnotatedRegion 更改状态栏颜色?

[英]How to change status bar color using AnnotatedRegion?

I want to change color of status bar using AnnotatedRegion , I got the below code from this answer but it is having no effect on status bar color.我想使用AnnotatedRegion更改状态栏的颜色,我从这个答案中得到了以下代码,但它对状态栏颜色没有影响。

AnnotatedRegion<SystemUiOverlayStyle>(
      value: SystemUiOverlayStyle.light.copyWith(statusBarColor: Colors.white),
      child: Scaffold(
        appBar: AppBar(
          title: Text('Annotated Region'),
        ),
        body: Center(
          child:
              Text('Status Bar!'),
        ),
      ),
    )

To quickly get started with this code, you can use this Github repository .要快速开始使用此代码,您可以使用此 Github 存储库

If you check the code of the Appbar you will see it uses it's own AnnotatedRegion如果您检查 Appbar 的代码,您会看到它使用了自己的 AnnotatedRegion

final SystemUiOverlayStyle overlayStyle = brightness == Brightness.dark
      ? SystemUiOverlayStyle.light
      : SystemUiOverlayStyle.dark;

return Semantics(
      container: true,
      child: AnnotatedRegion<SystemUiOverlayStyle>(
        value: overlayStyle,
        child: Material(
          color: widget.backgroundColor
            ?? appBarTheme.color
            ?? theme.primaryColor,
          elevation: widget.elevation
            ?? appBarTheme.elevation
            ?? _defaultElevation,
          shape: widget.shape,
          child: Semantics(
            explicitChildNodes: true,
            child: appBar,
          ),
        ),
      ),
    );

to override the effect of the Appbar you will need to wrap your scaffold with a SafeArea, then you will see the change of your own AnnotatedRegion要覆盖 Appbar 的效果,您需要用 SafeArea 包裹脚手架,然后您将看到自己的 AnnotatedRegion 的变化

return AnnotatedRegion<SystemUiOverlayStyle>(
      value: SystemUiOverlayStyle.light.copyWith(statusBarColor: Colors.white),
      child: SafeArea(
        child: Scaffold(
          primary: false,
          appBar: AppBar(
            title: Text('Annotated Region'),
          ),
          body: Center(
            child: Text('Status Bar!'),
          ),
        )
      )
    );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM