我正在创建一个本地化应用程序,在主页上带有底部导航栏。 如果我更改本地化,BottomNavigationBar 的项目名称不会更新,直到单击其中之一。 如何更新底部栏? 我想念什么?
代码:
@override
Widget build(BuildContext context) {
return BottomNavigationBar(
onTap: onTabTapped,
type: BottomNavigationBarType.fixed,
// new
currentIndex: _currentIndex,
selectedFontSize: 12,
unselectedFontSize: 12,
elevation: 10,
showSelectedLabels: true,
showUnselectedLabels: true,
items: [
new BottomNavigationBarItem(
icon: Icon(MyFlutterAppIOS.share_100, color: Colors.blueGrey),
title: Text(
allTranslations.text("sharetitle"),
style: TextStyle(
color: Colors.blueGrey,
),
),
),
new BottomNavigationBarItem(
icon: Icon(MyFlutterAppIOS.protect_100, color: Colors.blueGrey),
title: Text(
allTranslations.text("lblprivacypolicy"),
style: TextStyle(
color: Colors.blueGrey,
),
),
),
new BottomNavigationBarItem(
icon: Icon(MyFlutterAppIOS.protect_100, color: Colors.blueGrey),
title: Text(
allTranslations.text("lblterms"),
style: TextStyle(
color: Colors.blueGrey,
),
),
),
new BottomNavigationBarItem(
icon: Icon(MyFlutterAppIOS.info_100, color: Colors.blueGrey),
title: Text(
allTranslations.text("lblinfo"),
style: TextStyle(color: Colors.blueGrey),
),
)
],
);
}
最佳答案
我将 BottomNavigationBar 包装到 Localizations.override 时解决了这个问题
bottomNavigationBar: Localizations.override(
context: context,
locale: context.locale,
child: CustomBottomNavigationBar(
),
),
元素位于构建方法内部也很重要......例如:
Widget build(BuildContext context) {
List<_BottomNavigationBarItem> items = [
_BottomNavigationBarItem(
iconSelectedPath: AppIcons.report_dark,
iconNoSelectedPath: AppIcons.report_white,
title: LocaleKeys.report.tr(),
),
_BottomNavigationBarItem(
iconSelectedPath: AppIcons.home_dark,
iconNoSelectedPath: AppIcons.home_white,
title: LocaleKeys.home.tr(),
),
_BottomNavigationBarItem(
iconSelectedPath: AppIcons.settings_dark,
iconNoSelectedPath: AppIcons.settings_white,
title: LocaleKeys.settings.tr(),
),
];
...
}
关于BottomNavigationBar 项目的 Flutter 本地化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61549139/