show static method

Future<Future<void> Function()> show({
  1. String? locale,
  2. String? theme,
})

Shows locale (a language code like es) and theme (a theme id), and returns a function that puts back the ones shown before. A null argument keeps the current one.

Throws a StateError when the locale has no language file or no theme has that id. Nothing is saved, so the user's own choices are kept.

Implementation

static Future<Future<void> Function()> show({
  String? locale,
  String? theme,
}) async {
  final NyLocalization localization = NyLocalization.instance;
  final NyThemeManager themes = NyThemeManager.instance;
  final Locale localeBefore = localization.locale;
  final String themeBefore = themes.currentThemeId;
  final bool changeLocale =
      locale != null && locale != localeBefore.languageCode;
  final bool changeTheme = theme != null && theme != themeBefore;

  if (changeTheme && themes.getThemeById(theme) == null) {
    final List<String> ids = themeIds;
    throw StateError(
      ids.isEmpty
          ? 'No themes are registered.'
          : 'No theme with id "$theme". Registered themes: '
                '${ids.join(', ')}.',
    );
  }
  if (changeLocale) {
    final String file =
        '${localization.assetsDirectory ?? 'lang/'}$locale.json';
    if (!await _exists(file)) {
      throw StateError(
        'Couldn\'t switch to "$locale". Check that $file exists and is '
        'listed in your assets.',
      );
    }
    await localization.setLocale(locale: Locale(locale));
    NyApp.refreshLocale();
  }
  if (changeTheme) themes.showTheme(theme);

  return () async {
    if (changeTheme && themeBefore.isNotEmpty) themes.showTheme(themeBefore);
    if (changeLocale) {
      await localization.setLocale(locale: localeBefore);
      NyApp.refreshLocale();
    }
  };
}