gpt4 book ai didi

android - 动态添加 GridTiles 到 List

转载 作者:IT王子 更新时间:2023-10-29 07:21:13 26 4
gpt4 key购买 nike

我有一个玩家列表,我想将此列表呈现给 GridTile 按钮。我怎样才能做到这一点?我已经尝试做一个将 GridTiles 作为列表返回的函数,但无法让它工作。我已经阅读了一些关于 map 的内容。

我的方法是,使用包含玩家姓名和号码的按钮。 (播放器是我创建的类)

这是我的示例 atm:

class MyHomePage extends StatelessWidget{
@override
Widget build(BuildContext context){
List<Player> players = new List<Player>();
players.add(new Player("Tom", 10, "test"));
players.add(new Player("Mike", 22, "test"));
players.add(new Player("John", 33, "test"));

List<Widget> list = new List<Widget>();
list.add(new Text("Test"));
return new Scaffold(
appBar: new AppBar(
title: new Text('Players'),
),
body: new GridView.count(
crossAxisCount: 4,
children: new List<Widget>.generate(16, (index) {
return new GridTile(
child: new Card(
color: Colors.blue.shade200,
child: new Center(
child: new Text('tile $index'),
)
),
);
}),
),
);
}
}

最佳答案

试试这个

class Players{
int id;
String name;
Players({this.id,this.name});
//Getters
String get getName => name;
int get getID => id;
}

class DemoPageGridTile extends StatelessWidget {

List<Players> _listData = new List<Players>();

DemoPageGridTile(){
_generateList();
}

_generateList(){
for(int i=0; i<45; i++){
_listData.add(Players(id: i+1, name: "xyz_$i"));
}

}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("GridTile example"),
),
body: GridView.builder(
gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 4),
itemBuilder: (BuildContext context, int index) {
return Container(
margin: EdgeInsets.all(4.0),
child: RaisedButton(
onPressed: (){ print(_listData[index].id.toString()); },
child: Text(_listData[index].getName),
),
);
},
itemCount: _listData.length,
),
);
}
}

enter image description here

关于android - 动态添加 GridTiles 到 List<Widget>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55548574/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com