无法与BottomNavigationBar(Flutter 1.9.1)组合显示列]]

问题描述 投票:0回答:1

因此,我正在构建Flutter应用,其中有四个不同的功能。我非常喜欢BottomNavigationBar的外观以及图标和诸如此类的东西,因此我决定选择该选项。我的实现方式如下:我声明一个static const List<Widget> elements = <Widget>[ ... ],其中目前我只有四个Text()小部件。我还声明了一个变量selectedIndex,以便可以跟踪应显示的元素。然后,在我的脚手架中,我将bottomNavigationBar设置为BottomNavigationBar(),其中我有一个BottomNavigationBarItems的const列表,我想其余参数是显而易见的。当我重新添加const,但保持Column原样时,我得到以下信息:

Compiler message:
lib/main.dart:39:5: Error: Cannot invoke a non-'const' constructor where a const expression 
is expected.
Try using a constructor or factory that is 'const'.
Column(
^^^^^^

现在是问题所在:

[当我尝试在elements列表中放入一列时,出现一些不同的错误:

Const variables must be initialized with a constant value.
The constructor being called isn't a const constructor

但是我确实需要两个小工具,一个在另一个小工具上方,以使我的应用正常工作。有谁知道我该怎么做?

我尝试过的事情:

我尝试将Column作为一个单独的变量,但没有用,并且给了我同样的错误。当我从const列表中删除elenents以便添加列时,它没有引发错误,但它也没有同时显示我放入该列中的所有内容。

我的代码(至少相关的位):
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  int _geselecteerdeIndex = 0;
  static const TextStyle tekstStijl = TextStyle(fontSize: 30, fontWeight: FontWeight.bold);

  static const List<Widget> elementen = <Widget>[
    Text(
      "Index 0: Map",
      style: tekstStijl,
    ),
    Text(
      "Index 1: History",
      style: tekstStijl
    ),
    Text(
      "Index 2: Challenges",
      style: tekstStijl,
    ),
    Text(
      "Index 3: Settings",
      style: tekstStijl
    ),

  ];

  void _onItemTapped(int index) {
    setState(() {
      _geselecteerdeIndex = index;
    });
  }

  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Intershitty v.0.1.0'),
        backgroundColor: Colors.amber[300],
      ),
      body: Center(
        child: elementen.elementAt(_geselecteerdeIndex),
      ),
      bottomNavigationBar: BottomNavigationBar(
        items: const <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            icon: Icon(Icons.map),
            title: Text("Map")
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.trending_up),
            title: Text("History")
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.casino),
            title: Text("Challenges")
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.settings),
            title: Text("Settings")
          )
        ],
        currentIndex: _geselecteerdeIndex,
        selectedItemColor: Colors.amber[800],
        onTap: _onItemTapped,
        backgroundColor: Colors.grey[300],
        type: BottomNavigationBarType.fixed,
        showUnselectedLabels: false,
      ),
    );
  }
}

我的详细信息:

  • OS: Ubuntu 18.04 64位
  • Flutter版本:
  • 1.9.1
  • 我用于测试的设备:
  • 只是用于x86的Android模拟器

因此,我正在构建Flutter应用,其中有四个不同的功能。我非常喜欢BottomNavigationBar的外观以及图标和诸如此类的东西,因此我决定选择该选项。我的方式...

android flutter flutter-layout
1个回答
0
投票

[Column没有const构造函数,因此您不能将constColumn一起使用(这基本上就是这些错误所说明的内容。)

© www.soinside.com 2019 - 2024. All rights reserved.