build method

  1. @override
Widget build(
  1. BuildContext context
)
override

Builds a widget to allow users to change the application's language.

This widget is a CupertinoPageScaffold containing a CupertinoListSection that displays a list of available languages. Each language is represented by its name and country code. The currently selected language is indicated with a check mark icon.

When a language is selected, the LocaleCubit updates the application's locale accordingly. The widget also provides haptic feedback upon selection.

Implementation

@override
/// Builds a widget to allow users to change the application's language.
///
/// This widget is a [CupertinoPageScaffold] containing a [CupertinoListSection]
/// that displays a list of available languages. Each language is represented
/// by its name and country code. The currently selected language is indicated
/// with a check mark icon.
///
/// When a language is selected, the [LocaleCubit] updates the application's
/// locale accordingly. The widget also provides haptic feedback upon selection.
Widget build(BuildContext context) {
  final tr = AppLocalizations.of(context)!;
  final localeCubit = context.watch<LocaleCubit>();

  final locales = <String, Locale>{
    'العربية': const Locale('ar', 'SA'),
    'English': const Locale('en', 'US'),
    'Français': const Locale('fr', 'FR'),
    'Español': const Locale('es', 'ES'),
    'Deutsch': const Locale('de', 'DE'),
    '中文': const Locale('zh', 'CN'),
    '日本語': const Locale('ja', 'JP'),
    'Русский': const Locale('ru', 'RU'),
  };

  return CupertinoPageScaffold(
    navigationBar: CupertinoNavigationBar(middle: Text(tr.changeLanguage)),
    child: CupertinoListSection.insetGrouped(
      margin: const EdgeInsets.only(top: 8, left: 16, right: 16),
      children: locales.entries.map((entry) {
        final selected = localeCubit.state == entry.value;
        return CupertinoListTile(
          //leading: const Icon(CupertinoIcons.globe),
          title: Text(entry.key, style: const TextStyle(fontSize: 16)),
          subtitle: Text(entry.value.countryCode!),
          trailing: selected
              ? Icon(
                  CupertinoIcons.check_mark_circled,
                  color: Theme.of(context).primaryColor,
                )
              : null,
          onTap: () => {
            HapticFeedback.vibrate(),
            localeCubit.changeLocale(entry.value),
          },
        );
      }).toList(),
    ),
  );
}