ios - 如何为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,
),
),
)
],
),
任何帮助表示赞赏!
解决方案
我不确定你的问题。但是,如果您想遍历图像的不同名称,您应该使用字符串列表作为它们的名称:
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
}
然后创建这个类的列表并在你的循环中使用它们。
推荐阅读
- typescript - 为什么 setter 函数接受与其在打字稿中的字段相同的数据类型?
- corda - 有没有办法通知缔约方流程已经完成?
- apache-kafka - 当处理每条消息需要很长时间时,具有 6 个 Kafka 消费者的最大吞吐量策略
- huggingface-transformers - Transformer-XL:语言建模的输入和标签
- pine-script - 如何通过检查蜡烛 [1] 的信号将当前蜡烛上的最后一个开盘或收盘绘制为一个圆圈
- java - 底部导航显示在其他片段中
- python - 数字后带有减号的 CSV 文件。“ValueError:无法将字符串转换为浮点数:”
- python - 编辑并写出 pyparser 结果
- python - TypeError: 'list' 和 'int' 的实例之间不支持 '>' 无法用 len(list) 解决
- java - Java ArrayList 子列表给出错误的结果