在 Flutter 中,可以使用 Theme.of(context).brightness
属性来判断当前的模式是否为暗色模式。
下面是一个简单的例子:
if (Theme.of(context).brightness == Brightness.dark) {
// 当前为暗色模式
} else {
// 当前为亮色模式
}
如果当前的模式是暗色模式,则 Theme.of(context).brightness
的值为 Brightness.dark
;如果当前的模式是亮色模式,则 Theme.of(context).brightness
的值为 Brightness.light
。
注意:在使用 Theme.of(context).brightness
属性之前,需要确保当前的 BuildContext
已经绑定了主题。
通常情况下,可以在 MaterialApp
组件的子树中使用 Theme.of(context).brightness
属性,因为 MaterialApp
组件会为子树绑定主题。