getBrightness static method
Gets the brightness (light/dark mode) from the theme.
Tries to get brightness from:
- CupertinoTheme (if CupertinoApp is present)
- Material Theme (if MaterialApp is present)
- System brightness (fallback)
Implementation
static Brightness getBrightness(BuildContext context) {
try {
final cupertinoTheme = CupertinoTheme.of(context);
final brightness = cupertinoTheme.brightness;
// Don't fall back to Brightness.light here — if brightness is null the
// CupertinoTheme hasn't been explicitly set, so cascade to the actual
// system/material brightness instead.
if (brightness != null) return brightness;
} catch (_) {
// CupertinoApp not in tree, fall through to Material / system checks.
}
// Try Material theme next
try {
return Theme.of(context).brightness;
} catch (_) {}
// Final fallback: real system brightness
return MediaQuery.of(context).platformBrightness;
}