BottomNavigationBar 项目的 Flutter 本地化

标签 flutter localization bottomnavigationview

我正在创建一个本地化应用程序,在主页上带有底部导航栏。 如果我更改本地化,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/

相关文章:

flutter - 为什么 flutter 在 url 启动器中给我 url null ?

Flutter Web 富文本编辑器

localization - 删除 Liferay 主题中的本地化

java - 我的 OnNavigationItemSelectedListener kotlin android 有什么问题吗?

android - 如何修复错误 'NoSuchMethodError: The getter ' focusScopeNode' was called on null'?

image - 将图像从 Firebase 下载到 Flutter

iphone - 在保持货币格式的同时使用缩写缩短货币数字

asp.net-mvc - 在 Asp.Net Core 中启用基于客户端的文化

android - 设计 BottomNavigationView - 在代码中设置背景颜色

android - 为什么Android BottomNavigationView 不显示?