2 Flutter UI 之 List 组件

一  简单的List 组件

 children: <Widget>[
            ListTile(
                // 左边可以放图片 放置图标都可以
                leading: Image.asset("images/a.jpeg"),
                // 主标题
                title: Text("强国建设、民族复兴的唯一正确道",
                    style: TextStyle(fontSize: 20, color: Colors.red)),
                // 子标题
                subtitle: Text(
                  "把力量凝聚到实现党的二十大确定的目标任务上把力量凝聚到实现党的二十大确定的目标任务上把力",
                  maxLines: 2,
                )),
            ListTile(
                leading: Image.asset("images/a.jpeg"),
                title: Text("强国建设、民族复兴的唯一正确道",
                    style: TextStyle(fontSize: 20, color: Colors.red)),
                subtitle: Text(
                  "把力量凝聚到实现党的二十大确定的目标任务上把力量凝聚到实现党的二十大确定的目标任务上把力",
                  maxLines: 2,
                )),
            ListTile(
                leading: Image.asset("images/a.jpeg"),
                title: Text("强国建设、民族复兴的唯一正确道",
                    style: TextStyle(fontSize: 20, color: Colors.red)),
                subtitle: Text(
                  "把力量凝聚到实现党的二十大确定的目标任务上把力量凝聚到实现党的二十大确定的目标任务上把力",
                  maxLines: 2,
                ))
          ],

二 水平列表

 

水平滑动 设置滚动方向

Container(
            height: 180,
            child: ListView(
              padding: EdgeInsets.all(10),
              scrollDirection: Axis.horizontal,
              // ignore: prefer_const_literals_to_create_immutables
              children: <Widget>[
                // 滚动方向设置为水平
                Container(
                  width: 110,
                  color: Colors.lightBlue,
                ),
                Container(
                  width: 110,
                  color: Colors.lightGreen,
                  child: Container(
                    child: ListView(
                      children: [
                        Text("大风风起兮云飞扬风起兮云飞扬"),
                        Image.network(
                            "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fblog%2F202106%2F09%2F20210609081952_51ef5.thumb.1000_0.jpg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1678624103&t=9a08456ce6cd15d2c5249532d13d0e0a")
                      ],
                    ),
                  ),
                ),
                Container(
                  width: 110,
                  color: Colors.red,
                ),
                Container(
                  width: 110,
                  color: Colors.yellow,
                )
              ],
            ))

三 动态数据列表

模拟本地数据

  List<Widget> _getListData() {
    List<Widget> list = [];
    for (int i = 0; i < 20; i++) {
      list.add(ListTile(
        title: Text("我是第$i行数据"),
      ));
    }
    return list;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: Container(
            child: ListView(
          children: this._getListData(),
        )));
  }

四 ListView builder 方法创建动态列表

模拟网络数据 

List listData = [
  {
    "title": 'Candy Shop',
    "author": 'Mohamed Chahin',
    "imageUrl": 'https://www.itying.com/images/flutter/1.png',
  },
  {
    "title": 'Childhood in a picture',
    "author": 'Google',
    "imageUrl": 'https://www.itying.com/images/flutter/2.png',
  },
  {
    "title": 'Alibaba Shop',
    "author": 'Alibaba',
    "imageUrl": 'https://www.itying.com/images/flutter/3.png',
  },
  {
    "title": 'Candy Shop',
    "author": 'Mohamed Chahin',
    "imageUrl": 'https://www.itying.com/images/flutter/4.png',
  },
  {
    "title": 'Tornado',
    "author": 'Mohamed Chahin',
    "imageUrl": 'https://www.itying.com/images/flutter/5.png',
  },
  {
    "title": 'Undo',
    "author": 'Mohamed Chahin',
    "imageUrl": 'https://www.itying.com/images/flutter/6.png',
  },
  {
    "title": 'white-dragon',
    "author": 'Mohamed Chahin',
    "imageUrl": 'https://www.itying.com/images/flutter/7.png',
  }
];


  Widget _getListData(context, index) {
    return ListTile(
      title: Text(listData[index]["title"]),
      subtitle: Text(listData[index]["author"]),
      leading: Image.network(listData[index]["imageUrl"]),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: Container(
            child: ListView.builder(
                itemCount: listData.length, itemBuilder: this._getListData)));
  }

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值