因此,我正在构建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
但是我确实需要两个Widget,一个在另一个之上,以便我的应用正常运行。 有人知道我该怎么做吗?
我尝试将Column作为一个单独的变量,但没有用,并给了我同样的错误。
当我从const
列表中删除elenents
以便可以添加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,
),
);
}
}
我的详细信息:
答案 0 :(得分:0)
Column
没有const
构造函数,因此您不能将const
与Column
s一起使用(基本上就是这些错误所说的)。
这应该有效:
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(),
],
),
];