LocaleProvider class

A provider class that manages the application's locale settings.

This class extends ChangeNotifier to provide reactive locale state management throughout the Flutter application. It handles locale persistence using SharedPreferences and ensures only supported locales are applied.

The provider initializes with a saved locale preference or defaults to the first supported locale. It provides methods to change the current locale and retrieve human-readable language names.

Example usage:

// In your widget:
Consumer<LocaleProvider>(
  builder: (context, localeProvider, child) {
    return Text('Current locale: ${localeProvider.locale}');
  },
)
Inheritance

Constructors

LocaleProvider({required SharedPreferences prefs})
Creates a LocaleProvider instance with the given SharedPreferences.

Properties

hashCode int
The hash code for this object.
no setterinherited
hasListeners bool
Whether any listeners are currently registered.
no setterinherited
locale Locale?
The currently selected locale for the application.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
supportedLocales List<Locale>
List of supported locales from AppLocalizations.
no setter

Methods

addListener(VoidCallback listener) → void
Register a closure to be called when the object changes.
inherited
dispose() → void
Discards any resources used by the object. After this is called, the object is not in a usable state and should be discarded (calls to addListener will throw after the object is disposed).
inherited
getLanguageName(Locale locale, BuildContext context) String
Returns a human-readable name for the given locale.
init() Future<void>
Initializes the locale provider by loading the saved locale preference.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
notifyListeners() → void
Call all the registered listeners.
inherited
removeListener(VoidCallback listener) → void
Remove a previously registered closure from the list of closures that are notified when the object changes.
inherited
setLocale(Locale newLocale) Future<void>
Sets a new locale for the application.
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited