区域设置更改后重新加载页面
Reload page after locale change
更改区域设置后 context.setLocale()
非活动小部件中的语言发生变化。但我想不通,只能在新的语言环境中重新加载当前的小部件。有什么让它工作的想法吗?
小部件正文:
Form(
child: CardSettings(
children: <CardSettingsSection>[
CardSettingsSection(
header: CardSettingsHeader(
label: 'settings.general'.tr(),
),
children: [
CardSettingsListPicker(
label: 'Language',
items: context.supportedLocales
.map((locale) =>
locale.toStringWithSeparator(separator: ' '))
.toList(),
initialItem:
context.locale.toStringWithSeparator(separator: ' '),
onChanged: (String newLocale) {
context.setLocale(newLocale.toLocale(separator: ' '));
_getOptions();
},
),
],
),
CardSettingsSection(
header: CardSettingsHeader(
label: 'Map',
),
children: [
CardSettingsListPicker(
label: 'default floor',
items: _floorList,
initialItem: _floor,
onChanged: (String value) {
var floor = tr('plan.floor.' + value);
_preferences.setString('mapDefaultFloor', floor);
},
),
],
),
],
),
);
选项通过在 initState 中调用的异步函数加载,随后在代码中调用...
void _getOptions() {
setState(() {
_floorList = Floor.values
.map((value) => tr('plan.floor.' + value.toString().split('.')[1]))
.toList();
_planTypeList = PlanType.values
.map((value) => tr('plan.type.' + value.toString().split('.')[1]))
.toList();
_floor = tr('plan.floor.' +
(_preferences.getString('mapDefaultFLoor') ?? 'ground'));
_planType = tr('plan.type.' +
(_preferences.getString('mapDefaultType') ?? 'newNumbers'));
});
}
有状态的小部件会在您更新其状态时自动重新加载。因此,你需要做的就是将你的 widget 设置为 StatefulWidget,并使用 setState
函数告诉 flutter 状态已经发生变化,widget 需要重新加载。
取自flutter项目模板:
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
在上面显示的示例中,_counter
在状态中更新,为了使其在应用程序本身上显示正确的值,调用了 setState
函数。
解决方案是使 _getOptions()
异步并使用 await 关键字添加区域设置更改。这样可以确保在加载新值之前更改语言环境...
更改区域设置后 context.setLocale()
非活动小部件中的语言发生变化。但我想不通,只能在新的语言环境中重新加载当前的小部件。有什么让它工作的想法吗?
小部件正文:
Form(
child: CardSettings(
children: <CardSettingsSection>[
CardSettingsSection(
header: CardSettingsHeader(
label: 'settings.general'.tr(),
),
children: [
CardSettingsListPicker(
label: 'Language',
items: context.supportedLocales
.map((locale) =>
locale.toStringWithSeparator(separator: ' '))
.toList(),
initialItem:
context.locale.toStringWithSeparator(separator: ' '),
onChanged: (String newLocale) {
context.setLocale(newLocale.toLocale(separator: ' '));
_getOptions();
},
),
],
),
CardSettingsSection(
header: CardSettingsHeader(
label: 'Map',
),
children: [
CardSettingsListPicker(
label: 'default floor',
items: _floorList,
initialItem: _floor,
onChanged: (String value) {
var floor = tr('plan.floor.' + value);
_preferences.setString('mapDefaultFloor', floor);
},
),
],
),
],
),
);
选项通过在 initState 中调用的异步函数加载,随后在代码中调用...
void _getOptions() {
setState(() {
_floorList = Floor.values
.map((value) => tr('plan.floor.' + value.toString().split('.')[1]))
.toList();
_planTypeList = PlanType.values
.map((value) => tr('plan.type.' + value.toString().split('.')[1]))
.toList();
_floor = tr('plan.floor.' +
(_preferences.getString('mapDefaultFLoor') ?? 'ground'));
_planType = tr('plan.type.' +
(_preferences.getString('mapDefaultType') ?? 'newNumbers'));
});
}
有状态的小部件会在您更新其状态时自动重新加载。因此,你需要做的就是将你的 widget 设置为 StatefulWidget,并使用 setState
函数告诉 flutter 状态已经发生变化,widget 需要重新加载。
取自flutter项目模板:
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
在上面显示的示例中,_counter
在状态中更新,为了使其在应用程序本身上显示正确的值,调用了 setState
函数。
解决方案是使 _getOptions()
异步并使用 await 关键字添加区域设置更改。这样可以确保在加载新值之前更改语言环境...