feat: Refactor LanguageSelectionPage to use StatelessWidget and improve localization handling
This commit is contained in:
Binary file not shown.
@@ -9,19 +9,23 @@ import 'package:flutter_modular/flutter_modular.dart';
|
||||
/// Displays available languages and allows the user to select their preferred
|
||||
/// language. Changes are applied immediately via [LocaleBloc] and persisted.
|
||||
/// Shows a snackbar when the language is successfully changed.
|
||||
class LanguageSelectionPage extends StatefulWidget {
|
||||
class LanguageSelectionPage extends StatelessWidget {
|
||||
/// Creates a [LanguageSelectionPage].
|
||||
const LanguageSelectionPage({super.key});
|
||||
|
||||
@override
|
||||
State<LanguageSelectionPage> createState() => _LanguageSelectionPageState();
|
||||
}
|
||||
String _getLocalizedLanguageName(AppLocale locale) {
|
||||
switch (locale) {
|
||||
case AppLocale.en:
|
||||
return 'English';
|
||||
case AppLocale.es:
|
||||
return 'Español';
|
||||
}
|
||||
}
|
||||
|
||||
class _LanguageSelectionPageState extends State<LanguageSelectionPage> {
|
||||
void _showLanguageChangedSnackbar(String languageName) {
|
||||
void _showLanguageChangedSnackbar(BuildContext context, String languageName) {
|
||||
UiSnackbar.show(
|
||||
context,
|
||||
message: 'Language changed to $languageName',
|
||||
message: '${t.settings.change_language}: $languageName',
|
||||
type: UiSnackbarType.success,
|
||||
);
|
||||
|
||||
@@ -33,7 +37,7 @@ class _LanguageSelectionPageState extends State<LanguageSelectionPage> {
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: UiAppBar(
|
||||
title: 'Select Language',
|
||||
title: t.settings.change_language,
|
||||
showBackButton: true,
|
||||
bottom: PreferredSize(
|
||||
preferredSize: const Size.fromHeight(1.0),
|
||||
@@ -46,17 +50,9 @@ class _LanguageSelectionPageState extends State<LanguageSelectionPage> {
|
||||
return ListView(
|
||||
padding: const EdgeInsets.all(UiConstants.space5),
|
||||
children: <Widget>[
|
||||
_buildLanguageOption(
|
||||
context,
|
||||
label: 'English',
|
||||
locale: AppLocale.en,
|
||||
),
|
||||
_buildLanguageOption(context, locale: AppLocale.en),
|
||||
const SizedBox(height: UiConstants.space4),
|
||||
_buildLanguageOption(
|
||||
context,
|
||||
label: 'Español',
|
||||
locale: AppLocale.es,
|
||||
),
|
||||
_buildLanguageOption(context, locale: AppLocale.es),
|
||||
],
|
||||
);
|
||||
},
|
||||
@@ -67,9 +63,9 @@ class _LanguageSelectionPageState extends State<LanguageSelectionPage> {
|
||||
|
||||
Widget _buildLanguageOption(
|
||||
BuildContext context, {
|
||||
required String label,
|
||||
required AppLocale locale,
|
||||
}) {
|
||||
final String label = _getLocalizedLanguageName(locale);
|
||||
// Check if this option is currently selected.
|
||||
final AppLocale currentLocale = LocaleSettings.currentLocale;
|
||||
final bool isSelected = currentLocale == locale;
|
||||
@@ -79,7 +75,7 @@ class _LanguageSelectionPageState extends State<LanguageSelectionPage> {
|
||||
// Only proceed if selecting a different language
|
||||
if (currentLocale != locale) {
|
||||
Modular.get<LocaleBloc>().add(ChangeLocale(locale.flutterLocale));
|
||||
_showLanguageChangedSnackbar(label);
|
||||
_showLanguageChangedSnackbar(context, label);
|
||||
}
|
||||
},
|
||||
borderRadius: BorderRadius.circular(UiConstants.radiusMdValue),
|
||||
|
||||
Reference in New Issue
Block a user