Pitu Werno
Pitu Werno

Reputation: 123

Detect if OS dark mode is active

I am looking a way to detect if OS dark mode is active, using Flutter. I just want my app to follow the OS dark mode setting. I've googling it and most of the topics are about "setting & switching" dark mode, not about detect the OS' dark mode.

Upvotes: 2

Views: 2063

Answers (2)

Hemal Moradiya
Hemal Moradiya

Reputation: 2077

You can use platformBrightness to detect current mode of OS

bool isDarkMode() {
    final darkMode = WidgetsBinding.instance.platformDispatcher.platformBrightness;
    if (darkMode == Brightness.dark) {
      return true;
    } else {
      return false;
    }
  }

Upvotes: 6

Indrajit Sharma
Indrajit Sharma

Reputation: 170

Try this inside your build method

var brightness = MediaQuery.of(context).platformBrightness;
bool darkModeOn = brightness == Brightness.dark;

Upvotes: 1

Related Questions