changeLocale method
Change the current locale and persist the selection
This method:
- Validates that the new locale is supported
- Updates the current state with the new locale
- Persists the selection to local storage for future app launches
- Notifies all listeners of the change
If the locale is not supported, the change is ignored and the current locale remains active.
Example usage:
// Switch to Arabic
await notifier.changeLocale(const Locale('ar'));
// Switch to English
await notifier.changeLocale(const Locale('en'));
// Invalid locale (will be ignored)
await notifier.changeLocale(const Locale('fr')); // No effect if French not supported
@param locale The new locale to switch to
Implementation
Future<void> changeLocale(Locale locale) async {
if (_isSupportedLocale(locale)) {
dump(locale.languageCode, tag: 'changeLocale');
state = locale;
await JetStorage.write(_localeStorageKey, locale.languageCode);
}
}