如何使 "Stack" 中的小部件计数取决于变量?
How do i make the count of Widgets in "Stack" depend on a Variable?
我试图让玩家手中的纸牌数量取决于一个变量。我不知道如何在 GridView.builder 之外执行此操作。
Image of the Hand I have so far
我当前的代码:
return Stack(
children: [
Positioned(
bottom: verticalPosition(1),
left: horizontalPosition(1),
child: Transform.rotate(
angle: rotation(1),
child: PlayCardDragBox(
cardValue: 30,
index: 1,
),
),
),
Positioned(
bottom: verticalPosition(2),
left: horizontalPosition(2),
child: Transform.rotate(
angle: rotation(2),
child: PlayCardDragBox(
cardValue: 22,
index: 2,
),
),
),
...等等
如果您获得列表中的所有卡片值,您可以在集合中使用 for 循环:
List<int> cardValues = [30, 22, 16, 20, 10, 14];
return Stack(
children: [
for (int i = 0; i < cardValues.length; i++)
Positioned(
bottom: verticalPosition(i + 1),
left: horizontalPosition(i + 1),
child: Transform.rotate(
angle: rotation(i + 1),
child: PlayCardDragBox(
cardValue: cardValues[i],
index: i+1,
),
),
),
],
);
我试图让玩家手中的纸牌数量取决于一个变量。我不知道如何在 GridView.builder 之外执行此操作。
Image of the Hand I have so far
我当前的代码:
return Stack(
children: [
Positioned(
bottom: verticalPosition(1),
left: horizontalPosition(1),
child: Transform.rotate(
angle: rotation(1),
child: PlayCardDragBox(
cardValue: 30,
index: 1,
),
),
),
Positioned(
bottom: verticalPosition(2),
left: horizontalPosition(2),
child: Transform.rotate(
angle: rotation(2),
child: PlayCardDragBox(
cardValue: 22,
index: 2,
),
),
),
...等等
如果您获得列表中的所有卡片值,您可以在集合中使用 for 循环:
List<int> cardValues = [30, 22, 16, 20, 10, 14];
return Stack(
children: [
for (int i = 0; i < cardValues.length; i++)
Positioned(
bottom: verticalPosition(i + 1),
left: horizontalPosition(i + 1),
child: Transform.rotate(
angle: rotation(i + 1),
child: PlayCardDragBox(
cardValue: cardValues[i],
index: i+1,
),
),
),
],
);