【问题标题】:Can't display a Column in combination with a BottomNavigationBar (Flutter 1.9.1)无法与 BottomNavigationBar 结合显示 Column (Flutter 1.9.1)
【发布时间】:2019-10-06 08:15:57
【问题描述】:

所以我正在构建一个 Flutter 应用程序,其中有四个不同的功能。 我非常喜欢 BottomNavigationBar 的外观和图标等等,所以我决定选择那个选项。 我实现它的方式如下:我声明了一个static const List<Widget> elements = <Widget>[ ... ],其中我目前只有四个Text() 小部件。我还声明了一个变量selectedIndex,这样我就可以跟踪应该显示什么元素。然后,在我的 Scaffold 中,我将 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 作为一个单独的变量,但它不起作用并给了我同样的错误。 当我从 elenents 列表中删除 const 以便可以添加 Column 时,它没有抛出错误,但它也没有显示我放入列中的两件事。

我的代码(至少是相关位):

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

我的详细信息:

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

【问题讨论】:

    标签: android flutter flutter-layout


    【解决方案1】:

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

    这应该可行:

    final List<Widget> elementen = <Widget>[
      Column(
        children: <Widget>[
          Text(
            "Index 0: Map",
            style: tekstStijl,
          ),
          FlutterLogo(),
        ],
      ),
      Column(
        children: <Widget>[
          Text("Index 1: History", style: tekstStijl),
          FlutterLogo(),
        ],
      ),
      Column(
        children: <Widget>[
          Text(
            "Index 2: Challenges",
            style: tekstStijl,
          ),
          FlutterLogo(),
        ],
      ),
      Column(
        children: <Widget>[
          Text("Index 3: Settings", style: tekstStijl),
          FlutterLogo(),
        ],
      ),
    ];
    

    【讨论】:

    • 天哪,我怎么会这么瞎?非常感谢
    猜你喜欢
    • 1970-01-01
    • 2020-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-06
    • 2021-06-26
    • 2020-01-14
    相关资源
    最近更新 更多