3

How to make a bottom navbar which the bottom navbar item get data from list?

bottom navbar example from flutter documentation

bottomNavigationBar: BottomNavigationBar(
    items: const <BottomNavigationBarItem>[
      BottomNavigationBarItem(
        icon: Icon(Icons.home),
        label: 'Home',
      ),
      BottomNavigationBarItem(
        icon: Icon(Icons.business),
        label: 'Business',
      ),
      BottomNavigationBarItem(
        icon: Icon(Icons.school),
        label: 'School',
      ),
    ],
    currentIndex: _selectedIndex,
    selectedItemColor: Colors.amber[800],
    onTap: _onItemTapped,
  ),
Soveyyy
  • 274
  • 4
  • 17

3 Answers3

1
  1. Create a custom class
    class MyTabItem {

    String title;
    IconData icon;

    MyTabItem(this.name, this.icon);

    }
  1. Create list of tabs:
List<MyTabItem> _items = [
  MyTabItem('Home', Icons.home),
  MyTabItem('Business', Icons.business),
  MyTabItem('School', Icons.school),
];
  1. Create a method to iterate through the _items collection and returns List<BottomNavigationBarItem>
List<BottomNavigationBarItem> getBottomTabs( List<MyTabItem> tabs) {
    return tabs
        .map(
          (item) =>
          BottomNavigationBarItem(
            icon: Icon(item.icon),
            label: item.title,
          ),
    )
        .toList();
  }
  1. Finally, call the method:
bottomNavigationBar: BottomNavigationBar(
          items: getBottomTabs(items),
         ),

Answer was inspired by @Muldec response here: Flutter: Show different icons based on value

0

try this:

List<String> items = ['Home', 'Business', 'School'];

bottomNavigationBar: BottomNavigationBar(
    items: items.map((item) {
        Widget itemIcon = Icon(Icons.home);

        if(item == 'Home')
        {
          itemIcon = Icon(Icons.home),
        }else if(item == 'Business')
        {
          itemIcon = Icon(Icons.business),
        }else if(item == 'School')
        {
          itemIcon = Icon(Icons.school),
        }

        BottomNavigationBarItem(
          icon: itemIcon ,
          label: item,
        ),
      }).toList(),
    currentIndex: _selectedIndex,
    selectedItemColor: Colors.amber[800],
    onTap: _onItemTapped,
  ),
Jim
  • 6,928
  • 1
  • 7
  • 18
0

Not allowed to comment Jim's message. I used his code (works fine) but needs correction here I think:

    List<String> items = ['Home', 'Business', 'School'];

    bottomNavigationBar: BottomNavigationBar(
        items: items.map((item) {
            Widget itemIcon = Icon(Icons.home);

        if(item == 'Home')
        {
          itemIcon = Icon(Icons.home),
        }else if(item == 'Business')
        {
          itemIcon = Icon(Icons.business),
        }else if(item == 'School')
        {
          itemIcon = Icon(Icons.school),
        }

        return BottomNavigationBarItem(     // add return
          icon: itemIcon ,
          label: item,
        );                                  // replace , with ;
      }).toList(),
    currentIndex: _selectedIndex,
    selectedItemColor: Colors.amber[800],
    onTap: _onItemTapped,
  ),
Shabnam Siddiqui
  • 579
  • 4
  • 13
Nicolas
  • 1
  • 3