0

I want to change the color of the system navigation and status bar when entering the dark mode.

This is my code:

void main() async {
SystemChrome.setSystemUIOverlayStyle(
SystemUiOverlayStyle(
  statusBarBrightness: Brightness.dark,
  statusBarColor: Colors.white,
  statusBarIconBrightness: Brightness.dark,
  systemNavigationBarColor: Colors.white,
  systemNavigationBarDividerColor: Colors.white,
  systemNavigationBarIconBrightness: Brightness.dark,
),
);
...
}

I use SystemChrome and it works fine. But my problem is, I can only change the color manual via code, not when switching to dark or light mode or when I use the system settings.

2
  • Where are you testing it, I mean device web? And why not use themeMode on MaterialApp ? Commented Oct 16, 2021 at 18:50
  • It is an android application and I also use a themeMode inside my MaterialApp for changing dark and light mode. This works, but I can not figure out, how to do the same for the navigation and status bar. Commented Oct 16, 2021 at 21:07

3 Answers 3

2

After trying many things, I found the following solution to my problem. I don't know if it's the cleanest solution, but it works. This solution is interesting for those who don't use an AppBar like in my case.

The trick is as follows ...

@override
Widget build(BuildContext context) {
 Theme.of(context).scaffoldBackgroundColor == Colors.white
    ? _lightStatusAndNavigationBar()
    : _darkStatusAndNavigationBar();
return Scaffold(
      backgroundColor: Theme.of(context).scaffoldBackgroundColor,
...
}

The query is in the build of my class, in which I have the NavigationBar of my app. All other screens are connected there.

Using Theme.of (context) .scaffoldBackgroundColor, I ask which design should be used for the status and navigation bar.

These are the two functions for the different colors:

void _darkStatusAndNavigationBar() {
SystemChrome.setSystemUIOverlayStyle(
  SystemUiOverlayStyle(
    statusBarBrightness: Brightness.light,
    statusBarColor: Colors.grey.shade900,
    statusBarIconBrightness: Brightness.light,
    systemNavigationBarColor: Colors.grey.shade900,
    systemNavigationBarDividerColor: Colors.grey.shade900,
    systemNavigationBarIconBrightness: Brightness.light,
  ),
);
}

 void _lightStatusAndNavigationBar() {
SystemChrome.setSystemUIOverlayStyle(
  SystemUiOverlayStyle(
    statusBarBrightness: Brightness.dark,
    statusBarColor: Colors.white,
    statusBarIconBrightness: Brightness.dark,
    systemNavigationBarColor: Colors.white,
    systemNavigationBarDividerColor: Colors.white,
    systemNavigationBarIconBrightness: Brightness.dark,
  ),
);
}

If something is still incomprehensible, just ask. :)

Sign up to request clarification or add additional context in comments.

Comments

1

You can change the theme like this.

MaterialApp(
    themeMode: ThemeMode.light, // Change it as you want
    theme: ThemeData(
        primaryColor: Colors.white,
        primaryColorBrightness: Brightness.light,
        brightness: Brightness.light,
        primaryColorDark: Colors.black,
        canvasColor: Colors.white,
        // next line is important!
        appBarTheme: AppBarTheme(brightness: Brightness.light)),
    darkTheme: ThemeData(
        primaryColor: Colors.black,
        primaryColorBrightness: Brightness.dark,
        primaryColorLight: Colors.black,
        brightness: Brightness.dark,
        primaryColorDark: Colors.black,      
        indicatorColor: Colors.white,
        canvasColor: Colors.black,
        // next line is important!
        appBarTheme: AppBarTheme(brightness: Brightness.dark)),
...

2 Comments

Thanks for your code. I am doing it exactly this way how you show it. But I found out, that inside my app only the SystemChrome function works for all screens. But sure, I will try this out and will give feedback. :)
The problem is. I do not use an appBar inside my app. In no screen. And I will change the color of the StatusBar and the NavigationBar of the system. It should look like on Instagram. The hole screen should be covered with the background color.
1

Consider using an AnnotatedRegion widget.

@override
Widget build(BuildContext context) {
    return AnnotatedRegion<SystemUiOverlayStyle>(
        value: getSystemUIOverlayStyle(context),
        child: Scaffold(
            appBar: _appBar(context),
            body: _body(context),
        ),
    );
}

And the getSystemUIOverlayStyle method based of context.

SystemUiOverlayStyle getSystemUIOverlayStyle(BuildContext context) {
    final theme = Theme.of(context);
    return SystemUiOverlayStyle(
      systemNavigationBarColor: theme.colorScheme.surface,
      systemNavigationBarDividerColor: theme.colorScheme.surface,
      systemNavigationBarIconBrightness: theme.brightness == Brightness.light ? Brightness.dark : Brightness.light,
    );
}

More details https://api.flutter.dev/flutter/services/SystemChrome/setSystemUIOverlayStyle.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.