Android 如何在不使用Listview的情况下在颤振中循环按钮小部件
我想循环一个按钮,我正在使用此代码,但它显示了一个错误,我被困在这里1天,谢谢Android 如何在不使用Listview的情况下在颤振中循环按钮小部件,android,ios,flutter,dart,Android,Ios,Flutter,Dart,我想循环一个按钮,我正在使用此代码,但它显示了一个错误,我被困在这里1天,谢谢 我想使用for循环,因为这个数据是动态的 showDialog( barrierDismissible: true, context: context, builder: (BuildContext context) { // return object of type Dialog return CupertinoAl
我想使用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(
是的,
上下文:上下文,
生成器:(BuildContext上下文){
//返回对话框类型的对象
返回CupertinoAlertDialog(
标题:文本(“添加位置”),
行动:[
对于(var q=1;q我创建了一个简单的方法,希望能满足您的需要。该方法返回一个列表,使用循环向列表中添加项目。最后,它返回填充的列表
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;
}
showDialog(
是的,
上下文:上下文,
生成器:(BuildContext上下文){
//返回对话框类型的对象
返回CupertinoAlertDialog(
标题:文本(“添加位置”),
操作:_getList(),//尝试使用或不使用()的
);
},
);
//方法
列表_getList(){
列表温度=[];
对于(var q=1;q为什么不制作两个按钮?如果只制作两个按钮,则不必进行循环。该数据/按钮是动态的。我不明白为什么需要for循环。首先,构建方法中不应该有任何逻辑,因为它可以在任何时候出于任何原因调用。将逻辑保留在单独的方法中。这是在一个单独的方法中吗无状态或有状态小部件?我使用有状态widget这是一个好主意,但它的显示类型“List”不是“widget”类型的子类型它的工作,但每次我选择一个位置,它都会弹出到我的登录页面:DWell我不知道你是如何实现它的。我只是给你一个例子,说明你可以如何实现它。