Home > Software engineering >  Flutter BottomNavigationBarItem label variable
Flutter BottomNavigationBarItem label variable

Time:02-03

Wehn trying to set a vaiable in the "label" of BottomNavigationBarItem it gives an error in label: myVariable. It seems only allows text as "myText". Is there any way force a variable vlaue?

Widget build(BuildContext context) {
String myVariable = "Item1";
return CupertinoTabScaffold(
  tabBar: CupertinoTabBar(
    backgroundColor: Color.fromARGB(255, 0, 0, 0),
    activeColor: Color.fromARGB(255, 255, 255, 255),
    inactiveColor: Color.fromARGB(255, 160, 160, 160),
    items: const <BottomNavigationBarItem>[
      BottomNavigationBarItem(
        icon: Icon(Icons.build_outlined, size: 22),
        **label: myVariable,**
        activeIcon: Icon(Icons.build),
      ),
      BottomNavigationBarItem(
        icon: Icon(Icons.school_outlined, size: 22),
        label: "Item2",
        activeIcon: Icon(Icons.school),
      ),
    ],
  ),
  tabBuilder: (context, index) {
    switch (index) {
      case 0:
        return CupertinoTabView(builder: (context) {
          return CupertinoPageScaffold(
            child: list_maquina_1(),
          );
        });
      case 1:
        return CupertinoTabView(builder: (context) {
          return CupertinoPageScaffold(
            child: courses_page(),
          );
        });
      default:
        return CupertinoTabView(builder: (context) {
          return CupertinoPageScaffold(
            child: list_maquina_1(),
          );
        });
    }
  },
);

}

I tried myVariable.toString() ... but not worked

CodePudding user response:

So your problem is with the following :

const <BottomNavigationBarItem>

this is not allowing any of the things underneath it to be a variable. If you remove that "const" keyword it should work!

See this code snippet https://dartpad.dev/?id=bbd3f10c2593f0add04dd770318b33f7

  • Related