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; LocaleProvider() { _loadLocale(); } Future _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); } } class L10n { static final all = [ const Locale('en'), const Locale('fr'), const Locale('de') ]; }