android - 如何在不使用 Listview 的情况下在 flutter 中循环按钮小部件

标签 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';
                },
              ),
            }

            ],
          );
        },
      );```


最佳答案

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

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;
}

关于android - 如何在不使用 Listview 的情况下在 flutter 中循环按钮小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59259090/

相关文章:

ios - 将新单元格添加到 Collection View 时,我不断收到布局错误

ios - 通过 iOS 应用程序发送 Facebook 消息(在后台)

android - 关于在一个 Activity 中使用两个 baseAdapter 切换布局

android - 通知总是发出声音:setSound(null)在OS> = 8上不起作用

android - Fabric 插件崩溃 --> 没有这样的属性 : betaDistributionApkFilePath for class: org. codehaus.groovy.runtime.GStringImpl

ios - ViewDidLoad 中的 8 个 ViewController 或 1 个 ViewController 和 Adjust 按钮更优化

ios - Flutter - 使用 google_maps_webservice 和 google_maps_flutter 的附近地点

flutter - 有什么办法在Flutter中打开onPressed()事件上的相机吗?

plugins - 创建 Flutter 插件时如何修改类名

android - 与 Android 设备交互的桌面应用程序