Flutter中的ListTile和ListView组件

文章介绍了Flutter框架中的ListTile组件,它常用于创建包含图标和文本的列表块。ListView组件则是一个可滚动的列表视图,可以垂直或水平展示内容。ListView.builder()适用于大数据量、动态加载的场景,而ListView.separated()则提供了添加分隔符的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


1.ListTile组件

ListTile组件(列表块组件)通常由一些文本、一个前置和后置图标组成的组件,其常用属性及功能说明如表所示。
在这里插入图片描述

const ListTile({
    super.key,
    this.leading,
    this.title,
    this.subtitle,
    this.trailing,
    this.isThreeLine = false,
    this.dense,
    this.visualDensity,
    this.shape,
    this.style,
    this.selectedColor,
    this.iconColor,
    this.textColor,
    this.contentPadding,
    this.enabled = true,
    this.onTap,
    this.onLongPress,
    this.mouseCursor,
    this.selected = false,
    this.focusColor,
    this.hoverColor,
    this.focusNode,
    this.autofocus = false,
    this.tileColor,
    this.selectedTileColor,
    this.enableFeedback,
    this.horizontalTitleGap,
    this.minVerticalPadding,
    this.minLeadingWidth,
  })

实现代码示例:

Container(
              color: Colors.black12,
              child: ListTile(
                title: Text(name[3],style: TextStyle(fontWeight: FontWeight.bold),),
                subtitle: Text(info[3]),
                leading: CircleAvatar(
                  radius: 25,
                  backgroundImage: NetworkImage(pic[3]),
                ),
                selected: gSelected[3],
                trailing: Icon(Icons.navigate_next),
                onTap: (){
                  setState(() {
                    gSelected[3] = !gSelected[3];
                  });
                },
                onLongPress: (){
                  setState(() {
                    gSelected[3] = true;
                  });
                },
              ),
            ),

2.ListView组件

ListView组件(列表视图组件)是应用程序前端页面常见的一个以列表方式显示内容的组件。

  1. ListView ():用于构建包含少量子元素的可垂直或水平滚动的列表视图,默认为垂直滚动列表视图。常用属性及功能如表所示。
    在这里插入图片描述
ListView({
    super.key,
    super.scrollDirection,
    super.reverse,
    super.controller,
    super.primary,
    super.physics,
    super.shrinkWrap,
    super.padding,
    this.itemExtent,
    this.prototypeItem,
    bool addAutomaticKeepAlives = true,
    bool addRepaintBoundaries = true,
    bool addSemanticIndexes = true,
    super.cacheExtent,
    List<Widget> children = const <Widget>[],
    int? semanticChildCount,
    super.dragStartBehavior,
    super.keyboardDismissBehavior,
    super.restorationId,
    super.clipBehavior,
  })

在这里插入图片描述

2.1 ListView.builder () 构造方法

在实际应用开发中,数据源在多数应用场景中来源于网络,来源于网络的数据存在数据量大和数据条数不可预见等问题,在这种情况下使用ListView.builder ()构造方法可以根据数据源的实际情况动态加载数据。常用属性及功能如表所示。
在这里插入图片描述
在这里插入图片描述

2.2 ListView.separated ()构造方法

ListView.separated () : ListView.separated ()构造方法中用itemBuilder属性构建列表项,用separatorBuilder属性构建列表项之间的分隔符子项。
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值