tsacdop-podcast-app-android/lib/settings/languages.dart

139 lines
4.5 KiB
Dart
Raw Normal View History

2020-07-07 17:29:21 +02:00
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:intl/intl.dart';
2020-08-13 11:58:24 +02:00
import 'package:intl/intl_standalone.dart';
2020-07-07 17:29:21 +02:00
import '../generated/l10n.dart';
2020-08-02 17:28:22 +02:00
import '../local_storage/key_value_storage.dart';
2020-07-26 12:20:42 +02:00
import '../util/extension_helper.dart';
2020-07-07 17:29:21 +02:00
class LanguagesSetting extends StatefulWidget {
const LanguagesSetting({Key key}) : super(key: key);
@override
_LanguagesSettingState createState() => _LanguagesSettingState();
}
class _LanguagesSettingState extends State<LanguagesSetting> {
2020-08-02 17:28:22 +02:00
_setLocale(Locale locale, {bool systemDefault = false}) async {
var localeStorage = KeyValueStorage(localeKey);
if (systemDefault) {
await localeStorage.saveStringList([]);
2020-08-13 11:58:24 +02:00
await findSystemLocale();
var systemLanCode;
final list = Intl.systemLocale.split('_');
if (list.length == 2) {
systemLanCode = list.first;
} else if (list.length == 3) {
systemLanCode = '${list[0]}_${list[1]}';
} else {
systemLanCode = 'en';
}
await S.load(Locale(systemLanCode));
if (mounted) {
setState(() {});
}
2020-08-02 17:28:22 +02:00
} else {
await localeStorage
.saveStringList([locale.languageCode, locale.countryCode]);
2020-08-13 11:58:24 +02:00
await S.load(locale);
if (mounted) {
setState(() {});
}
2020-08-02 17:28:22 +02:00
}
2020-08-13 11:58:24 +02:00
}
@override
void initState() {
super.initState();
findSystemLocale();
2020-08-02 17:28:22 +02:00
}
2020-07-07 17:29:21 +02:00
@override
Widget build(BuildContext context) {
final s = context.s;
2020-08-15 19:31:07 +02:00
return Column(
children: [
ListTile(
title: Text(
s.systemDefault,
style: TextStyle(
color: Intl.systemLocale.contains(Intl.getCurrentLocale())
? context.accentColor
: null),
),
onTap: () =>
_setLocale(Locale(Intl.systemLocale), systemDefault: true),
contentPadding: const EdgeInsets.only(left: 20, right: 20),
),
Divider(height: 1),
ListTile(
title: Text('English'),
onTap: () => _setLocale(Locale('en')),
contentPadding: const EdgeInsets.only(left: 20, right: 20),
trailing: Radio<Locale>(
value: Locale('en'),
groupValue: Locale(Intl.getCurrentLocale()),
onChanged: _setLocale)),
Divider(height: 1),
ListTile(
title: Text('简体中文'),
onTap: () => _setLocale(Locale('zh_Hans')),
contentPadding: const EdgeInsets.only(left: 20, right: 20),
trailing: Radio<Locale>(
value: Locale('zh_Hans'),
groupValue: Locale(Intl.getCurrentLocale()),
onChanged: _setLocale,
)),
Divider(height: 1),
ListTile(
title: Text('Français'),
onTap: () => _setLocale(Locale('fr')),
contentPadding: const EdgeInsets.only(left: 20, right: 20),
trailing: Radio<Locale>(
value: Locale('fr'),
groupValue: Locale(Intl.getCurrentLocale()),
onChanged: _setLocale),
),
Divider(height: 1),
ListTile(
title: Text('Español'),
onTap: () => _setLocale(Locale('es')),
contentPadding: const EdgeInsets.only(left: 20, right: 20),
trailing: Radio<Locale>(
value: Locale('es'),
groupValue: Locale(Intl.getCurrentLocale()),
onChanged: _setLocale),
2020-07-30 19:16:14 +02:00
),
2020-08-15 19:31:07 +02:00
Divider(height: 1),
2020-09-23 16:18:45 +02:00
ListTile(
2020-09-24 09:27:52 +02:00
title: Text('Português'),
2020-09-23 16:18:45 +02:00
onTap: () => _setLocale(Locale('pt')),
contentPadding: const EdgeInsets.only(left: 20, right: 20),
trailing: Radio<Locale>(
value: Locale('pt'),
groupValue: Locale(Intl.getCurrentLocale()),
onChanged: _setLocale),
),
Divider(height: 1),
2020-08-15 19:31:07 +02:00
ListTile(
onTap: () =>
'mailto:<tsacdop.app@gmail.com>?subject=Tsacdop localization project'
.launchUrl,
contentPadding: const EdgeInsets.only(left: 20, right: 20),
title: Align(
alignment: Alignment.centerLeft,
child: Image(
image: Theme.of(context).brightness == Brightness.light
? AssetImage('assets/localizely_logo.png')
: AssetImage('assets/localizely_logo_light.png'),
height: 20),
2020-07-30 19:16:14 +02:00
),
2020-08-15 19:31:07 +02:00
subtitle: Text(
"If you'd like to contribute to localization project, please contact me."),
2020-07-30 19:16:14 +02:00
),
2020-08-15 19:31:07 +02:00
],
2020-07-07 17:29:21 +02:00
);
}
}