Search code examples
flutterdartflutter-widget

How to localize BottomNavigationBarItem labels


Is there any workaround to translate the BottomNavigationBarItem into different languages? I use Lang.getString(context, key) to access a map(loaded from a json file) to fetch the right translation, it doesn't make any sense to hard code the label like this, label: "Home". what should I do?

PageView(
        controller: pageController,
        onPageChanged: _pageSwipped,
        children: <Widget>[Page1()],
      ),
      bottomNavigationBar: BottomNavigationBar(
        items: const <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            icon: Icon(Icons.list),
            label: Lang.getString(context, key), // it doesn't work because it should be constant.
          ),
        ],
        currentIndex: _currenIndex,
        selectedItemColor: Colors.blue,
        iconSize: _deviceSize.size.height * 0.046,
        selectedFontSize: _deviceSize.size.height * 0.023,
        unselectedItemColor: Colors.grey,
        onTap: _bottomTapped,
      )

Solution

  • Remove const keyword from line

     items: const <BottomNavigationBarItem>[
     ......
     ......
     ]