如何使用 Flutter 检查 iOS/Android 上是否启用了暗模式? [英] How to check if dark mode is enabled on iOS/Android using Flutter?

查看:44
本文介绍了如何使用 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;
    

  • 这篇关于如何使用 Flutter 检查 iOS/Android 上是否启用了暗模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆