diff --git a/lib/l10n/app_de.arb b/lib/l10n/app_de.arb index 90e4206..08f51e1 100644 --- a/lib/l10n/app_de.arb +++ b/lib/l10n/app_de.arb @@ -93,5 +93,7 @@ "username": "Benutzername", "password": "Passwort", "enter_your_password": "Geben Sie Ihr Passwort ein", - "home": "Startseite" + "home": "Startseite", + "language_selection": "Sprache", + "language_selection_description": "Wählen Sie Ihre bevorzugte Sprache aus" } diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb index 9f66c72..c5da565 100644 --- a/lib/l10n/app_en.arb +++ b/lib/l10n/app_en.arb @@ -93,5 +93,7 @@ "username": "username", "password": "password", "enter_your_password": "Enter your password", - "home": "Home" + "home": "Home", + "language_selection": "Language", + "language_selection_description": "Select your preferred language" } \ No newline at end of file diff --git a/lib/l10n/app_es.arb b/lib/l10n/app_es.arb index b681829..f789e0e 100644 --- a/lib/l10n/app_es.arb +++ b/lib/l10n/app_es.arb @@ -93,5 +93,7 @@ "username": "nombre de usuario", "password": "contraseña", "enter_your_password": "Introduce tu contraseña", - "home": "Inicio" + "home": "Inicio", + "language_selection": "Idioma", + "language_selection_description": "Selecciona tu idioma preferido." } diff --git a/lib/l10n/app_fr.arb b/lib/l10n/app_fr.arb index 7218e55..0490725 100644 --- a/lib/l10n/app_fr.arb +++ b/lib/l10n/app_fr.arb @@ -93,5 +93,7 @@ "username": "nom d'utilisateur", "password": "mot de passe", "enter_your_password": "Entrez votre mot de passe", - "home": "Accueil" + "home": "Accueil", + "language_selection": "Langue", + "language_selection_description": "Choisissez votre langue préférée." } diff --git a/lib/main.dart b/lib/main.dart index cecb373..4504155 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -2,6 +2,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:gocast_mobile/models/user/user_state_model.dart'; import 'package:gocast_mobile/providers.dart'; +import 'package:gocast_mobile/utils/UserPreferences.dart'; import 'package:gocast_mobile/utils/globals.dart'; import 'package:gocast_mobile/utils/theme.dart'; import 'package:gocast_mobile/navigation_tab.dart'; @@ -21,6 +22,7 @@ Future main() async { Logger.level = Level.info; WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform); + await UserPreferences.init(); runApp( const ProviderScope( @@ -56,7 +58,7 @@ class App extends ConsumerWidget { GlobalCupertinoLocalizations.delegate, ], supportedLocales: L10n.all, - locale: const Locale('de'), + locale: Locale(UserPreferences.getLanguage()), theme: appTheme, // Your light theme darkTheme: darkAppTheme, // Define your dark theme themeMode: diff --git a/lib/utils/UserPreferences.dart b/lib/utils/UserPreferences.dart new file mode 100644 index 0000000..d92f550 --- /dev/null +++ b/lib/utils/UserPreferences.dart @@ -0,0 +1,27 @@ +import 'package:shared_preferences/shared_preferences.dart'; + +class UserPreferences { + static SharedPreferences? _preferences; + + static const _keyLanguage = 'language'; + + static Future init() async => _preferences = await SharedPreferences.getInstance(); + + static Future setLanguage(String language) async => await _preferences?.setString(_keyLanguage, language); + + static String getLanguage() => _preferences?.getString(_keyLanguage) ?? 'en'; + + static String getLanguageName(String lang) { + switch (lang) { + case 'en': + return 'English'; + case 'es': + return 'Español'; + case 'fr': + return 'Français'; + case 'de': + return 'Deutsch'; + } + return 'English'; + } +} diff --git a/lib/view_models/setting_view_model.dart b/lib/view_models/setting_view_model.dart index 4a7f939..4df3644 100644 --- a/lib/view_models/setting_view_model.dart +++ b/lib/view_models/setting_view_model.dart @@ -120,4 +120,5 @@ class SettingViewModel extends StateNotifier { void setLoading(bool isLoading) { state = state.copyWith(isLoading: isLoading); } + } diff --git a/lib/views/settings_view/settings_screen_view.dart b/lib/views/settings_view/settings_screen_view.dart index ce3320e..e7ecef4 100644 --- a/lib/views/settings_view/settings_screen_view.dart +++ b/lib/views/settings_view/settings_screen_view.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:gocast_mobile/providers.dart'; +import 'package:gocast_mobile/utils/UserPreferences.dart'; import 'package:gocast_mobile/views/on_boarding_view/welcome_screen_view.dart'; import 'package:gocast_mobile/views/settings_view/playback_speed_settings_view.dart'; import 'package:gocast_mobile/views/settings_view/preferred_greeting_view.dart'; @@ -71,6 +72,7 @@ class _SettingsScreenState extends ConsumerState { ref: ref, ), _buildThemeSelectionTile(context, ref), + _builLanguageSelectionTile(context, ref), _buildSwitchListTile( title: AppLocalizations.of(context)!.download_over_wifi_only, value: settingState.isDownloadWithWifiOnly, @@ -126,7 +128,6 @@ class _SettingsScreenState extends ConsumerState { } else { themeModeText = AppLocalizations.of(context)!.system_default; } - return ListTile( title: Text(AppLocalizations.of(context)!.choose_theme), subtitle: Text(themeModeText), @@ -135,6 +136,38 @@ class _SettingsScreenState extends ConsumerState { ); } + ListTile _builLanguageSelectionTile(BuildContext context, WidgetRef ref) { + return ListTile( + title: Text(AppLocalizations.of(context)!.language_selection), + subtitle: Text(UserPreferences.getLanguageName(UserPreferences.getLanguage())), + trailing: const Icon(Icons.arrow_forward_ios), + onTap: () async { + String? selectedLanguage = await showDialog( + context: context, + builder: (BuildContext context) { + return SimpleDialog( + title: Text(AppLocalizations.of(context)!.language_selection_description), + children: ['en', 'fr', 'de', 'es'] + .map((String lang) => SimpleDialogOption( + onPressed: () { + Navigator.pop(context, lang); + }, + child: Text(UserPreferences.getLanguageName(lang)), // Assuming you have a method to get the language name + )) + .toList(), + ); + }, + ); + if (selectedLanguage != null) { + await UserPreferences.setLanguage(selectedLanguage); + //to rebuild the app + ref.read(settingViewModelProvider.notifier).setLoading(false); + } + }, + ); + } + + void _showThemeSelectionSheet(BuildContext context, WidgetRef ref) { showModalBottomSheet( context: context,