Flutter:ListView-ScrollPhysics 详细介绍(翻译)

15,126 阅读3分钟

(原文地址:medium.com/flutter-com…) ==>需要翻墙

ListView

(2019年12月:本文只是简单的翻译 且flutter更新速度很快 可实现性不做保证)

Flutter中的ListView用法详细介绍

本文旨在对ListView类,ScrollPhysics以及常规小组件的使用和细节进行更深入的探索。

Flutter中的ListView是可滚动项的线性列表。我们可以使用它来制作可滚动项目列表或制作重复项目列表。

ListView的类型:
1. ListView
2. ListView.builder
3. ListView.separated
4. ListView.custom

ListView

ListView类的默认构造函数。

ListView(
  children: <Widget>[
    ItemOne(),
    ItemTwo(),
    ItemThree(),
  ],
),

ListView只需要一个子列表并使其可滚动。且一般只运用于少量的Widget,因为List构造列表中元素,大量元素可能使这种效率很低下。

ListView.builder()

ListView.builder()构造重复的项目列表,有两个主要的参数:itemCount列表中的项目数,itemBuilder构建的每个列表。

ListView.builder(
  itemCount: itemCount,
  itemBuilder: (context, position) {
    return listItem();
  },
),

列表的items是惰性构建,即只创建了特定数量的items,当向前滚动时,早期的items会被销毁。

小技巧:itemCount不作为必需的参数,则列表是无限的

ListView.builder(
  itemBuilder: (context, position) {
    return Card(
      child: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Text(position.toString(), style: TextStyle(fontSize: 22.0),),
      ),
    );
  },
),

ListView.separated()

separated()构造函数中,我们生成一个列表,可以指定每个项之间的分隔符。

实质上,我们构造了两个交织列表:一个作为主列表,一个作为分隔符列表。值得注意的是此构造函数的itemCount是必须的参数。

ListView.separated(
      itemBuilder: (context, position) {
        return ListItem();
      },
      separatorBuilder: (context, position) {
        return SeparatorItem();
      },
      itemCount: itemCount,
),

这种类型的列表允许您动态定义分隔符,为不同类型的项目使用不同类型的分隔符,在需要时添加或删除分隔符等。

这个实现还可以用于插入其他类型的元素(例如广告),并且不需要对列表项中间的主列表进行任何修改。

注意:分隔符列表长度比项目列表小于1,则在最后一个元素之后不存在分隔符。

ListView.custom()

正如其名所示,custom()构造函数可以自定义构建ListViews,以便构建列表的子组件。主要的参数是SliverChildDelegate

SliverChildDelegates的类型:

SliverChildListDelegate
SliverChildBuilderDelegate

SliverChildListDelegate接受子组件的直接列表,而SliverChildBuiderDelegate使用IndexedWidgetBuilder构建(我们使用的构建器函数)。

可以使用或子类化这些来构建自己的委托。 ListView.builder本质上是一个带有SliverChildBuilderDelegateListView.custom

ListView默认构造函数的行为类似于带有SliverChildListDelegateListView.custom

ScrollPhysics

为了控制滚动的发生方式,ListView构造函数中设置了physics参数。

ScrollPhysics类型:

NeverScrollablePhysics

NeverScrollablePhysics呈现不可滚动的列表。使用此选项可以完全禁用ListView的滚动。

BouncingScrollPhysics

当列表结束时,BouncingScrollPhysics会弹回列表。在iOS上使用类似的效果。

ClampingScrollPhysics

这是Android上使用的默认滚动物理。 列表在结尾处停止并给出指示的水波纹效果。

FixedExtentScrollPhysics

这与此列表中的其他内容略有不同,因为它仅适用于FixedExtendScrollControllers和使用它们的列表。如下:采用ListWheelScrollView来制作类似轮子的列表。

FixedExtentScrollPhysics仅滚动到项目而不是其间的任何偏移。

FixedExtentScrollController fixedExtentScrollController =
    new FixedExtentScrollController();
ListWheelScrollView(
  controller: fixedExtentScrollController,
  physics: FixedExtentScrollPhysics(),
  children: monthsOfTheYear.map((month) {
    return Card(
        child: Row(
      children: <Widget>[
        Expanded(
            child: Padding(
          padding: const EdgeInsets.all(8.0),
          child: Text(
            month,
            style: TextStyle(fontSize: 18.0),
          ),
        )),
      ],
    ));
  }).toList(),
  itemExtent: 60.0,
),

另外:

如何在列表中的item不被摧毁?

Flutter提供了一个KeepAlive()小部件,它可以保持项目处于活动状态,否则可能会被摧毁。在列表中,元素默认包装在AutomaticKeepAlive小部件中。 可以通过将addAutomaticKeepAlives字段设置为false来禁用AutomaticKeepAlives。这在元素不需要保持活动或KeepAlive的自定义实现的情况下非常有用。

ListView在列表和外部小部件之间有空格?

默认情况下,ListView在它和外部窗口小部件之间有填充,要删除它,将填充设置为EdgeInsets.all(0.0)