【问题标题】:navigation bar item's label from array导航栏项目的标签来自数组
【发布时间】:2022-01-20 02:26:52
【问题描述】:

我正在颤振中创建一个底部导航栏。我想从数组中选择标签。以下程序引发错误“const 列表文字中的值必须是常量”。我知道脚手架中的导航栏项目列表被声明为 const,我们不能使用非常量字符串数组。如何解决这个问题?

class _MyHomePageState extends State<MyHomePage> {
  int _currentIndex = 0;
  // final List _children = [];
  Text titleText = new Text('Pensor');
  var tabLabels =  ['Home', 'Water', 'Insights', 'Cabliration', 'Settings'];

  @override
  Widget build(BuildContext context) {
    // This method is rerun every time setState is called, for instance as done
    // by the _incrementCounter method above.
    //
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
    return Scaffold(
      appBar: AppBar(
        title: titleText,
      ),
      // body: _children[_currentIndex],
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        onTap: onTabTapped,
        currentIndex: _currentIndex,
        items: const <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            icon: Icon(Icons.home),
            label: tabLabels[0], // <-- Here is the error. The tabLabels are not const. 
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.water_damage),
            label: 'Water',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.insights),
            label: 'Insights',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.biotech),
            label: 'Calibration',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.settings),
            label: 'Settings',
          ),

        ],
      ),
    );
  }

【问题讨论】:

    标签: flutter


    【解决方案1】:

    只需删除 const

    items: <BottomNavigationBarItem>[
              BottomNavigationBarItem(
                icon: Icon(Icons.home),
                label: tabLabels[0], 
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.water_damage),
                label: 'Water',
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.insights),
                label: 'Insights',
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.biotech),
                label: 'Calibration',
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.settings),
                label: 'Settings',
              ),
    
            ],
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多