feature/internationale #46
@@ -1,15 +1,34 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
class LocaleProvider with ChangeNotifier {
|
||||
static const _localeKey = 'locale_code';
|
||||
|
||||
Locale _locale = const Locale('en');
|
||||
|
||||
Locale get locale => _locale;
|
||||
|
||||
void setLocale(Locale locale) {
|
||||
LocaleProvider() {
|
||||
_loadLocale();
|
||||
}
|
||||
|
||||
Future<void> _loadLocale() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final code = prefs.getString(_localeKey);
|
||||
if (code != null && L10n.all.contains(Locale(code))) {
|
||||
_locale = Locale(code);
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
||||
void setLocale(Locale locale) async {
|
||||
if (!L10n.all.contains(locale)) return;
|
||||
|
||||
_locale = locale;
|
||||
notifyListeners();
|
||||
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setString(_localeKey, locale.languageCode);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -26,7 +26,7 @@ class MyApp extends StatelessWidget {
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: false,
|
||||
locale: localeProvider.locale, // <-- utilise la locale courante
|
||||
supportedLocales: const [Locale('en'), Locale('fr'), Locale('de')],
|
||||
supportedLocales: L10n.all,
|
||||
localizationsDelegates: AppLocalizations.localizationsDelegates,
|
||||
home: LoginDemo(),
|
||||
);
|
||||
|
Reference in New Issue
Block a user