切换语言无需重启

This commit is contained in:
ytc1012
2025-11-24 11:45:41 +08:00
parent 6557e1f911
commit bf21dba275
3 changed files with 18 additions and 8 deletions

View File

@@ -31,7 +31,8 @@
"Bash(flutter analyze:*)", "Bash(flutter analyze:*)",
"Bash(start \"\" \"f:\\cursor-auto\\focusBuddy\\onboarding-preview.html\")", "Bash(start \"\" \"f:\\cursor-auto\\focusBuddy\\onboarding-preview.html\")",
"Bash(flutter emulators:*)", "Bash(flutter emulators:*)",
"Bash(flutter gen-l10n:*)" "Bash(flutter gen-l10n:*)",
"Bash(/proc/$PPID/fd/0)"
], ],
"deny": [], "deny": [],
"ask": [] "ask": []

View File

@@ -35,6 +35,12 @@ class MyApp extends StatefulWidget {
required this.encouragementService, required this.encouragementService,
}); });
// Static method to access the state from anywhere (returns void to avoid exposing private type)
static void updateLocale(BuildContext context, String localeCode) {
final state = context.findAncestorStateOfType<_MyAppState>();
state?.updateLocale(localeCode);
}
@override @override
State<MyApp> createState() => _MyAppState(); State<MyApp> createState() => _MyAppState();
} }
@@ -68,6 +74,13 @@ class _MyAppState extends State<MyApp> {
} }
} }
// Method to update locale from settings
void updateLocale(String localeCode) {
setState(() {
_locale = Locale(localeCode);
});
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return MaterialApp( return MaterialApp(

View File

@@ -3,6 +3,7 @@ import 'package:shared_preferences/shared_preferences.dart';
import '../l10n/app_localizations.dart'; import '../l10n/app_localizations.dart';
import '../theme/app_colors.dart'; import '../theme/app_colors.dart';
import '../theme/app_text_styles.dart'; import '../theme/app_text_styles.dart';
import '../main.dart';
/// Settings Screen - MVP version with duration presets /// Settings Screen - MVP version with duration presets
class SettingsScreen extends StatefulWidget { class SettingsScreen extends StatefulWidget {
@@ -74,14 +75,9 @@ class _SettingsScreenState extends State<SettingsScreen> {
_selectedLocale = localeCode; _selectedLocale = localeCode;
}); });
// Show snackbar to inform user to restart // Update locale immediately without restart
if (!mounted) return; if (!mounted) return;
ScaffoldMessenger.of(context).showSnackBar( MyApp.updateLocale(context, localeCode);
SnackBar(
content: Text(AppLocalizations.of(context)!.onboardingReset),
duration: const Duration(seconds: 2),
),
);
} }
@override @override