按下按钮时,我需要以编程方式更改应用程序区域设置
我的代码:
MaterialApp(
localizationsDelegates: [
_newLocaleDelegate,
const AppTranslationsDelegate(),
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: [
const Locale('en', ''),
const Locale('ar', ''),
],
locale: mylocale,
title: appTitle,
home: Scaffold(
body: new MyCustomForm(),
),
debugShowCheckedModeBanner: false
);
new MaterialButton(
onPressed: () {
setState(() {
mylocale=Locale("ar","");
_newLocaleDelegate = AppTranslationsDelegate(newLocale:mylocale);
});
},
),
翻译代码:
AppTranslations.of(context).text("text")
AppTranslations类:
class AppTranslations {
Locale locale;
static Map<dynamic, dynamic> _localisedValues;
AppTranslations(Locale locale) {
this.locale = locale;
_localisedValues = null;
}
static AppTranslations of(BuildContext context) {
return Localizations.of<AppTranslations>(context, AppTranslations);
}
static Future<AppTranslations> load(Locale locale) async {
AppTranslations appTranslations = AppTranslations(locale);
String jsonContent =
await rootBundle.loadString("assets/locale/localization_${locale.languageCode}.json");
_localisedValues = json.decode(jsonContent);
return appTranslations;
}
get currentLanguage => locale.languageCode;
String text(String key) {
print(key);
if(_localisedValues!=null)
return _localisedValues[key] ?? "$key";
else
return key;
}
}
我的问题:
当区域设置更改页面方向更改为不翻译时, 要获得效果翻译,需要刷新页面或转到另一页面并返回,
任何帮助
答案 0 :(得分:0)
您可以尝试将MaterialApp
包裹在AnimatedSwitcher中吗?
AnimatedSwitcher(
// Following two fields for your reference
// duration: const Duration(milliseconds: 500),
// transitionBuilder: (Widget child, Animation<double> animation) {
// return ScaleTransition(child: child, scale: animation);
// },
child: MaterialApp(
// As before, the same code, however:
key: ValueKey<Locale>(mylocale),
)
)
顺便说一句,为私有变量加上下划线前缀的好习惯,例如_myLocale
。