flutter_pagewise 是一个 Flutter 包,旨在简化应用程序中分页内容的实现。它提供了一些小部件,可按批次或页面加载数据,非常适合高效处理大型数据集,而不会影响性能。该包支持下拉刷新和无限滚动,适用于列表、网格和其他可滚动界面。它可与任何数据源无缝集成,并在 Android、iOS、Web、macOS、Windows 和 Linux 等主要平台上运行。
适合场景
无限滚动列表
懒加载数据
下拉刷新分页
大型数据集渲染
动态内容获取
基于网格的分页显示
主要特点
自动分页加载
内置刷新支持
跨平台兼容性
可自定义的加载指示器
灵活的数据源集成
支持垂直和水平滚动
适合对象
大型数据集的应用程序
社交媒体动态
电子商务商品列表
新闻或博客内容
任何需要高效数据加载的应用程序
使用建议
需要适当的错误处理
需要仔细的状态管理
可能需要自定义缓存策略
不适合小数据集
依赖外部数据源
性能随网络速度而变化
原始 README
以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。
A library for widgets that load their content one page (or batch) at a time (also known as lazy-loading and pagination).
Features
Load data one page at a time
Retry failed pages
Override the default loading, retry, and error widgets if desired
Manage loading of data more closely using a PagewiseLoadController
ListView and GridView implementations
SliverList and SliverGrid implementations
Extendability using inheritance
Breaking Change Starting V1.0.0:
The library has been rewritten in version 1.0.0 to provide a more
efficient implementation that does not require a totalCount parameter
and shows only one loading sign when users scroll down. In addition,
a new parameter has been added to itemBuilder callback to provide
the index if needed by the user.
Installing the library:
Like any other package, add the library to your pubspec.yaml dependencies:
PagewiseGridView: A pagewise implementation of GridView. It could be
used as follows:
PagewiseGridView.count(
pageSize: 10,
crossAxisCount: 2,
mainAxisSpacing: 8.0,
crossAxisSpacing: 8.0,
childAspectRatio: 0.555,
padding: EdgeInsets.all(15.0),
itemBuilder: (context, entry, index) {
// return a widget that displays the entry's data
},
pageFuture: (pageIndex) {
// return a Future that resolves to a list containing the page's data
},
);
PagewiseListView: A pagewise implementation of ListView. It could be
used as follows:
PagewiseListView(
pageSize: 10,
padding: EdgeInsets.all(15.0),
itemBuilder: (context, entry, index) {
// return a widget that displays the entry's data
},
pageFuture: (pageIndex) {
// return a Future that resolves to a list containing the page's data
}
);
PagewiseSliverGrid: A pagewise implementation of SliverGrid. It could be used similar to PagewiseGridView for cases where a sliver is needed.
PagewiseSliverList: A pagewise implementation of SliverList. It could be used similar to PagewiseListView for cases where a sliver is needed.
The classes provide all the properties of ListViews and
GridViews. In addition, you must provide the itemBuilder, which
tells Pagewise how you want to render each element, and pageFuture,
which Pagewise calls to fetch new pages. Please note that pageFuture
must not return more values than mentioned in the pageSize parameter.
Customizing the widget:
In addition to the required parameters, Pagewise provides you with
optional parameters to customize the widget. You have loadingBuilder,
errorBuilder, noItemsFoundBuilder, and retryBuilder to customize the widgets that show
on loading, error, no found items and retry respectively.
Thus, the retryBuilder provides you with a callback that you can
call when you want to retry.
The errorBuilder is only relevant when showRetry is set to false,
because, otherwise, the retryBuilder is shown instead. The errorBuilder
can be used as follows:
Check the classes' documentation for more details.
Providing your own PagewiseLoadController:
Pagewise widgets manage the loading of pages using a
PagewiseLoadController. This controller is responsible for fetching data,
handling errors, etc.
You don't have to provide a controller yourself when creating a Pagewise
widget. The widget will create one for you. However you might wish to create
one yourself in order to achieve some effects.
Notice though that if you provide a controller yourself, you should provide
the [pageFuture] and [pageSize] parameters to the controller instead of
the widget.
A possible use case of the controller is to force a reset of the loaded
pages using a RefreshIndicator.
you could achieve that as follows (note that we added the Future.value({}) as a dummy return value, because
onRefresh expects a Future, but reset does not return one):
Another use case for creating the controller yourself is if you want to
listen to the state of Pagewise and act accordingly.
For example, you might want to show a snackbar when we reach the end of the list
In that case, you could do:
final _pageLoadController = PagewiseLoadController(
pageSize: 6,
pageFuture: BackendService.getPage
);
@overridevoidinitState() {
super.initState();
this._pageLoadController.addListener(() {
if (!this._pageLoadController.hasMoreItems) {
Scaffold.of(context).showSnackBar(
SnackBar(
content: Text('No More Items!')
)
);
}
});
}
Creating your own Pagewise Widgets:
You need to inherit from the Pagewise class. Check the code of
PagewiseListView and PagewiseGridView for examples