Issue
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
],
),
),
Solution
Container(
child: GridView.count(
crossAxisCount: cardList.length,
children: cardList.map((item) {
return cards(item["img"],item["icon"], item["name"]);
}).toList();,
),),
Answered By - 宋永涛
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.