重整多余的UI组件和未定功能的逻辑链路的修正

This commit is contained in:
2026-11-11 16:33:04 +08:00
parent 34cb9d2805
commit 47f518f94c
100 changed files with 5789 additions and 3070 deletions

View File

@@ -3,21 +3,32 @@ import 'package:flutter/services.dart';
import 'package:flutter/material.dart';
class AppLocalizations {
static const chinese = Locale('zh', 'CN');
static const english = Locale('en', 'US');
static const supportedLocales = [chinese, english];
static Locale localeForLanguage(String? languageCode) =>
languageCode == 'zh' ? chinese : english;
static final _placeholderPattern = RegExp(r'\{(\w+)\}');
final Locale locale;
Map<String, dynamic> _localizedStrings = {};
AppLocalizations(this.locale);
static AppLocalizations of(BuildContext context) {
return Localizations.of<AppLocalizations>(context, AppLocalizations)!;
}
static const LocalizationsDelegate<AppLocalizations> delegate = _AppLocalizationsDelegate();
static const LocalizationsDelegate<AppLocalizations> delegate =
_AppLocalizationsDelegate();
Future<bool> load() async {
try {
String jsonString = await rootBundle.loadString(
'assets/languages/${locale.languageCode}-${locale.countryCode}.json',
final assetLocale = localeForLanguage(locale.languageCode);
final jsonString = await rootBundle.loadString(
'assets/languages/${assetLocale.toLanguageTag()}.json',
);
Map<String, dynamic> jsonMap = json.decode(jsonString);
_localizedStrings = jsonMap;
@@ -27,11 +38,11 @@ class AppLocalizations {
return false;
}
}
String translate(String key) {
String translate(String key, [Map<String, Object?> values = const {}]) {
final keys = key.split('.');
dynamic value = _localizedStrings;
for (final k in keys) {
if (value is Map<String, dynamic>) {
value = value[k];
@@ -39,26 +50,34 @@ class AppLocalizations {
return key;
}
}
return value?.toString() ?? key;
final text = value?.toString() ?? key;
if (values.isEmpty) return text;
return text.replaceAllMapped(
_placeholderPattern,
(match) => values[match[1]]?.toString() ?? match[0]!,
);
}
}
class _AppLocalizationsDelegate extends LocalizationsDelegate<AppLocalizations> {
class _AppLocalizationsDelegate
extends LocalizationsDelegate<AppLocalizations> {
const _AppLocalizationsDelegate();
@override
bool isSupported(Locale locale) {
return ['zh', 'en'].contains(locale.languageCode);
return AppLocalizations.supportedLocales.any(
(supported) => supported.languageCode == locale.languageCode,
);
}
@override
Future<AppLocalizations> load(Locale locale) async {
final localizations = AppLocalizations(locale);
await localizations.load();
return localizations;
}
@override
bool shouldReload(_AppLocalizationsDelegate old) => false;
}

View File

@@ -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');
}
}