简体   繁体   中英

How do i make the count of Widgets in “Stack” depend on a Variable?

I am trying to make the amount of Cards in the Hand of the Player depend on a Variable. I can't figure out how to do this outside of the GridView.builder.

Image of the Hand I have so far

My Current Code:

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,
            ),
          ),
        ),

... and so on

If you get all the card values in a list you may use the for loop in a collection:

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,
          ),
        ),
      ),
  ],
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM