Flutter - How to set status bar color when AppBar not present

后端 未结 17 815
孤城傲影
孤城傲影 2020-12-07 23:56

How to set status bar color when AppBar not present.

I have tried this but not working.

Widget build(BuildContext context) {
    SystemChrome.setSys         


        
17条回答
  •  臣服心动
    2020-12-08 01:01

    Use EmptyAppBar, with some code for restoring color as in AppBar.

    class EmptyAppBar  extends StatelessWidget implements PreferredSizeWidget {
      static const double _defaultElevation = 4.0;
      @override
      Widget build(BuildContext context) {
        final ThemeData themeData = Theme.of(context);
        final AppBarTheme appBarTheme = AppBarTheme.of(context);
        final Brightness brightness = appBarTheme.brightness
            ?? themeData.primaryColorBrightness;
        final SystemUiOverlayStyle overlayStyle = brightness == Brightness.dark
            ? SystemUiOverlayStyle.light
            : SystemUiOverlayStyle.dark;
    
        return Semantics(
          container: true,
          child: AnnotatedRegion(
            value: overlayStyle,
            child: Material(
              color: appBarTheme.color
                  ?? themeData.primaryColor,
              elevation: appBarTheme.elevation
                  ?? _defaultElevation,
              child: Semantics(
                explicitChildNodes: true,
                child: Container(),
              ),
            ),
          ),
        );
      }
      @override
      Size get preferredSize => Size(0.0,0.0);
    }
    

提交回复
热议问题