getBrightness static method

Brightness getBrightness(
  1. BuildContext context
)

Gets the brightness (light/dark mode) from the theme.

Tries to get brightness from:

  1. CupertinoTheme (if CupertinoApp is present)
  2. Material Theme (if MaterialApp is present)
  3. 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;
    }
  }
}