如何在不使用Listview的情况下使按钮窗口小部件循环颤动

时间:2019-12-10 01:49:43

标签: android ios flutter dart

我要循环使用我的代码,但显示错误,我在这里停留了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';
                },
              ),
            }

            ],
          );
        },
      );```


1 个答案:

答案 0 :(得分:1)

我创建了一个简单的方法,希望可以满足您的需求。该方法返回一个列表,该列表使用循环将项目添加到列表中。最后,它返回填充的列表。

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;
}
相关问题