重整多余的UI组件和未定功能的逻辑链路的修正
This commit is contained in:
@@ -2,44 +2,57 @@ import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import 'app_localizations.dart';
|
||||
|
||||
class LocaleCubit extends Cubit<Locale> {
|
||||
static const String _localeKey = 'app_locale';
|
||||
static const String _chosenKey = 'app_locale_chosen';
|
||||
static const _languageKey = 'app_locale_lang';
|
||||
static const _countryKey = 'app_locale_country';
|
||||
static const _chosenKey = 'app_locale_chosen';
|
||||
|
||||
/// 用户是否已明确选择过语言(决定登录页是否弹强制语言选择框)
|
||||
bool hasChosenLanguage = false;
|
||||
LocaleCubit(this._preferences)
|
||||
: _hasChosenLanguage = _preferences.get(_chosenKey) == true,
|
||||
super(_savedLocale(_preferences));
|
||||
|
||||
LocaleCubit() : super(const Locale('en', 'US')) {
|
||||
_loadSavedLocale();
|
||||
final SharedPreferences _preferences;
|
||||
bool _hasChosenLanguage;
|
||||
Future<void> _pendingSave = Future<void>.value();
|
||||
|
||||
/// 用户是否已明确选择过语言。
|
||||
bool get hasChosenLanguage => _hasChosenLanguage;
|
||||
|
||||
static Locale _savedLocale(SharedPreferences preferences) {
|
||||
if (preferences.get(_chosenKey) != true) return AppLocalizations.english;
|
||||
return AppLocalizations.localeForLanguage(
|
||||
preferences.get(_languageKey)?.toString(),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _loadSavedLocale() async {
|
||||
Future<void> setLocale(Locale locale) {
|
||||
if (isClosed) return Future<void>.value();
|
||||
final selected = AppLocalizations.localeForLanguage(locale.languageCode);
|
||||
_hasChosenLanguage = true;
|
||||
emit(selected);
|
||||
|
||||
// 快速切换时按点选顺序写入,最后一次选择用于下次启动。
|
||||
return _pendingSave = _pendingSave.then((_) => _persist(selected));
|
||||
}
|
||||
|
||||
Future<void> _persist(Locale locale) async {
|
||||
try {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
hasChosenLanguage = prefs.getBool(_chosenKey) ?? false;
|
||||
if (hasChosenLanguage) {
|
||||
final languageCode = prefs.getString('${_localeKey}_lang') ?? 'en';
|
||||
final countryCode = prefs.getString('${_localeKey}_country') ?? 'US';
|
||||
emit(Locale(languageCode, countryCode));
|
||||
} else {
|
||||
// 未选择过:默认英文(登录页仍会弹出强制选择框)
|
||||
emit(const Locale('en', 'US'));
|
||||
final languageSaved = await _preferences.setString(
|
||||
_languageKey,
|
||||
locale.languageCode,
|
||||
);
|
||||
final countrySaved = await _preferences.setString(
|
||||
_countryKey,
|
||||
locale.countryCode!,
|
||||
);
|
||||
final chosenSaved = await _preferences.setBool(_chosenKey, true);
|
||||
if (!languageSaved || !countrySaved || !chosenSaved) {
|
||||
debugPrint('[LocaleCubit] 语言偏好未写入存储');
|
||||
}
|
||||
} catch (e) {
|
||||
emit(const Locale('en', 'US'));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> setLocale(Locale locale) async {
|
||||
try {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setString('${_localeKey}_lang', locale.languageCode);
|
||||
await prefs.setString('${_localeKey}_country', locale.countryCode ?? '');
|
||||
await prefs.setBool(_chosenKey, true);
|
||||
hasChosenLanguage = true;
|
||||
emit(locale);
|
||||
} catch (e) {
|
||||
debugPrint('❌ [LocaleCubit] 保存语言设置失败: $e');
|
||||
} catch (error) {
|
||||
debugPrint('[LocaleCubit] 保存语言设置失败: $error');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user