Save language settings permanent.

This commit is contained in:
stonegate 2020-08-02 23:28:22 +08:00
parent 9033181fcf
commit da4a31a81e
3 changed files with 56 additions and 43 deletions

View File

@ -36,6 +36,7 @@ const String rewindSecondsKey = 'rewindSecondsKey';
const String playerHeightKey = 'playerHeightKey';
const String speedKey = 'speedKey';
const String skipSilenceKey = 'skipSilenceKey';
const String localeKey = 'localeKey';
class KeyValueStorage {
final String key;

View File

@ -4,6 +4,7 @@ import 'package:intl/intl.dart';
import 'package:url_launcher/url_launcher.dart';
import '../generated/l10n.dart';
import '../local_storage/key_value_storage.dart';
import '../util/extension_helper.dart';
class LanguagesSetting extends StatefulWidget {
@ -22,6 +23,20 @@ class _LanguagesSettingState extends State<LanguagesSetting> {
}
}
_setLocale(Locale locale, {bool systemDefault = false}) async {
var localeStorage = KeyValueStorage(localeKey);
if (systemDefault) {
await localeStorage.saveStringList([]);
} else {
await localeStorage
.saveStringList([locale.languageCode, locale.countryCode]);
}
await S.load(locale);
if (mounted) {
setState(() {});
}
}
@override
Widget build(BuildContext context) {
final s = context.s;
@ -44,66 +59,43 @@ class _LanguagesSettingState extends State<LanguagesSetting> {
children: [
ListTile(
title: Text(s.systemDefault),
onTap: () async {
await S.load(Locale(Intl.systemLocale));
setState(() {});
},
onTap: () =>
_setLocale(Locale(Intl.systemLocale), systemDefault: true),
contentPadding: const EdgeInsets.only(left: 75, right: 20),
trailing: Radio<Locale>(
value: Locale(Intl.systemLocale),
groupValue: Locale(Intl.getCurrentLocale()),
onChanged: (locale) async {
await S.load(locale);
setState(() {});
}),
onChanged: (locale) =>
_setLocale(locale, systemDefault: true)),
),
Divider(height: 2),
ListTile(
title: Text('English'),
onTap: () async {
await S.load(Locale('en'));
setState(() {});
},
contentPadding: const EdgeInsets.only(left: 75, right: 20),
trailing: Radio<Locale>(
value: Locale('en'),
groupValue: Locale(Intl.getCurrentLocale()),
onChanged: (locale) async {
await S.load(locale);
setState(() {});
}),
),
title: Text('English'),
onTap: () => _setLocale(Locale('en')),
contentPadding: const EdgeInsets.only(left: 75, right: 20),
trailing: Radio<Locale>(
value: Locale('en'),
groupValue: Locale(Intl.getCurrentLocale()),
onChanged: _setLocale)),
Divider(height: 2),
ListTile(
title: Text('简体中文'),
onTap: () async {
await S.load(Locale('zh_Hans'));
setState(() {});
},
contentPadding: const EdgeInsets.only(left: 75, right: 20),
trailing: Radio<Locale>(
title: Text('简体中文'),
onTap: () => _setLocale(Locale('zh_Hans')),
contentPadding: const EdgeInsets.only(left: 75, right: 20),
trailing: Radio<Locale>(
value: Locale('zh_Hans'),
groupValue: Locale(Intl.getCurrentLocale()),
onChanged: (locale) async {
await S.load(locale);
setState(() {});
}),
),
onChanged: _setLocale,
)),
Divider(height: 2),
ListTile(
title: Text('LE françAIS'),
onTap: () async {
await S.load(Locale('fr'));
setState(() {});
},
title: Text('Français'),
onTap: () => _setLocale(Locale('fr')),
contentPadding: const EdgeInsets.only(left: 75, right: 20),
trailing: Radio<Locale>(
value: Locale('fr'),
groupValue: Locale(Intl.getCurrentLocale()),
onChanged: (locale) async {
await S.load(locale);
setState(() {});
}),
onChanged: _setLocale),
),
Divider(height: 2),
//ListTile(

View File

@ -5,6 +5,8 @@ import 'package:connectivity/connectivity.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_downloader/flutter_downloader.dart';
import 'package:intl/intl.dart';
import 'package:tsacdop/generated/l10n.dart';
import 'package:workmanager/workmanager.dart';
import '../local_storage/key_value_storage.dart';
@ -100,6 +102,7 @@ class SettingState extends ChangeNotifier {
var autoDownloadStorage = KeyValueStorage(autoDownloadNetworkKey);
var fastForwardSecondsStorage = KeyValueStorage(fastForwardSecondsKey);
var rewindSecondsStorage = KeyValueStorage(rewindSecondsKey);
var localeStorage = KeyValueStorage(localeKey);
Future initData() async {
await _getTheme();
@ -108,6 +111,11 @@ class SettingState extends ChangeNotifier {
await _getRealDark();
}
Locale _locale;
/// Load locale.
Locale get locale => _locale;
/// Spp thememode. default auto.
ThemeMode _theme;
ThemeMode get theme => _theme;
@ -253,6 +261,7 @@ class SettingState extends ChangeNotifier {
@override
void addListener(VoidCallback listener) {
super.addListener(listener);
_getLocale();
_getAutoUpdate();
_getDownloadUsingData();
_getSleepTimerData();
@ -331,6 +340,17 @@ class SettingState extends ChangeNotifier {
await fastForwardSecondsStorage.getInt(defaultValue: 30);
}
Future _getLocale() async {
var localeString = await localeStorage.getStringList();
if (localeString.isEmpty) {
_locale = Locale(Intl.systemLocale);
} else {
_locale = Locale(localeString.first, localeString[1]);
}
await S.load(_locale);
print(_locale.toString());
}
Future _saveAccentSetColor() async {
await accentStorage
.saveString(_accentSetColor.toString().substring(10, 16));