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;
return brightness ?? Brightness.light;
} catch (_) {
// CupertinoApp not in tree, try Material theme
try {
final materialTheme = Theme.of(context);
return materialTheme.brightness;
} catch (_) {
// No theme found, use system brightness
return MediaQuery.of(context).platformBrightness;
}
}
}