💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、豆包、星火、月之暗面及文生图、文生视频 广告
动态列表: 1. 通过循环语句实现 2. 通过ListView.builder实现 ## 循环语句 ~~~ class HomeContent extends StatelessWidget{ //自定义方法 List<Widget> _getData(){ List<Widget> list = new List(); for(var i=0; i< 20; i++){ list.add(ListTile(title: Text('我是$i列表哈'),)); } return list; } @override Widget build(BuildContext context) { // TODO: implement build return ListView( children: _getData() ); } } ~~~ 注意:toList(), ["title"]可访问,.不能访问 ~~~ class HomeContent extends StatelessWidget{ //自定义方法 List<Widget> _getData(){ var tempList = listData.map((value){ return ListTile( title: Text(value["title"]), leading: Image.network(value["imageUrl"]), subtitle: Text(value["author"]), ); }); return tempList.toList(); } @override Widget build(BuildContext context) { // TODO: implement build return ListView( children: _getData() ); } } ~~~ ## ListView.builder(推荐) ~~~ class HomeContent extends StatelessWidget{ List list = new List(); HomeContent(){ for(var i=0; i<20; i++){ list.add( ListTile( title:Text("我是$i列表") ) ); } } @override Widget build(BuildContext context) { // TODO: implement build return ListView.builder( //长度 itemCount: this.list.length, // context上下文,index索引 //根据长度遍历数据 itemBuilder: (context,index){ return this.list[index]; }, ); } } ~~~ ~~~ List list = new List(); HomeContent(){ for(var i=0; i<listData.length; i++){ list.add( ListTile( title:Text(listData[i]["title"]), leading: Image.network(listData[i]["imageUrl"]), subtitle: Text(listData[i]["author"]), // title: Text("xxx") ) ); } } @override Widget build(BuildContext context) { // TODO: implement build return ListView.builder( //长度 itemCount: this.list.length, // context上下文,index索引 //根据长度遍历数据 itemBuilder: (context,index){ return this.list[index]; }, ); } } ~~~ ~~~ Widget _getListData(context, index){ return ListTile( title: Text(listData[index]["title"]) ); } @override Widget build(BuildContext context) { // TODO: implement build return ListView.builder( //长度 itemCount: listData.length, // context上下文,index索引 //根据长度遍历数据 itemBuilder: this._getListData, ); } } ~~~