切换语言无需重启
This commit is contained in:
@@ -35,6 +35,12 @@ class MyApp extends StatefulWidget {
|
||||
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
|
||||
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
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
|
||||
Reference in New Issue
Block a user