-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathlocale_provider.dart
More file actions
49 lines (37 loc) · 1.51 KB
/
locale_provider.dart
File metadata and controls
49 lines (37 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
/// 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;
/// 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();
}
/// Sets the application's locale and persists it.
///
/// When the locale is changed, it notifies all listening widgets to rebuild.
Future<void> setLocale(Locale newLocale) async {
_locale = newLocale;
final prefs = await SharedPreferences.getInstance();
await prefs.setString(_localeKey, newLocale.languageCode);
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
final prefs = await SharedPreferences.getInstance();
await prefs.remove(_localeKey);
notifyListeners();
}
}