feat(settings): 添加通知偏好设置和后端 API 集成

This commit is contained in:
qzl
2026-04-07 18:43:42 +08:00
parent b18a205bf3
commit b22673ce49
9 changed files with 612 additions and 29 deletions
@@ -3,18 +3,18 @@ import 'package:flutter/material.dart';
import '../../../../l10n/app_localizations.dart';
import '../../../../shared/theme/design_tokens.dart';
import '../../data/models/profile_settings.dart';
import 'language_settings_screen.dart';
import '../widgets/settings_section_widgets.dart';
import 'language_settings_screen.dart';
class GeneralSettingsScreen extends StatefulWidget {
const GeneralSettingsScreen({
super.key,
required this.settings,
required this.onInterfaceLanguageChanged,
required this.onSettingsChanged,
});
final ProfileSettingsV1 settings;
final Future<void> Function(String languageTag) onInterfaceLanguageChanged;
final Future<void> Function(ProfileSettingsV1 settings) onSettingsChanged;
@override
State<GeneralSettingsScreen> createState() => _GeneralSettingsScreenState();
@@ -62,15 +62,87 @@ class _GeneralSettingsScreenState extends State<GeneralSettingsScreen> {
children: [
SettingsMenuTile(
icon: Icons.language_rounded,
title: l10n.language,
title: l10n.settingsInterfaceLanguage,
subtitle: displayLanguageLabel(
l10n,
_settings.preferences.interfaceLanguage,
),
tint: colors.primary,
background: colors.surfaceContainerHighest,
onTap: () => _selectLanguage(
_settings.preferences.interfaceLanguage,
(lang) => setState(() {
_settings = _settings.copyWith(
preferences: _settings.preferences.copyWith(
interfaceLanguage: lang,
),
);
}),
),
),
SettingsMenuTile(
icon: Icons.smart_toy_rounded,
title: l10n.settingsAiLanguage,
subtitle: displayLanguageLabel(
l10n,
_settings.preferences.aiLanguage,
),
tint: colors.primary,
background: colors.surfaceContainerHighest,
showDivider: true,
onTap: () => _selectLanguage(
_settings.preferences.aiLanguage,
(lang) => setState(() {
_settings = _settings.copyWith(
preferences: _settings.preferences.copyWith(
aiLanguage: lang,
),
);
}),
),
),
SettingsMenuTile(
icon: Icons.access_time_rounded,
title: l10n.settingsTimezone,
subtitle: _settings.preferences.timezone,
tint: colors.primary,
background: colors.surfaceContainerHighest,
showDivider: true,
onTap: () => _selectTimezone(context),
),
SettingsMenuTile(
icon: Icons.public_rounded,
title: l10n.settingsCountry,
subtitle: _settings.preferences.country,
tint: colors.primary,
background: colors.surfaceContainerHighest,
showDivider: false,
onTap: _openLanguageSettings,
onTap: () => _selectCountry(context),
),
],
),
const SizedBox(height: AppSpacing.lg),
SectionLabel(text: l10n.settingsSectionNotification),
SettingsGroupCard(
children: [
SettingsSwitchTile(
icon: Icons.notifications_rounded,
title: l10n.settingsNotificationAllow,
value: _settings.notification.allowNotifications,
tint: colors.primary,
background: colors.surfaceContainerHighest,
onChanged: (value) =>
_updateNotification(allowNotifications: value),
),
SettingsSwitchTile(
icon: Icons.vibration_rounded,
title: l10n.settingsNotificationVibration,
value: _settings.notification.allowVibration,
tint: colors.primary,
background: colors.surfaceContainerHighest,
showDivider: false,
onChanged: (value) =>
_updateNotification(allowVibration: value),
),
],
),
@@ -80,25 +152,181 @@ class _GeneralSettingsScreenState extends State<GeneralSettingsScreen> {
);
}
Future<void> _openLanguageSettings() async {
Future<void> _selectLanguage(
String currentLanguage,
void Function(String) onChanged,
) async {
final result = await Navigator.of(context).push<String>(
MaterialPageRoute<String>(
builder: (_) => LanguageSettingsScreen(
selectedLanguageTag: _settings.preferences.interfaceLanguage,
builder: (_) =>
LanguageSettingsScreen(selectedLanguageTag: currentLanguage),
),
);
if (result == null || result == currentLanguage) {
return;
}
onChanged(result);
await widget.onSettingsChanged(_settings);
}
Future<void> _selectTimezone(BuildContext context) async {
final result = await Navigator.of(context).push<String>(
MaterialPageRoute<String>(
builder: (_) => TimezoneSettingsScreen(
selectedTimezone: _settings.preferences.timezone,
),
),
);
if (result == null || result == _settings.preferences.interfaceLanguage) {
return;
}
await widget.onInterfaceLanguageChanged(result);
if (!mounted) {
if (result == null || result == _settings.preferences.timezone) {
return;
}
setState(() {
_settings = _settings.copyWith(
preferences: _settings.preferences.copyWith(interfaceLanguage: result),
preferences: _settings.preferences.copyWith(timezone: result),
);
});
await widget.onSettingsChanged(_settings);
}
Future<void> _selectCountry(BuildContext context) async {
final result = await Navigator.of(context).push<String>(
MaterialPageRoute<String>(
builder: (_) => CountrySettingsScreen(
selectedCountry: _settings.preferences.country,
),
),
);
if (result == null || result == _settings.preferences.country) {
return;
}
setState(() {
_settings = _settings.copyWith(
preferences: _settings.preferences.copyWith(country: result),
);
});
await widget.onSettingsChanged(_settings);
}
void _updateNotification({bool? allowNotifications, bool? allowVibration}) {
final newNotification = _settings.notification.copyWith(
allowNotifications: allowNotifications,
allowVibration: allowVibration,
);
final newSettings = _settings.copyWith(notification: newNotification);
setState(() {
_settings = newSettings;
});
widget.onSettingsChanged(newSettings);
}
}
class TimezoneSettingsScreen extends StatelessWidget {
const TimezoneSettingsScreen({super.key, required this.selectedTimezone});
final String selectedTimezone;
static const _timezones = [
'Asia/Shanghai',
'Asia/Hong_Kong',
'Asia/Tokyo',
'America/New_York',
'America/Los_Angeles',
'Europe/London',
'Europe/Paris',
];
@override
Widget build(BuildContext context) {
final l10n = AppLocalizations.of(context)!;
final colors = Theme.of(context).colorScheme;
return Scaffold(
backgroundColor: colors.surfaceContainerLow,
appBar: AppBar(
title: Text(l10n.settingsTimezone),
centerTitle: true,
backgroundColor: colors.surfaceContainerLow,
surfaceTintColor: colors.surfaceContainerLow,
),
body: ListView(
padding: const EdgeInsets.all(AppSpacing.lg),
children: [
SettingsGroupCard(
children: [
for (int i = 0; i < _timezones.length; i++)
SettingsMenuTile(
icon: Icons.access_time_rounded,
title: _timezones[i],
subtitle: '',
tint: colors.primary,
background: colors.surfaceContainerHighest,
showDivider: i != _timezones.length - 1,
showChevron: false,
trailing: selectedTimezone == _timezones[i]
? Icon(Icons.check_rounded, color: colors.primary)
: null,
onTap: () => Navigator.of(context).pop(_timezones[i]),
),
],
),
],
),
);
}
}
class CountrySettingsScreen extends StatelessWidget {
const CountrySettingsScreen({super.key, required this.selectedCountry});
final String selectedCountry;
static const _countries = ['CN', 'HK', 'TW', 'US', 'JP', 'GB', 'FR'];
static const _labels = [
'China',
'Hong Kong',
'Taiwan',
'USA',
'Japan',
'UK',
'France',
];
@override
Widget build(BuildContext context) {
final l10n = AppLocalizations.of(context)!;
final colors = Theme.of(context).colorScheme;
return Scaffold(
backgroundColor: colors.surfaceContainerLow,
appBar: AppBar(
title: Text(l10n.settingsCountry),
centerTitle: true,
backgroundColor: colors.surfaceContainerLow,
surfaceTintColor: colors.surfaceContainerLow,
),
body: ListView(
padding: const EdgeInsets.all(AppSpacing.lg),
children: [
SettingsGroupCard(
children: [
for (int i = 0; i < _countries.length; i++)
SettingsMenuTile(
icon: Icons.public_rounded,
title: _labels[i],
subtitle: _countries[i],
tint: colors.primary,
background: colors.surfaceContainerHighest,
showDivider: i != _countries.length - 1,
showChevron: false,
trailing: selectedCountry == _countries[i]
? Icon(Icons.check_rounded, color: colors.primary)
: null,
onTap: () => Navigator.of(context).pop(_countries[i]),
),
],
),
],
),
);
}
}
@@ -81,9 +81,7 @@ class PrivacyNotificationSettingsScreen extends StatelessWidget {
SettingsMenuTile(
icon: Icons.notifications_outlined,
title: l10n.settingsNotificationSystem,
subtitle: l10n.settingsPlaceholderState(
settings.notification.length,
),
subtitle: l10n.settingsPlaceholderState(1),
tint: colors.secondary,
background: colors.surfaceContainerHighest,
onTap: () => _openPlaceholder(