1

I have a function written and want to call it using for-loop so that it is dynamic. The following code is for the function:

 var cardList = [
    {"img" : "assets/1.png", "icon" :"assets/icon1.png", "name" : "card1"},
    {"img" : "assets/2.png", "icon" :"assets/icon2.png", "name" : "card2"},
    {"img" : "assets/3.png", "icon" :"assets/icon3.png", "name" : "card3"},
    {"img" : "assets/4.png", "icon" :"assets/icon4.png", "name" : "card4"},
  ];
  Widget cards(img, icon, name){
    return Container(
      child: GestureDetector(
        onTap: () {},
        child: Card(
          elevation: 5.0,
          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10.0)),
          child: Stack(
            children: [
              Image(image: AssetImage(img)),
              Row(
                children: [
                Padding(padding: EdgeInsets.only(top: 3.0)),
                Image(image: AssetImage(icon),),
                Container(
                  child: Text(
                    name,
                  ),
                ),
                ],
              ),
            ],
          )
        )
      ),
    );
  }

Now to call it in the main code, I have:

Container(
                child: GridView.count(
                  crossAxisCount: 2,
                  children: [
                    for(var i in cardList)
                    cards(...)//not sure how to call this
                  ],
                  ),
              ),
0

2 Answers 2

1

The vertical viewport was given unbounded height error because shrinkWrap and ScrollPhysics both are missing.

Container(
                child: GridView.count(
                  shrinkWrap: true,
                  physics: ScrollPhysics(),
                  crossAxisCount: cardList.length,
                  children: cardList.map((item) {
                    return cards(item["img"],item["icon"], item["name"]);
                  }).toList();,
                ),)
Sign up to request clarification or add additional context in comments.

Comments

0
Container(
    child: GridView.count(
      crossAxisCount: cardList.length,
      children: cardList.map((item) {
        return cards(item["img"],item["icon"], item["name"]);
      }).toList();,
),),

3 Comments

I tried it, but there was an error Vertical viewport was given unbounded height. I have given height and width to the very first container next to return, yet not working.
@yaminir Container set height: double.infinity
For which Container should I set this height?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.