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

后端 未结 17 655
孤城傲影
孤城傲影 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 00:54

    you just have to add this if you want to use as default template in the MaterialApp Theme :

    appBarTheme: AppBarTheme(brightness: Brightness.light)
    

    Result will be like this :

    return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
            appBarTheme: AppBarTheme(brightness: Brightness.light),  <========
            visualDensity: VisualDensity.adaptivePlatformDensity,
          ),
          home: ...,
        );
    
    0 讨论(0)
  • 2020-12-08 00:58

    you should solve this question in two ways:

    1. you did not set appBar then you just write in this way:
    SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.dark.copyWith(
      statusBarColor: Colors.black, 
    ));
    
    

    or

    SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light.copyWith(
      statusBarColor: Colors.white, 
    ));
    
    1. you always set appBar so you should set appBar but not upon code:
    Scaffold(
      appBar: AppBar(
        brightness: Brightness.light,
      )
    )
    

    or

    Scaffold(
      appBar: AppBar(
        brightness: Brightness.dark,
      )
    )
    
    0 讨论(0)
  • 2020-12-08 01:00

    You can use SystemChrome class to change Status bar and Navigation bar color. First import

    import 'package:flutter/services.dart';
    

    After this, you need to add following lines (better place to put these lines is in your main() method)

    void main() {
      SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
        systemNavigationBarColor: Colors.blue, // navigation bar color
        statusBarColor: Colors.pink, // status bar color
      ));
    }
    
    0 讨论(0)
  • 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<SystemUiOverlayStyle>(
            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);
    }
    
    0 讨论(0)
  • 2020-12-08 01:02

    this best status bar like default material design theme without AppBar()

     Container(width: MediaQuery.of(context).size.width, height: MediaQuery.of(context).padding.top, color: Theme.of(context).accentColor)
    
    0 讨论(0)
提交回复
热议问题