多语言支持

This commit is contained in:
ytc1012
2025-11-24 11:25:33 +08:00
parent 2c6ced5c14
commit 4444c401b9
14 changed files with 672 additions and 167 deletions

View File

@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../l10n/app_localizations.dart';
import '../theme/app_colors.dart';
import '../theme/app_text_styles.dart';
@@ -13,7 +14,20 @@ class SettingsScreen extends StatefulWidget {
return prefs.getInt(_durationKey) ?? 25;
}
/// Get the saved locale
static Future<String?> getSavedLocale() async {
final prefs = await SharedPreferences.getInstance();
return prefs.getString(_localeKey);
}
/// Save the locale
static Future<void> saveLocale(String localeCode) async {
final prefs = await SharedPreferences.getInstance();
await prefs.setString(_localeKey, localeCode);
}
static const String _durationKey = 'default_duration';
static const String _localeKey = 'app_locale';
@override
State<SettingsScreen> createState() => _SettingsScreenState();
@@ -21,6 +35,7 @@ class SettingsScreen extends StatefulWidget {
class _SettingsScreenState extends State<SettingsScreen> {
int _selectedDuration = 25; // Default
String _selectedLocale = 'en'; // Default
final List<int> _durationOptions = [15, 25, 45];
@@ -28,6 +43,7 @@ class _SettingsScreenState extends State<SettingsScreen> {
void initState() {
super.initState();
_loadSavedDuration();
_loadSavedLocale();
}
Future<void> _loadSavedDuration() async {
@@ -37,6 +53,13 @@ class _SettingsScreenState extends State<SettingsScreen> {
});
}
Future<void> _loadSavedLocale() async {
final prefs = await SharedPreferences.getInstance();
setState(() {
_selectedLocale = prefs.getString(SettingsScreen._localeKey) ?? 'en';
});
}
Future<void> _saveDuration(int duration) async {
final prefs = await SharedPreferences.getInstance();
await prefs.setInt(SettingsScreen._durationKey, duration);
@@ -45,12 +68,30 @@ class _SettingsScreenState extends State<SettingsScreen> {
});
}
Future<void> _saveLocale(String localeCode) async {
await SettingsScreen.saveLocale(localeCode);
setState(() {
_selectedLocale = localeCode;
});
// Show snackbar to inform user to restart
if (!mounted) return;
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(AppLocalizations.of(context)!.onboardingReset),
duration: const Duration(seconds: 2),
),
);
}
@override
Widget build(BuildContext context) {
final l10n = AppLocalizations.of(context)!;
return Scaffold(
backgroundColor: AppColors.background,
appBar: AppBar(
title: const Text('Settings'),
title: Text(l10n.settings),
backgroundColor: AppColors.background,
),
body: ListView(
@@ -58,31 +99,43 @@ class _SettingsScreenState extends State<SettingsScreen> {
children: [
// Focus Duration Section
_buildSection(
title: 'Focus Settings',
title: l10n.focusSettings,
children: [
Padding(
padding: const EdgeInsets.only(bottom: 16),
child: Text(
'Default Focus Duration',
l10n.defaultFocusDuration,
style: AppTextStyles.bodyText,
),
),
..._durationOptions.map((duration) {
return _buildDurationOption(duration);
return _buildDurationOption(l10n, duration);
}),
],
),
const SizedBox(height: 32),
// Language Section
_buildSection(
title: l10n.language,
children: [
_buildLanguageOption(l10n, 'en', l10n.english),
const Divider(color: AppColors.divider),
_buildLanguageOption(l10n, 'zh', l10n.chinese),
],
),
const SizedBox(height: 32),
// About Section
_buildSection(
title: 'About',
title: l10n.about,
children: [
ListTile(
contentPadding: EdgeInsets.zero,
title: Text(
'Privacy Policy',
l10n.privacyPolicy,
style: AppTextStyles.bodyText,
),
trailing: const Icon(
@@ -98,7 +151,7 @@ class _SettingsScreenState extends State<SettingsScreen> {
ListTile(
contentPadding: EdgeInsets.zero,
title: Text(
'About FocusBuddy',
l10n.aboutFocusBuddy,
style: AppTextStyles.bodyText,
),
trailing: const Icon(
@@ -114,7 +167,7 @@ class _SettingsScreenState extends State<SettingsScreen> {
ListTile(
contentPadding: EdgeInsets.zero,
title: Text(
'Reset Onboarding',
l10n.resetOnboarding,
style: AppTextStyles.bodyText.copyWith(
color: AppColors.textSecondary,
),
@@ -136,7 +189,7 @@ class _SettingsScreenState extends State<SettingsScreen> {
// Version info
Center(
child: Text(
'Version 1.0.0 (MVP)',
l10n.version,
style: AppTextStyles.helperText.copyWith(fontSize: 12),
),
),
@@ -174,7 +227,7 @@ class _SettingsScreenState extends State<SettingsScreen> {
);
}
Widget _buildDurationOption(int duration) {
Widget _buildDurationOption(AppLocalizations l10n, int duration) {
final isSelected = _selectedDuration == duration;
return GestureDetector(
@@ -222,7 +275,7 @@ class _SettingsScreenState extends State<SettingsScreen> {
// Duration text
Expanded(
child: Text(
'$duration minutes',
l10n.minutesValue(duration, l10n.minutes(duration)),
style: TextStyle(
fontFamily: 'Nunito',
fontSize: 16,
@@ -245,9 +298,9 @@ class _SettingsScreenState extends State<SettingsScreen> {
color: AppColors.success.withValues(alpha: 0.1),
borderRadius: BorderRadius.circular(8),
),
child: const Text(
'Default',
style: TextStyle(
child: Text(
l10n.defaultLabel,
style: const TextStyle(
fontFamily: 'Nunito',
fontSize: 12,
fontWeight: FontWeight.w600,
@@ -261,25 +314,47 @@ class _SettingsScreenState extends State<SettingsScreen> {
);
}
Widget _buildLanguageOption(AppLocalizations l10n, String localeCode, String label) {
final isSelected = _selectedLocale == localeCode;
return ListTile(
contentPadding: EdgeInsets.zero,
title: Text(
label,
style: TextStyle(
fontFamily: 'Nunito',
fontSize: 16,
fontWeight: isSelected ? FontWeight.w600 : FontWeight.w400,
color: isSelected ? AppColors.primary : AppColors.textPrimary,
),
),
trailing: isSelected
? const Icon(
Icons.check_circle,
color: AppColors.primary,
)
: null,
onTap: () => _saveLocale(localeCode),
);
}
void _showPrivacyPolicy() {
final l10n = AppLocalizations.of(context)!;
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text('Privacy Policy'),
title: Text(l10n.privacyPolicyTitle),
content: SingleChildScrollView(
child: Text(
'FocusBuddy is 100% offline. We do not collect your name, email, '
'location, or usage data. All sessions stay on your device.\n\n'
'There is no cloud sync, no account system, and no analytics tracking.\n\n'
'For the full privacy policy, visit:\n'
'[Your website URL]/privacy',
l10n.privacyPolicyContent,
style: AppTextStyles.bodyText,
),
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('Close'),
child: Text(l10n.close),
),
],
),
@@ -287,18 +362,20 @@ class _SettingsScreenState extends State<SettingsScreen> {
}
void _showAboutDialog() {
final l10n = AppLocalizations.of(context)!;
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text('About FocusBuddy'),
title: Text(l10n.aboutTitle),
content: SingleChildScrollView(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'FocusBuddy',
style: TextStyle(
Text(
l10n.appTitle,
style: const TextStyle(
fontFamily: 'Nunito',
fontSize: 20,
fontWeight: FontWeight.w700,
@@ -307,21 +384,17 @@ class _SettingsScreenState extends State<SettingsScreen> {
),
const SizedBox(height: 8),
Text(
'A gentle focus timer for neurodivergent minds',
l10n.aboutSubtitle,
style: AppTextStyles.bodyText,
),
const SizedBox(height: 16),
Text(
'"Focus is not about never getting distracted — '
'it\'s about gently coming back every time you do."',
l10n.aboutQuote,
style: AppTextStyles.encouragementQuote,
),
const SizedBox(height: 16),
Text(
'✨ No punishment for distractions\n'
'💚 Encouragement over criticism\n'
'🔒 100% offline and private\n'
'🌱 Made with care',
l10n.aboutFeatures,
style: AppTextStyles.bodyText,
),
],
@@ -330,7 +403,7 @@ class _SettingsScreenState extends State<SettingsScreen> {
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('Close'),
child: Text(l10n.close),
),
],
),
@@ -338,18 +411,20 @@ class _SettingsScreenState extends State<SettingsScreen> {
}
void _resetOnboarding() async {
final l10n = AppLocalizations.of(context)!;
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text('Reset Onboarding?'),
title: Text(l10n.resetOnboardingTitle),
content: Text(
'This will show the onboarding screens again when you restart the app.',
l10n.resetOnboardingMessage,
style: AppTextStyles.bodyText,
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('Cancel'),
child: Text(l10n.cancel),
),
TextButton(
onPressed: () async {
@@ -360,13 +435,13 @@ class _SettingsScreenState extends State<SettingsScreen> {
Navigator.pop(context);
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Onboarding reset. Restart the app to see it again.'),
duration: Duration(seconds: 3),
SnackBar(
content: Text(l10n.onboardingReset),
duration: const Duration(seconds: 3),
),
);
},
child: const Text('Reset'),
child: Text(l10n.reset),
),
],
),