-
Notifications
You must be signed in to change notification settings - Fork 46
fix: persist selected language across app restarts #316
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,19 +1,49 @@ | ||||||||||||||||||
| import 'package:flutter/material.dart'; | ||||||||||||||||||
| import 'package:shared_preferences/shared_preferences.dart'; | ||||||||||||||||||
|
Comment on lines
1
to
+2
|
||||||||||||||||||
|
|
||||||||||||||||||
| /// A provider class for managing the application's locale. | ||||||||||||||||||
| /// | ||||||||||||||||||
| /// This class extends [ChangeNotifier] to allow widgets to listen for | ||||||||||||||||||
| /// changes in the locale and rebuild accordingly. | ||||||||||||||||||
| class LocaleProvider with ChangeNotifier { | ||||||||||||||||||
| static const String _localeKey = 'app_locale'; | ||||||||||||||||||
|
|
||||||||||||||||||
| Locale _locale = const Locale('en'); | ||||||||||||||||||
|
|
||||||||||||||||||
| Locale get locale => _locale; | ||||||||||||||||||
|
|
||||||||||||||||||
| /// Sets the application's locale. | ||||||||||||||||||
| /// Loads the saved locale from local storage (SharedPreferences). | ||||||||||||||||||
| /// Call this once when the provider is created. | ||||||||||||||||||
| Future<void> loadSavedLocale() async { | ||||||||||||||||||
| final prefs = await SharedPreferences.getInstance(); | ||||||||||||||||||
| final code = prefs.getString(_localeKey); | ||||||||||||||||||
|
|
||||||||||||||||||
| if (code == null || code.isEmpty) return; | ||||||||||||||||||
|
|
||||||||||||||||||
| _locale = Locale(code); | ||||||||||||||||||
| notifyListeners(); | ||||||||||||||||||
| } | ||||||||||||||||||
|
Comment on lines
+17
to
+25
|
||||||||||||||||||
|
|
||||||||||||||||||
| /// Sets the application's locale and persists it. | ||||||||||||||||||
| /// | ||||||||||||||||||
| /// When the locale is changed, it notifies all listening widgets to rebuild. | ||||||||||||||||||
| void setLocale(Locale newLocale) { | ||||||||||||||||||
| Future<void> setLocale(Locale newLocale) async { | ||||||||||||||||||
| _locale = newLocale; | ||||||||||||||||||
|
|
||||||||||||||||||
| final prefs = await SharedPreferences.getInstance(); | ||||||||||||||||||
| await prefs.setString(_localeKey, newLocale.languageCode); | ||||||||||||||||||
|
|
||||||||||||||||||
|
Comment on lines
+23
to
+35
|
||||||||||||||||||
| notifyListeners(); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| /// Optional: Reset to system/default behavior. | ||||||||||||||||||
| /// Use this if you provide a "System Default" option in UI. | ||||||||||||||||||
| Future<void> clearLocale() async { | ||||||||||||||||||
| _locale = const Locale('en'); // or set to null if your app supports null locale | ||||||||||||||||||
|
Comment on lines
+39
to
+42
|
||||||||||||||||||
| /// Optional: Reset to system/default behavior. | |
| /// Use this if you provide a "System Default" option in UI. | |
| Future<void> clearLocale() async { | |
| _locale = const Locale('en'); // or set to null if your app supports null locale | |
| /// Reset to the app's default locale (English) and clear any saved override. | |
| /// Use this if you provide a "Use app default" option in the UI. | |
| Future<void> clearLocale() async { | |
| _locale = const Locale('en'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
loadSavedLocale()is async but is kicked off without being awaited; this will boot the app in the default locale and then rebuild once the preference loads (visible language “flash” on cold start). If you want the correct locale from the first frame, makemain()async, callWidgetsFlutterBinding.ensureInitialized(), and await locale loading beforerunApp(or gateMaterialAppbehind a loading state).