setTheme method

void setTheme(
  1. ThemeModel theme,
  2. bool notify
)

Implementation

void setTheme(ThemeModel theme, bool notify)
{
  /// Initial Theme Setting
  String def = 'light';
  String cBrightness = settings('BRIGHTNESS')?.toLowerCase() ?? def;
  if (cBrightness == 'system' || cBrightness == 'platform')
  try
  {
    cBrightness = MediaQueryData.fromWindow(WidgetsBinding.instance.window).platformBrightness.toString().toLowerCase().split('.')[1];
  }
  catch(e)
  {
    cBrightness = def;
  }

  var brightness  = theme.brightness;
  var colorscheme = theme.colorscheme;
  var font        = theme.font;

  // theme values from the app
  theme.brightness  = cBrightness;
  theme.colorscheme = settings('PRIMARY_COLOR')?.toLowerCase() ?? 'lightblue'; // backwards compatibility
  theme.colorscheme = settings('COLOR_SCHEME')?.toLowerCase()  ?? theme.colorscheme;
  theme.font        = settings('FONT');

  // has the theme changed?
  bool modified = (theme.brightness != brightness || theme.colorscheme != colorscheme || theme.font != font);
  if (modified && notify && context != null)
  {
    // call the theme notifier
    final themeNotifier = Provider.of<ThemeNotifier>(context!, listen: false);
    String brightness   = settings('BRIGHTNESS')   ?? ThemeModel.defaultBrightness;
    String color        = settings('COLOR_SCHEME') ?? ThemeModel.defaultColor;
    themeNotifier.setTheme(brightness, color);
    themeNotifier.mapSystemThemeBindables();
  }
}