因此,我正在构建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,
),
);
}
}
我的详细信息:
因此,我正在构建Flutter应用,其中有四个不同的功能。我非常喜欢BottomNavigationBar的外观以及图标和诸如此类的东西,因此我决定选择该选项。我的方式...
[Column
没有const
构造函数,因此您不能将const
与Column
一起使用(这基本上就是这些错误所说明的内容。)