refactor(settings): 重构设置页面,支持语言和地区设置

This commit is contained in:
qzl
2026-04-07 18:44:14 +08:00
parent 6e82053ea7
commit 6217844865
4 changed files with 101 additions and 52 deletions
@@ -13,10 +13,8 @@ class LanguageSettingsScreen extends StatelessWidget {
Widget build(BuildContext context) { Widget build(BuildContext context) {
final l10n = AppLocalizations.of(context)!; final l10n = AppLocalizations.of(context)!;
final colors = Theme.of(context).colorScheme; final colors = Theme.of(context).colorScheme;
final options = <({String tag, String label})>[
(tag: 'zh-CN', label: l10n.chinese), final options = _buildLanguageOptions(l10n);
(tag: 'en-US', label: l10n.english),
];
return Scaffold( return Scaffold(
backgroundColor: colors.surfaceContainerLow, backgroundColor: colors.surfaceContainerLow,
@@ -40,12 +38,10 @@ class LanguageSettingsScreen extends StatelessWidget {
tint: colors.primary, tint: colors.primary,
background: colors.surfaceContainerHighest, background: colors.surfaceContainerHighest,
showDivider: i != options.length - 1, showDivider: i != options.length - 1,
showChevron: false,
trailing: selectedLanguageTag == options[i].tag trailing: selectedLanguageTag == options[i].tag
? Icon(Icons.check_rounded, color: colors.primary) ? Icon(Icons.check_rounded, color: colors.primary)
: Icon( : null,
Icons.chevron_right_rounded,
color: colors.outline,
),
onTap: () => Navigator.of(context).pop(options[i].tag), onTap: () => Navigator.of(context).pop(options[i].tag),
), ),
], ],
@@ -54,4 +50,41 @@ class LanguageSettingsScreen extends StatelessWidget {
), ),
); );
} }
List<({String tag, String label})> _buildLanguageOptions(
AppLocalizations l10n,
) {
final supportedLocales = AppLocalizations.supportedLocales;
return supportedLocales.map((locale) {
final tag = _localeToTag(locale);
final label = _getLocaleLabel(locale, l10n);
return (tag: tag, label: label);
}).toList();
}
String _localeToTag(Locale locale) {
final lang = locale.languageCode;
final script = locale.scriptCode;
final country = locale.countryCode;
if (script != null && country != null) {
return '$lang-$script-$country';
} else if (country != null) {
return '$lang-$country';
}
return _mapToBackendTag(lang);
}
String _mapToBackendTag(String flutterTag) {
const mapping = {'zh': 'zh-CN', 'en': 'en-US'};
return mapping[flutterTag] ?? flutterTag;
}
String _getLocaleLabel(Locale locale, AppLocalizations l10n) {
if (locale.languageCode == 'zh') {
return l10n.chinese;
} else if (locale.languageCode == 'en') {
return l10n.english;
}
return locale.languageCode;
}
} }
@@ -14,11 +14,13 @@ class ProfileEditScreen extends StatefulWidget {
required this.account, required this.account,
required this.settings, required this.settings,
required this.onUploadAvatar, required this.onUploadAvatar,
required this.onSave,
}); });
final String account; final String account;
final ProfileSettingsV1 settings; final ProfileSettingsV1 settings;
final Future<ProfileSettingsV1> Function(String filePath) onUploadAvatar; final Future<ProfileSettingsV1> Function(String filePath) onUploadAvatar;
final Future<ProfileSettingsV1> Function(ProfileSettingsV1 updated) onSave;
@override @override
State<ProfileEditScreen> createState() => _ProfileEditScreenState(); State<ProfileEditScreen> createState() => _ProfileEditScreenState();
@@ -191,7 +193,7 @@ class _ProfileEditScreenState extends State<ProfileEditScreen> {
); );
} }
void _save() { Future<void> _save() async {
final l10n = AppLocalizations.of(context)!; final l10n = AppLocalizations.of(context)!;
final name = _nameController.text.trim(); final name = _nameController.text.trim();
if (name.isEmpty) { if (name.isEmpty) {
@@ -202,14 +204,33 @@ class _ProfileEditScreenState extends State<ProfileEditScreen> {
); );
return; return;
} }
Navigator.of(context).pop( final updated = widget.settings.copyWith(
widget.settings.copyWith( displayName: name,
displayName: name, bio: _bioController.text.trim(),
bio: _bioController.text.trim(), avatarPath: _avatarPath,
avatarPath: _avatarPath, avatarUrl: _avatarPreviewUrl,
avatarUrl: _avatarPreviewUrl,
),
); );
try {
final saved = await widget.onSave(updated);
if (!mounted) {
return;
}
Navigator.of(context).pop(saved);
} catch (error, stackTrace) {
_logger.error(
message: 'Failed to save profile',
error: error,
stackTrace: stackTrace,
);
if (!mounted) {
return;
}
Toast.show(
context,
AppLocalizations.of(context)!.errorRequestGeneric,
type: ToastType.error,
);
}
} }
Future<void> _pickAndUploadAvatar() async { Future<void> _pickAndUploadAvatar() async {
@@ -1,16 +1,14 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import '../../../../l10n/app_localizations.dart'; import '../../../../l10n/app_localizations.dart';
import '../../../../core/logging/logger.dart';
import '../../../../shared/theme/design_tokens.dart'; import '../../../../shared/theme/design_tokens.dart';
import '../../../../shared/widgets/app_modal_dialog.dart'; import '../../../../shared/widgets/app_modal_dialog.dart';
import '../../../../shared/widgets/gua_icon.dart'; import '../../../../shared/widgets/gua_icon.dart';
import '../../../../shared/widgets/toast/toast.dart';
import '../../../../shared/widgets/toast/toast_type.dart';
import '../../data/models/profile_settings.dart'; import '../../data/models/profile_settings.dart';
import '../widgets/settings_section_widgets.dart'; import '../widgets/settings_section_widgets.dart';
import 'coin_center_screen.dart'; import 'coin_center_screen.dart';
import 'general_settings_screen.dart'; import 'general_settings_screen.dart';
import 'invite_screen.dart';
import 'legal_center_screen.dart'; import 'legal_center_screen.dart';
import 'profile_edit_screen.dart'; import 'profile_edit_screen.dart';
@@ -24,6 +22,7 @@ class SettingsScreen extends StatefulWidget {
required this.onSettingsChanged, required this.onSettingsChanged,
required this.onUploadAvatar, required this.onUploadAvatar,
required this.onLogout, required this.onLogout,
required this.onSaveProfile,
}); });
final String account; final String account;
@@ -33,13 +32,14 @@ class SettingsScreen extends StatefulWidget {
final Future<void> Function(ProfileSettingsV1 settings) onSettingsChanged; final Future<void> Function(ProfileSettingsV1 settings) onSettingsChanged;
final Future<ProfileSettingsV1> Function(String filePath) onUploadAvatar; final Future<ProfileSettingsV1> Function(String filePath) onUploadAvatar;
final Future<void> Function() onLogout; final Future<void> Function() onLogout;
final Future<ProfileSettingsV1> Function(ProfileSettingsV1 updated)
onSaveProfile;
@override @override
State<SettingsScreen> createState() => _SettingsScreenState(); State<SettingsScreen> createState() => _SettingsScreenState();
} }
class _SettingsScreenState extends State<SettingsScreen> { class _SettingsScreenState extends State<SettingsScreen> {
final Logger _logger = getLogger('features.settings.settings_screen');
late ProfileSettingsV1 _settings; late ProfileSettingsV1 _settings;
@override @override
@@ -102,6 +102,13 @@ class _SettingsScreenState extends State<SettingsScreen> {
background: colors.surfaceContainerHighest, background: colors.surfaceContainerHighest,
onTap: _openGeneralSettings, onTap: _openGeneralSettings,
), ),
SettingsMenuTile(
icon: Icons.card_giftcard_rounded,
title: l10n.settingsInviteTitle,
tint: colors.primary,
background: colors.surfaceContainerHighest,
onTap: _openInvite,
),
SettingsMenuTile( SettingsMenuTile(
icon: Icons.description_outlined, icon: Icons.description_outlined,
title: l10n.settingsLegalCenterTitle, title: l10n.settingsLegalCenterTitle,
@@ -140,20 +147,26 @@ class _SettingsScreenState extends State<SettingsScreen> {
} }
Future<void> _openGeneralSettings() async { Future<void> _openGeneralSettings() async {
final result = await Navigator.of(context).push<ProfileSettingsV1>( await Navigator.of(context).push<void>(
MaterialPageRoute<ProfileSettingsV1>( MaterialPageRoute<void>(
builder: (_) => GeneralSettingsScreen( builder: (_) => GeneralSettingsScreen(
settings: _settings, settings: _settings,
onInterfaceLanguageChanged: widget.onInterfaceLanguageChanged, onSettingsChanged: (newSettings) async {
await widget.onSettingsChanged(newSettings);
if (!mounted) return;
setState(() {
_settings = newSettings;
});
},
), ),
), ),
); );
if (result == null || !mounted) { }
return;
} Future<void> _openInvite() async {
setState(() { await Navigator.of(
_settings = result; context,
}); ).push<void>(MaterialPageRoute<void>(builder: (_) => const InviteScreen()));
} }
Future<void> _openProfileEdit() async { Future<void> _openProfileEdit() async {
@@ -163,35 +176,16 @@ class _SettingsScreenState extends State<SettingsScreen> {
account: widget.account, account: widget.account,
settings: _settings, settings: _settings,
onUploadAvatar: widget.onUploadAvatar, onUploadAvatar: widget.onUploadAvatar,
onSave: widget.onSaveProfile,
), ),
), ),
); );
if (result == null || !mounted) { if (result == null || !mounted) {
return; return;
} }
try { setState(() {
await widget.onSettingsChanged(result); _settings = result;
if (!mounted) { });
return;
}
setState(() {
_settings = result;
});
} catch (error, stackTrace) {
_logger.error(
message: 'Failed to save profile settings',
error: error,
stackTrace: stackTrace,
);
if (!mounted) {
return;
}
Toast.show(
context,
AppLocalizations.of(context)!.errorRequestGeneric,
type: ToastType.error,
);
}
} }
Future<void> _openLegalCenter() async { Future<void> _openLegalCenter() async {
@@ -27,6 +27,7 @@ void main() {
lowerName: '', lowerName: '',
signType: '中上签', signType: '中上签',
keywords: '稳中求进、审时度势、蓄势待发', keywords: '稳中求进、审时度势、蓄势待发',
focusPoints: const ['世应有情,主事可成', '动爻化退,宜稳不宜急'],
conclusion: '1. 方向可行\n2. 节奏宜稳', conclusion: '1. 方向可行\n2. 节奏宜稳',
analysis: '当前阶段需先稳住节奏,再做关键推进。', analysis: '当前阶段需先稳住节奏,再做关键推进。',
suggestion: '1. 控节奏\n2. 重复盘', suggestion: '1. 控节奏\n2. 重复盘',