i18n_multilanguage_with_flags 0.2.0
i18n_multilanguage_with_flags: ^0.2.0 copied to clipboard
Generic Flutter i18n toolkit with language metadata, flag dropdowns, and language selection persistence.
import 'package:flutter/material.dart';
import 'package:i18n_multilanguage_with_flags/i18n_multilanguage_with_flags.dart';
import 'i18n/translation_keys.dart';
import 'i18n/translations/all_translations.dart';
void main() {
runApp(const ExampleApp());
}
class ExampleApp extends StatefulWidget {
const ExampleApp({super.key});
@override
State<ExampleApp> createState() => _ExampleAppState();
}
class _ExampleAppState extends State<ExampleApp> {
late final I18nMultilanguageController _controller;
final _storage = LanguageSelectionStorage();
final _defaultCatalogMap = buildDefaultLanguageCatalogMap();
@override
void initState() {
super.initState();
_controller = I18nMultilanguageController(
languages: buildConfigurableLanguageCatalog(
includeIds: const ['en_us', 'pt_br'],
),
initialUiLanguageId: 'en_us',
initialSecondaryLanguageId: 'pt_br',
translations: appTranslations,
);
_hydrate();
}
Future<void> _hydrate() async {
await _controller.loadFromStorage(_storage);
if (mounted) {
setState(() {});
}
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text(_controller.tr(TranslationKeys.appTitle)),
),
body: AnimatedBuilder(
animation: _controller,
builder: (context, _) {
return Column(
children: [
I18nPreferencesForm(
languages: _controller.languages,
initialUiLanguageId: _controller.uiLanguageId,
initialSecondaryLanguageId: _controller.secondaryLanguageId,
initialDisplayName: _controller.displayName,
showDisplayNameField: true,
requireSecondaryLanguage: true,
uiLanguageLabel:
_controller.tr(TranslationKeys.uiLanguageLabel),
secondaryLanguageLabel:
_controller.tr(TranslationKeys.secondaryLanguageLabel),
displayNameLabel:
_controller.tr(TranslationKeys.displayNameLabel),
displayNameHint:
_controller.tr(TranslationKeys.displayNameHint),
submitLabel: _controller.tr(TranslationKeys.submit),
onSubmit: (snapshot) async {
_controller.applySnapshot(snapshot);
await _controller.saveToStorage(_storage);
},
),
const Divider(height: 1),
Padding(
padding: const EdgeInsets.all(16),
child: Align(
alignment: Alignment.centerLeft,
child: Text(
'${_controller.tr(TranslationKeys.summary)}: '
'${_controller.uiLanguageId} / '
'${_controller.secondaryLanguageId ?? '-'} / '
'${_controller.displayName} / '
'langs=${_controller.languages.length}',
),
),
),
const SizedBox(height: 8),
Wrap(
spacing: 8,
runSpacing: 8,
children: [
OutlinedButton(
onPressed: () {
final spanish = _defaultCatalogMap['es_es'];
if (spanish == null) return;
_controller.upsertLanguage(spanish);
},
child: const Text('Add Spanish'),
),
OutlinedButton(
onPressed: () {
_controller.removeLanguage('pt_br');
},
child: const Text('Remove Portuguese'),
),
],
),
const SizedBox(height: 12),
],
);
},
),
),
);
}
}