|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter_bloc/flutter_bloc.dart'; |
| 3 | +import 'package:ht_main/app/bloc/app_bloc.dart'; |
| 4 | +import 'package:ht_main/l10n/l10n.dart'; |
| 5 | +import 'package:ht_main/settings/bloc/settings_bloc.dart'; |
| 6 | +import 'package:ht_main/shared/constants/app_spacing.dart'; |
| 7 | +import 'package:ht_shared/ht_shared.dart' show AppLanguage; |
| 8 | + |
| 9 | +// Defines the available languages and their display names. |
| 10 | +// In a real app, this might come from a configuration or be more dynamic. |
| 11 | +const Map<String, String> _supportedLanguages = { |
| 12 | + 'en': 'English', |
| 13 | + 'ar': 'العربية (Arabic)', |
| 14 | +}; |
| 15 | + |
| 16 | +/// {@template language_settings_page} |
| 17 | +/// A page for selecting the application language. |
| 18 | +/// {@endtemplate} |
| 19 | +class LanguageSettingsPage extends StatelessWidget { |
| 20 | + /// {@macro language_settings_page} |
| 21 | + const LanguageSettingsPage({super.key}); |
| 22 | + |
| 23 | + @override |
| 24 | + Widget build(BuildContext context) { |
| 25 | + final l10n = context.l10n; |
| 26 | + final settingsBloc = context.watch<SettingsBloc>(); |
| 27 | + final settingsState = settingsBloc.state; |
| 28 | + |
| 29 | + if (settingsState.status != SettingsStatus.success || |
| 30 | + settingsState.userAppSettings == null) { |
| 31 | + return Scaffold( |
| 32 | + appBar: AppBar(title: Text(l10n.settingsTitle)), // Placeholder l10n key |
| 33 | + body: const Center(child: CircularProgressIndicator()), |
| 34 | + ); |
| 35 | + } |
| 36 | + |
| 37 | + final currentLanguage = settingsState.userAppSettings!.language; |
| 38 | + |
| 39 | + return BlocListener<SettingsBloc, SettingsState>( |
| 40 | + listener: (context, state) { |
| 41 | + if (state.status == SettingsStatus.success) { |
| 42 | + context.read<AppBloc>().add(const AppSettingsRefreshed()); |
| 43 | + } |
| 44 | + }, |
| 45 | + child: Scaffold( |
| 46 | + appBar: AppBar(title: Text(l10n.settingsTitle)), // Placeholder l10n key |
| 47 | + body: ListView.separated( |
| 48 | + padding: const EdgeInsets.symmetric(vertical: AppSpacing.md), |
| 49 | + itemCount: _supportedLanguages.length, |
| 50 | + separatorBuilder: (context, index) => |
| 51 | + const Divider(indent: AppSpacing.lg, endIndent: AppSpacing.lg), |
| 52 | + itemBuilder: (context, index) { |
| 53 | + final languageCode = _supportedLanguages.keys.elementAt(index); |
| 54 | + final languageName = _supportedLanguages.values.elementAt(index); |
| 55 | + final isSelected = languageCode == currentLanguage; |
| 56 | + |
| 57 | + return ListTile( |
| 58 | + title: Text(languageName), |
| 59 | + trailing: isSelected |
| 60 | + ? Icon(Icons.check, color: Theme.of(context).primaryColor) |
| 61 | + : null, |
| 62 | + onTap: () { |
| 63 | + if (!isSelected) { |
| 64 | + // Dispatch event to SettingsBloc |
| 65 | + context |
| 66 | + .read<SettingsBloc>() |
| 67 | + .add(SettingsLanguageChanged(languageCode)); |
| 68 | + } |
| 69 | + }, |
| 70 | + ); |
| 71 | + }, |
| 72 | + ), |
| 73 | + ), |
| 74 | + ); |
| 75 | + } |
| 76 | +} |
0 commit comments