android - 如何在不使用 Listview 的情况下在颤动中循环按钮小部件
问题描述
我想循环一个按钮,我正在使用此代码,但它显示错误,我被困在这里 1 天谢谢
我想使用 for 循环,因为这个数据是动态的。
showDialog(
barrierDismissible: true,
context: context,
builder: (BuildContext context) {
// return object of type Dialog
return CupertinoAlertDialog(
title: Text('Add Location'),
actions: <Widget>[
for (var q = 1;q<=2;q++){
FlatButton(
child: new Text("Location A"),
onPressed: () {
Navigator.of(context).pop();
locationA = 'Location A';
},
),
}
],
);
},
);```
解决方案
我创建了一个简单的方法,希望能满足您的需求。该方法返回一个列表,该列表使用循环将项目添加到列表中。最后,它返回填充的列表。
showDialog(
barrierDismissible: true,
context: context,
builder: (BuildContext context) {
// return object of type Dialog
return CupertinoAlertDialog(
title: Text('Add Location'),
actions: _getList(), // try with or without the ()'s
);
},
);
// the method
List<Widget> _getList() {
List<Widget> temp = [];
for (var q = 1; q<=2; q++) {
temp.add(
FlatButton(
child: new Text("Location A"),
onPressed: () {
Navigator.of(context).pop();
locationA = 'Location A';
},
);
);
}
return temp;
}
推荐阅读
- c# - 使用 * 星号定义未知文件夹路径的路径
- authentication - 如何解决此服务帐户创建错误?
- mysql - 未找到 last_name 值时查询不返回任何内容
- python - 具有特定于值的颜色图的 Python imshow
- javascript - 在 JavaScript 的 ajax 中请求的资源上不存在“Access-Control-Allow-Origin”标头
- javascript - 国际电话输入从另一个下拉列表更改下拉拨号代码
- python - 在 cmd & vscode 中显示 RTL 语言
- docker - 'LibreSSL SSL_connect: SSL_ERROR_SYSCALL' on curl 到 Docker 容器上的 NGINX 反向代理
- php - 将字符串与php中的数字进行比较时感到困惑
- ruby-on-rails - 如何在 rspec 中为非 crud 操作和私有方法编写测试用例?