如何使用 Flutter 检查 iOS/Android 上是否启用了暗模式?

如何使用 Flutter 检查 iOS/Android 上是否启用了暗模式?

问题描述:

如何使用 Flutter 检查 Android Q 中是否启用了暗模式?
我知道如何设置暗模式,但我没有找到一种检查背景主题的方法.
这是设置深色主题的代码.

How can I check if dark mode is enabled in Android Q with Flutter?
I know how to set the dark mode, but I didn't find a way to check the background theme.
Here is the code to set the dark theme.

darkTheme: ThemeData.dark(),

有两种方式:

  1. 不需要上下文.可以在 initState 中使用,例如:

 var brightness = SchedulerBinding.instance!.window.platformBrightness;
 bool isDarkMode = brightness == Brightness.dark;

  • context 是必需的:

  • context is required:

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