Flutter ListView动态列表

For循环实现

在这里插入图片描述

class HomeContext extends StatelessWidget{
  List<Widget> _getData(){
    List<Widget> list=new List();
    for(var i=1;i<10;i++){//循环创建九条数据
      list.add(ListTile(
        title: Text("News $i"),
      ));
    }
    return list;
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return ListView(
      children: this._getData(),//通过私有方法调用
    );
  }
}

外部引用

在这里插入图片描述

dart.dart

List listData=[
  {
    "title":"News 01",
    "author":"author01",
    "imgUrl":"http://5b0988e595225.cdn.sohucs.com/images/20171108/e8d0b0ab35b14b33a499d74cbc52b43c.jpeg"
  },
  {
    "title":"News 02",
    "author":"author02",
    "imgUrl":"http://5b0988e595225.cdn.sohucs.com/images/20171108/ba1b73bec9784b9eae4416226abcddc7.jpeg"
  },
  {
    "title":"News 03",
    "author":"author03",
    "imgUrl":"http://5b0988e595225.cdn.sohucs.com/images/20171108/fb469ca492a146fd8476fb144cd11f2c.jpeg"
  },
  {
    "title":"News 04",
    "author":"author04",
    "imgUrl":"http://5b0988e595225.cdn.sohucs.com/images/20171108/d29a69f9c7fc41d1aae4516df8ebfac3.jpeg"
  },
  {
    "title":"News 05",
    "author":"author05",
    "imgUrl":"http://5b0988e595225.cdn.sohucs.com/images/20171108/d29a69f9c7fc41d1aae4516df8ebfac3.jpeg"
  },
  {
    "title":"News 06",
    "author":"author06",
    "imgUrl":"http://5b0988e595225.cdn.sohucs.com/images/20171108/d29a69f9c7fc41d1aae4516df8ebfac3.jpeg"
  },
];

main.dart先import引入

import 'package:demo/res/data.dart';
class HomeContext extends StatelessWidget{
  //自定义方法
  List<Widget> _getData(){
    List<Widget> list=listData.map((value){
      return ListTile(
        leading: Image.network(value["imgUrl"]),
        title: Text(value['title']),
        subtitle: Text(value['author']),
      );
    }).toList();
    return list;
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return ListView(
      children: this._getData(),//通过私有方法调用
    );
  }
}


ListView.builder

在这里插入图片描述

class HomeContext extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return ListView.builder(
        itemCount: listData.length,//借助前面引入的数据
        itemBuilder: (context,index){
          return ListTile(
            title: Text(listData[index]['title'] ),
            subtitle: Text( listData[index]['author'] ),
          );
        });
  }
}


把方法抽离出去

在这里插入图片描述

class HomeContext extends StatelessWidget{
  Widget _getData(context,index){
    return ListTile(
      title: Text(listData[index]['title']),
      subtitle: Text(listData[index]['author']),
      leading: Image.network(listData[index]['imgUrl']),
    );
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return ListView.builder(
        itemCount: listData.length,
        itemBuilder: this._getData);
  }
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值