language_screen.dart 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_riverpod/flutter_riverpod.dart';
  3. import '../../../core/l10n/app_localizations.dart';
  4. import '../../../providers/app_provider.dart';
  5. class LanguageScreen extends ConsumerWidget {
  6. const LanguageScreen({super.key});
  7. @override
  8. Widget build(BuildContext context, WidgetRef ref) {
  9. final l10n = AppLocalizations.of(context)!;
  10. final current = ref.watch(localeProvider);
  11. final cs = Theme.of(context).colorScheme;
  12. return Scaffold(
  13. appBar: AppBar(
  14. title: Text(l10n.language),
  15. centerTitle: true,
  16. ),
  17. body: ListView.separated(
  18. padding: const EdgeInsets.symmetric(vertical: 8),
  19. itemCount: supportedLanguages.length,
  20. separatorBuilder: (_, __) => Divider(
  21. height: 1,
  22. indent: 20,
  23. endIndent: 20,
  24. color: cs.outline.withAlpha(40),
  25. ),
  26. itemBuilder: (context, index) {
  27. final option = supportedLanguages[index];
  28. final isSelected = _matches(current, option.locale);
  29. return ListTile(
  30. contentPadding:
  31. const EdgeInsets.symmetric(horizontal: 20, vertical: 4),
  32. title: Text(
  33. option.label,
  34. style: TextStyle(
  35. fontSize: 15,
  36. fontWeight:
  37. isSelected ? FontWeight.w600 : FontWeight.normal,
  38. color: isSelected ? cs.primary : cs.onSurface,
  39. ),
  40. ),
  41. trailing: isSelected
  42. ? Icon(Icons.check_rounded, color: cs.primary, size: 20)
  43. : null,
  44. onTap: () {
  45. ref.read(localeProvider.notifier).setLocale(option.locale);
  46. Navigator.of(context).pop();
  47. },
  48. );
  49. },
  50. ),
  51. );
  52. }
  53. bool _matches(Locale a, Locale b) {
  54. if (a.languageCode != b.languageCode) return false;
  55. return (a.countryCode ?? '') == (b.countryCode ?? '');
  56. }
  57. }