flutter 之 ListView的使用与详解 map for listview.builder 的使用

在这里插入图片描述

1.ListView 配合ListTile 实现新闻列表样式

ListView(
  children: <Widget>[
    ListTile(
      title: const Text('我是 title'),
      subtitle: const Text('我是 sub_title'),
      leading: Image.asset(
        'images/c.png',
        fit: BoxFit.cover,
      ),
      trailing: const Icon(
        Icons.chevron_right,
        size: 22,
        color: Colors.grey,
      ),
    ),
  ],
),

2.ListView垂直列表

ListView(
   padding: const EdgeInsets.all(16),
   children: <Widget>[
     Image.network('https://www.itying.com/images/flutter/1.png'),
     Image.network('https://www.itying.com/images/flutter/2.png'),
     Image.network('https://www.itying.com/images/flutter/4.png'),
     Image.network('https://www.itying.com/images/flutter/3.png'),
     Image.network('https://www.itying.com/images/flutter/5.png'),
     Image.network('https://www.itying.com/images/flutter/7.png'),
   ],
 ),
ListView(
          // scrollDirection: Axis.horizontal,
          padding: const EdgeInsets.all(16),
          children: <Widget>[
            Container(
              decoration: BoxDecoration(
                color: Colors.red.shade300,
                border: Border.all(color: Colors.grey.shade200),
              ),
              height: 100,
              width: 200,
            ),
            Container(
              decoration: BoxDecoration(
                color: Colors.green.shade300,
                border: Border.all(color: Colors.grey.shade200),
              ),
              height: 100,
              width: 200,
            ),
            Container(
              decoration: BoxDecoration(
                color: Colors.blue.shade300,
                border: Border.all(color: Colors.grey.shade200),
              ),
              height: 100,
              width: 200,
            ),
            Container(
              decoration: BoxDecoration(
                color: Colors.pink.shade200,
                border: Border.all(color: Colors.grey.shade200),
              ),
              height: 100,
              width: 200,
            ),
          ],
        ),

注:ListView的垂直布局中直接加 container ,container 的宽度是自适应宽度的,设置宽度无效!
在这里插入图片描述

3.ListView 水平列表 scrollDirection: Axis.horizontal,

 ListView(
 scrollDirection: Axis.horizontal,
   padding: const EdgeInsets.all(16),
   children: <Widget>[
     Container(
       decoration: BoxDecoration(
         color: Colors.red.shade300,
         border: Border.all(color: Colors.grey.shade200),
       ),
       height: 100,
       width: 200,
     ),
     Container(
       decoration: BoxDecoration(
         color: Colors.green.shade300,
         border: Border.all(color: Colors.grey.shade200),
       ),
       height: 100,
       width: 200,
     ),
     Container(
       decoration: BoxDecoration(
         color: Colors.blue.shade300,
         border: Border.all(color: Colors.grey.shade200),
       ),
       height: 100,
       width: 200,
     ),
     Container(
       decoration: BoxDecoration(
         color: Colors.pink.shade200,
         border: Border.all(color: Colors.grey.shade200),
       ),
       height: 100,
       width: 200,
     ),
   ],
 ),

注:ListView水平布局中直接加 container ,container 的高度是自适应宽度的,设置高度无效!
在这里插入图片描述

SizedBox(
          height: 160,
          child: ListView(
            scrollDirection: Axis.horizontal,
            padding: const EdgeInsets.all(16),
            children: <Widget>[
              Container(
                height: 160,
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(4),
                ),
                margin: const EdgeInsets.only(right: 8),
                child: Column(
                  children: [
                    SizedBox(
                      height: 100,
                      child: Image.network(
                          'https://www.itying.com/images/flutter/4.png',
                          width: 100,
                          fit: BoxFit.cover),
                    ),
                    const SizedBox(height: 5),
                    const Text('灯塔')
                  ],
                ),
              ),
              Container(
                height: 150,
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(4),
                ),
                margin: const EdgeInsets.only(right: 8),
                child: Column(
                  children: [
                    SizedBox(
                      height: 100,
                      child: Image.network(
                          'https://www.itying.com/images/flutter/4.png',
                          width: 100,
                          fit: BoxFit.cover),
                    ),
                    const SizedBox(height: 5),
                    const Text('灯塔')
                  ],
                ),
              ),
              Container(
                height: 150,
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(4),
                ),
                margin: const EdgeInsets.only(right: 8),
                child: Column(
                  children: [
                    SizedBox(
                      height: 100,
                      child: Image.network(
                          'https://www.itying.com/images/flutter/4.png',
                          width: 100,
                          fit: BoxFit.cover),
                    ),
                    const SizedBox(height: 5),
                    const Text('灯塔')
                  ],
                ),
              ),
              Container(
                height: 150,
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(4),
                ),
                margin: const EdgeInsets.only(right: 8),
                child: Column(
                  children: [
                    SizedBox(
                      height: 100,
                      child: Image.network(
                          'https://www.itying.com/images/flutter/4.png',
                          width: 100,
                          fit: BoxFit.cover),
                    ),
                    const SizedBox(height: 5),
                    const Text('灯塔')
                  ],
                ),
              ),
              Container(
                height: 150,
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(4),
                ),
                margin: const EdgeInsets.only(right: 8),
                child: Column(
                  children: [
                    SizedBox(
                      height: 100,
                      child: Image.network(
                          'https://www.itying.com/images/flutter/4.png',
                          width: 100,
                          fit: BoxFit.cover),
                    ),
                    const SizedBox(height: 5),
                    const Text('灯塔')
                  ],
                ),
              ),
              Container(
                height: 150,
                margin: const EdgeInsets.only(right: 8),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(4),
                ),
                child: Column(
                  children: [
                    SizedBox(
                      height: 100,
                      child: Image.network(
                          'https://www.itying.com/images/flutter/4.png',
                          width: 100,
                          fit: BoxFit.cover),
                    ),
                    const SizedBox(height: 5),
                    const Text('灯塔')
                  ],
                ),
              ),
            ],
          ),
        ),

在这里插入图片描述

4.ListView 动态数据处理


class _MyHomePageState extends State<MyHomePage> {
// 自定义方法方法用于遍历动态组件
  List<Widget> _initListData() {
    List<Widget> list = [];
    for (var i = 0; i < 20; i++) {
      list.add(ListTile(
        title: const Text('我是 title'),
        subtitle: const Text('我是 sub_title'),
        leading: Image.asset(
          'images/c.png',
          fit: BoxFit.cover,
        ),
        trailing: const Icon(
          Icons.chevron_right,
          size: 22,
          color: Colors.grey,
        ),
      ));
    }
    return list;
  }

  
  Widget build(BuildContext context) {
    return ListView(
      // 调用方法
      children: _initListData(),
    );
  }
}

在这里插入图片描述

5.Listview 的外部动态数组 for 的使用方法

// news.dart

List newsList = [
  {
    'title': '你所经历的哪些事情让你怀疑这个世界是假的/有 bug 的?',
    'sub_title':
        '这个世界不一定是假的,但我的亲身经历告诉我,肯定不止这一个世界。在我最开始来的那个时空,2019年5月17日已经世界末日了!这个世界不一定是假的,但我的亲身经历告诉我,肯定不止这一个世界。在我最开始来的那个时空,2019年5月17日已经世界末日了!这个世界不一定是假的,但我的亲身经历告诉我,肯定不止这一个世界。',
    'cover': 'https://pica.zhimg.com/v2-a657e5713d0536ad01f301cd47bc4e7c_b.jpg',
  },
  {
    'title': '有什么网站能让你一直收藏?',
    'sub_title':
        '它的每种文件类型都包括多种文件格式,并且每种文件格式转换都支持丰富的设置。它的每种文件类型都包括多种文件格式,并且每种文件格式转换都支持丰富的设置。它的每种文件类型都包括多种文件格式,并且每种文件格式转换都支持丰富的设置。它的每种文件类型都包括多种文件格式,并且每种文件格式转换都支持丰富的设置。它的每种文件类型都包括多种文件格式,并且每种文件格式转换都支持丰富的设置。',
    'cover': 'https://pic4.zhimg.com/v2-16c9bca2a43ca7b55de9ec41a6afa833_b.jpg',
  },
  {
    'title': '穷,可以让一个人卑微成什么样?',
    'sub_title': '我经常在某鱼上挂一些小孩儿穿不下的衣服,基本都很新,或者买来没穿就小了的那种。',
    'cover':
        'https://pic1.zhimg.com/80/v2-b4f760689645df9004219b65b0df8bb8_1440w.webp?source=1940ef5c',
  },
  {
    'title': '所有穿越文生存指南,我只信我军这几本',
    'sub_title': '给大家介绍一下几本神书,不仅世界末日,穿越也能用得上,常备无患~',
    'cover':
        'https://picx.zhimg.com/80/v2-a928de8f3e617dec43e2c561c3a8511a_1440w.webp?source=1940ef5c',
  },
  {
    'title': '光看书名根本感受不到这本书的强大。',
    'sub_title': '实在很难让我把它和这些评价联系在一起。',
    'cover':
        'https://picx.zhimg.com/80/v2-90f3f78f298edbb36f1e7b036c797778_1440w.webp?source=1940ef5c',
  },
  {
    'title': '万一不小心穿越了。',
    'sub_title': '这看进度,八成已经灾后重建,进行到工业革命了吧?',
    'cover':
        'https://picx.zhimg.com/80/v2-8d4a34f102858dc1d77543ae94652014_1440w.webp?source=1940ef5c',
  },
];

引入data 数据,并动态处理数组;

import './res/news.dart';


class _MyHomePageState extends State<MyHomePage> {
// 自定义方法 方法用于遍历动态组件
  List<Widget> _initListData() {
    List<Widget> list = [];
    for (var item in newsList) {
      list.add(
        Padding(
          padding: const EdgeInsets.only(top: 8),
          child: ListTile(
            leading: Image.network(
              item['cover'],
              fit: BoxFit.cover,
              width: 100,
            ),
            title: Text(
              item['title'] ?? '--',
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
              style: const TextStyle(
                color: Colors.black87,
                fontSize: 16,
              ),
            ),
            subtitle: Padding(
              padding: const EdgeInsets.only(top: 8),
              child: Text(
                item['sub_title'] ?? '--',
                maxLines: 2,
                overflow: TextOverflow.ellipsis,
                style: const TextStyle(
                  color: Colors.black54,
                  fontSize: 14,
                ),
              ),
            ),
          ),
        ),
      );
    }
    return list;
  }

  
  Widget build(BuildContext context) {
    return ListView(
      // 调用方法
      children: _initListData(),
    );
  }
}

在这里插入图片描述

6.Listview 的外部动态数组 map 的使用方法


class _MyHomePageState extends State<MyHomePage> {
  // 自定义方法 方法用于遍历动态组件(一层一层写法)
  List<Widget> _initListData() {
    var list = newsList.map((item) {
      return ListTile(
        title: Text(item['title']),
        leading: Image.network(
          item['cover'],
          width: 100,
          fit: BoxFit.cover,
        ),
      );
    }); // 返回值是 (1,2,3)这种各类型的
    return list.toList();
  }
  // 自定义方法 方法用于遍历动态组件(合并写法)
  List<Widget> _initListData() {
    return newsList.map((item) {
      return ListTile(
        title: Text(item['title']),
        leading: Image.network(
          item['cover'],
          width: 100,
          fit: BoxFit.cover,
        ),
      );
    }).toList();
  }

  
  Widget build(BuildContext context) {
    return ListView(
      // 调用方法 可以this.也可直接去掉不用!
      children: _initListData(),
    );
  }
}

7.Listview.builder 的使用。


class _MyHomePageState extends State<MyHomePage> {
// 自定义方法 方法用于遍历动态组件
  
  Widget build(BuildContext context) {
    return ListView.builder(
      itemCount: newsList.length,  // 遍历总数量
      itemBuilder: (context, index) {  // 方法参数是 context 和index 两个参数
        return ListTile(
          leading: Image.network(
            newsList[index]['cover'],
            width: 100,
            fit: BoxFit.cover,
          ),
          title: Text(newsList[index]['title']),
        );
      },
    );
  }
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 好的,以下是Flutter ListView.builder的相关内容: Flutter中的ListView.builder是一种创建可滚动列表的方式。 它是使用基于生成器的方式来创建项目的,这意味着仅当需要显示项目时才会生成它们,这可以显著提高性能。 以下是ListView.builder的基本用法: ```dart ListView.builder( itemCount: items.length, itemBuilder: (BuildContext context, int index) { return ListTile( title: Text(items[index]), ); }, ); ``` 其中,`itemCount`属性指定了列表中的项数,而`itemBuilder`属性是一个回调函数,用于生成每个项目的小部件。 在这个例子中,我们使用了`ListTile`小部件来呈现列表项。`ListTile`是一种内置的小部件,用于创建带有文本和可选图标的项目。 你可以将这些代码放到Flutter应用程序中的任何位置,以创建一个基本的ListView.builder。 ### 回答2: Flutter Listview.BuilderFlutter框架中的一种基于ListView的快速构建方法。ListViewFlutter中的一个常见UI组件,用于在Flutter应用中呈现多个数据垂直或水平列表。ListView.BuilderListView的一种实现方式,它可以更加灵活地适应不同的应用场景。 使用Flutter Listview.Builder,我们可以使用一组数据来快速构建一个列表视图,而不需要手动添加多个组件。我们可以使用类似于map()函数的方式来将列表中每个数据项映射到一个组件,然后将这些组件作为列表的items属性传递给ListView.Builder。 与其他构建ListView的方法相比,使用ListView.Builder的好处在于它的效率更高,因为它只会在显示屏幕所需要的数据项上构建组件,而不是在整个列表上构建所有组件。这种构建方式能够显著提高性能和渲染速度。 在实际使用ListView.Builder时,我们需要通过itemCount属性来指定列表的长度,然后在itemBuilder回调函数中构建每个数据项所对应的组件。此外,我们还可以在ListView.Builder使用其他键来进行更多的自定义,例如控制滚动的控制器、构建分割线、调整列表项之间的间距等等。 总之,Flutter Listview.Builder是一个快速、灵活且高效的构建Flutter列表视图的方法,在应用中使用它可以有效地提高应用渲染性能和响应速度,是一个值得使用的重要UI组件。 ### 回答3: Flutter是一个快速开发高质量、高性能并且可即时部署到iOS和Android平台的跨平台移动应用框架。ListViewFlutter中最基本、最常用的列表组件之一,它可以用来显示任意类型的数据列表,而ListView.builder则是一个快速而且可以高度定制列表的方法。 ListView.builder的工作方式是接受一个itemBuilder回调函数,它可以根据数据源和索引来构建一个可视化的列表项。ListView.builder用于构建动态列表,只会在需要时才会进行渲染,通过比普通ListView更高效地创建长列表来减少资源占用。 该组件有许多属性来控制其外观、交互和动画效果。您可以设置列表项高度、内容填充等,使您的列表在各种设备和屏幕尺寸上看起来完美。除了itemBuilder,还有一些其他属性,如itemCount,用于指定列表项数量;itemExtent,用于指定每个列表项的固定高度等。 总之,ListView.builderFlutter中非常重要的一个组件,它简化了Flutter中的动态列表的构建和管理。 由于其高效性和易于使用的属性,它已经成为Flutter应用程序中的重要部分。所以我们要熟练掌握该组件以便更有效地构建数据展示列表、列表式的交互体验等功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值