Search code examples
flutterbottomnavigationview

How to change the bottom navigation bar items based on a condition in Flutter


I have a list of bottom nav bar items: [A, B, C, D]. I want to change this list depending on a condition X.

For example: When the user is logged out, user has the list mentioned [A,B,C,D]. But when the user logs in, the bottom nav bar item list needs to change to [A, B, C, E]. I add and remove the items from the list when the condition X is changed at runtime. However this seems quite incorrect to me and I am not sure if this is the way to go about this issue.

Sometimes there is the issue where the list does not update and stays [A,B,C,D]. Should I have two separate lists for the items perhaps?

Any help is appreciated, thanks a bunch.


Solution

  • You can simply make a function of return type List.

    List navBarItems (){
    
      if(your condition X){
    
        return ["item1","item2","item3"];
      }else{
        return ["item1","item2"];
      }
    
    }
    

    Then place it in bottom nav bar items: navBarItems(). or simply

    bottomnavbaritems:(condition X)?["item1","item2","item3"]: ["item1","item2"];
    

    And use setState() to change the conditions.