getx
getx copied to clipboard
After Get.updateLocale is called, how can the current page Controller be reinitialized?
Get.updateLocale(Locale('pt', 'BR'))
Because I have such a scenario now, after calling this function, I need to re-request information from the network, so that the correct text interface can be displayed correctly.
如果你用obx方式的话,你可以尝试一下。
-
在Controller内,创建值
final RxString currentLocale = 'en_US'.obs;
-
更改语言的函数
void changeLocale() {
if (currentLocale.value == 'en_US') {
Get.updateLocale(Locale('zh', 'CN'));
currentLocale.value == 'zh_CN';
} else {
Get.updateLocale(Locale('en', 'US'));
currentLocale.value == 'en_US';
}
- 创建接口
Future<String> getData(String locale) async {
await Future<void>.delayed(const Duration(seconds: 2));
if (param == 'en_US') {
return 'Hello World';
} else {
return '你好世界';
}
}
- 在UI内,使用FutureBuilder部件
Obx(
() => FutureBuilder<String>(
future: mouController.getData(mouController.locale.value),
builder: (BuildContext context, AsyncSnapshot<String> snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return Text(snapshot.data!);
} else {
return const Text('Loading...');
}
},
),
),
如有问题,回复里贴下你的代码哈。