首页 > 解决方案 > 如何为flutter中的每个索引分配每个值

问题描述

我正在制作一个显示一行图像的应用程序(所有这些图像都使用 0-4 的循环/索引)。编码 :

for (int i = 0; i <= 3; i++)
     child: Card(
                          semanticContainer: true,
                          clipBehavior: Clip.antiAliasWithSaveLayer,
                          child: Container(
                            width: 300,
                            height: 300,
                            color: Colors.accents[i],
                            child: Image.asset(
                              "assets/farCryImages/$i.jpg",
                              alignment: Alignment.center,
                              fit: BoxFit.cover,
                            ),
                          ),
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(8),
                          ),
                          elevation: 5,
                        ),

我想向用户显示他们的名字的弹出窗口(即:第一张图片:a,第二张图片:b 等..)。但我似乎找不到办法做到这一点,我需要帮助。

我试过这个,但它不会工作:

for (int i = 0; i <= 3; i++)
                    Padding(
                      padding: const EdgeInsets.fromLTRB(20, 20, 0, 0),
                      child: GestureDetector(
                        onLongPress: () => showDialog(
                          context: context,
                          builder: (BuildContext context) => AlertDialog(
                            title: Text("$i"),
                          ),
                        ),

我想将 $i 更改为特定照片的特定名称。

完整代码:

child: Row(
                children: [
                  for (int i = 0; i <= 3; i++)
                    Padding(
                      padding: const EdgeInsets.fromLTRB(20, 20, 0, 0),
                      child: GestureDetector(
                        onLongPress: () => showDialog(
                          context: context,
                          builder: (BuildContext context) => AlertDialog(
                            title: Text("$i"),
                          ),
                        ),
                        child: Card(
                          semanticContainer: true,
                          clipBehavior: Clip.antiAliasWithSaveLayer,
                          child: Container(
                            width: 300,
                            height: 300,
                            color: Colors.accents[i],
                            child: Image.asset(
                              "assets/farCryImages/$i.jpg",
                              alignment: Alignment.center,
                              fit: BoxFit.cover,
                            ),
                          ),
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(8),
                          ),
                          elevation: 5,
                        ),
                      ),
                    )
                ],
              ),

任何帮助表示赞赏!

标签: iosflutterdartflutter-layout

解决方案


我不确定你的问题。但是,如果您想遍历图像的不同名称,您应该使用字符串列表作为它们的名称:

List<String> imageName = [
    "first image",
    "second image", 
    // and so on
  ]

然后,在你的循环中调用它们:

     onLongPress: () => showDialog(
                          context: context,
                          builder: (BuildContext context) => AlertDialog(
                            title: Text(imageName[i]),
                          ),
                        ),

另外,为了更好的解决方案,我建议为每个图像使用一个类:

class ImageItem{
  String name;
  String path;
  // and other parameters like id

  // and a constructor
}

然后创建这个类的列表并在你的循环中使用它们。


推荐阅读