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

后端 未结 17 654
孤城傲影
孤城傲影 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:36

    First, import services package:

    import 'package:flutter/services.dart';
    

    Next, simply put this in the build function of your App:

    SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
      statusBarColor: Colors.blue, //or set color with: Color(0xFF0000FF)
    ));
    

    Additionally, you can set useful properties like: statusBarIconBrightness, systemNavigationBarColor or systemNavigationBarDividerColor


    If you prefer a more flutter/widget way of doing the same thing, consider using the AnnotatedRegion<SystemUiOverlayStyle> widget.

    The value: property can be set to a SystemUiOverlayStyle() object containing the same properties as shown above.


    For more infos, head over to the API Docs

    0 讨论(0)
  • 2020-12-08 00:39

    I tried many methods but they didn’t work. And i found another method, use safeArea ,AnnotatedRegion and Scaffold

    AnnotatedRegion(
      // status icon and text color, dark:black  light:white
      value: SystemUiOverlayStyle.dark,
      child: Scaffold(
         // statusbar color
         backgroundColor: Colors.white,
         body : SafeArea(****)
      )
    }
    

    The code implements the white status bar and black text

    0 讨论(0)
  • 2020-12-08 00:42

    If you dont want to use app bar, then

    appBar: AppBar(
          elevation: 0,
          backgroundColor: Colors.white, # status bar color
          toolbarHeight: 0,
          brightness: Brightness.light # or Brightness.dark
    
    0 讨论(0)
  • 2020-12-08 00:43

    On Android, add the following to onCreate in MainActivity.java, after the call to super.onCreate(savedInstanceState);

    getWindow().setStatusBarColor(0x00000000);

    or you can use the the flutter_statusbarcolor plugin

       changeStatusColor(Color color) async {
        try {
          await FlutterStatusbarcolor.setStatusBarColor(color);
        } on PlatformException catch (e) {
          print(e);
        }
      }
    

    Sample project

    0 讨论(0)
  • 2020-12-08 00:44

    Simply add this to your build function, or main function.

    import 'package:flutter/services.dart';
    
    Widget build(BuildContext context) {
      SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light);
      ...
    }
    
    0 讨论(0)
  • 2020-12-08 00:45

    Use the following in your main function to change the status bar color for all the views/screens. This will work even without an app bar.

    WidgetsFlutterBinding.ensureInitialized();
    SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
      statusBarIconBrightness: Brightness.dark, // this will change the brightness of the icons
      statusBarColor: Colors.white, // or any color you want
    ));
    
    0 讨论(0)
提交回复
热议问题