ThemeManager constructor

ThemeManager({
  1. List<ThemeData>? themes,
  2. Color? statusBarColorBuilder(
    1. ThemeData?
    )?,
  3. Color? navigationBarColorBuilder(
    1. ThemeData?
    )?,
  4. ThemeData? darkTheme,
  5. ThemeData? lightTheme,
  6. ThemeMode defaultTheme = ThemeMode.system,
})

Implementation

ThemeManager({
  this.themes,
  this.statusBarColorBuilder,
  this.navigationBarColorBuilder,
  this.darkTheme,
  this.lightTheme,
  this.defaultTheme = ThemeMode.system,
}) {
  var hasMultipleThemes = themes != null && themes!.length > 1;
  var hasLightAndDarkThemes = darkTheme != null && lightTheme != null;
  assert(hasMultipleThemes || hasLightAndDarkThemes,
      '''You have to supply themes if you want to use themes. You have supplied no themes. Don\'t do that. Supply themes.
You can supply either a list of ThemeData objects to the themes property or a lightTheme and a darkTheme to be swapped between.
      ''');

  var storedThemeIndex = _sharedPreferences.themeIndex;

  ThemeData? selectedTheme;

  if (hasMultipleThemes) {
    if (storedThemeIndex != null) {
      try {
        selectedTheme = themes![storedThemeIndex];
      } catch (e) {
        print(
            '''WARNING: You have changed your number of themes. Because of this we will clear your previously selected
      theme and broadcast the first theme in your list of themes.''');
        _sharedPreferences.themeIndex = null;
        selectedTheme = themes!.first;
      }
    } else {
      selectedTheme = themes!.first;
    }
    updateOverlayColors(selectedTheme);
  } else {
    _selectedThemeMode = defaultTheme;

    var savedUserThemeMode = _sharedPreferences.userThemeMode;
    if (savedUserThemeMode != null) {
      _selectedThemeMode = savedUserThemeMode;
    }

    if (_selectedThemeMode == ThemeMode.system) {
      final brightness = ambiguate(SchedulerBinding.instance)!
          .platformDispatcher
          .platformBrightness;
      selectedTheme = brightness == Brightness.dark ? darkTheme : lightTheme;
    } else {
      selectedTheme =
          _selectedThemeMode == ThemeMode.dark ? darkTheme : lightTheme;
    }

    updateOverlayColors(selectedTheme);
  }

  ThemeModel _currTheme = ThemeModel(
      selectedTheme: selectedTheme,
      darkTheme: darkTheme,
      themeMode: _selectedThemeMode);

  _themesController = BehaviorSubject<ThemeModel>.seeded(_currTheme);
  _initialTheme = _currTheme;

  ThemeService.getInstance().setThemeManager(this);
}