changeTheme static method

void changeTheme({
  1. required BuildContext context,
  2. required String themeKey,
})

Changes the current application theme

Dispatches AppThemeEvent.changeTheme through Bloc to update theme state

Parameters:

  • context: BuildContext for accessing Bloc provider
  • themeKey: The new theme key to apply

Throws Exception with message from ThemeExceptionConstants if Bloc operation fails

Implementation

static void changeTheme({required BuildContext context, required String themeKey}) {
  ThemeData? themeData;
  for (var i = 0; i < _appThemes.length; i++) {
    if (_appThemes[i].key == themeKey) {
      themeData = _appThemes[i].themeData;
      break;
    }
  }
  if (themeData == null) {
    throw Exception(ThemeExceptionConstants.notFoundKey);
  }
  try {
    context.read<AppThemeBloc>().add(AppThemeEvent.changeTheme(AppTheme(key: themeKey, themeData: themeData)));
  } catch (e) {
    throw Exception(ThemeExceptionConstants.appThemeMessage);
  }
}