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;
    // 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;
}