我在网上搜索了很多答案。
我在字母列表上写了迭代,然后使用“地图”类将卡片放在屏幕上
在代码中,您可以看到我进行了一行操作,并使用“ map”将卡上所有的userBoard都打印到了屏幕上。我想在其中添加一些逻辑,所以我需要获取elemnt的ID(用于Taping事件)。有办法可以做到吗?
实际上我想通过userBoard获取元素的特定索引
码:
Widget build(BuildContext context) {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Row(
children: userBoard
.map((element) => Stack(children: <Widget>[
Align(
alignment: Alignment(0, -0.6),
child: GestureDetector(
onTap: (() {
setState(() {
// print("element=${element.toString()}");
// print("element=${userBoard[element]}");
});
}),
child: SizedBox(
width: 40,
height: 60,
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(5.0),
),
child: Center(
child: Text(element,
style: TextStyle(fontSize: 30)),
)),
),
),
)
]))
.toList(),
)
],
),
Run Code Online (Sandbox Code Playgroud)
}
图片 -每张卡都是地图的“元素”。我想获取函数onTap的索引。
谢谢。