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