我做了编写你的第一个 Flutter App,第 2 部分教程。
让我们考虑一下从本教程的应用程序中提取的这段代码。
return Scaffold(
appBar: AppBar(
title: Text('Saved Words'),
),
body: ListView(children: divided),
);
为什么我不能写这样的东西:
return Scaffold(
appBar: AppBar(
title: Text('Saved Words'),
),
body: () { if (true) {
ListView(children: divided);
}
},
);
在定义小部件属性时不能放置 If 语句吗?
您可以使用三元运算符:
body: _isTrue
? ListView(children: divided)
: <some other widget>;
你不能。但是看看下面的代码,这允许你做基本上相同的事情。
return Scaffold(
appBar: AppBar(
title: Text('Saved Words'),
),
body: variable == 2 ? Container() : Center()
);
为澄清起见,编辑语法如下:
布尔值评估 ? 如果为真:如果为假