Flutter底部导航栏项目标签变量

2fjabf4q  于 2023-02-05  发布在  Flutter
关注(0)|答案(1)|浏览(134)

尝试在BottomNavigationBarItem的“标签”中设置变量时,在**标签中出现错误:myVariable.**似乎只允许文本为“myText”。有什么办法强制一个变量值?

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(),
          );
        });
    }
  },
);

}
我尝试了myVariable.toString()...但不起作用

epfja78i

epfja78i1#

因此,您的问题在于:

const <BottomNavigationBarItem>

这是不允许它下面的任何东西是一个变量。2如果你删除“const”关键字,它应该工作!
请参见此代码片段https://dartpad.dev/?id=bbd3f10c2593f0add04dd770318b33f7

相关问题