InfiniteScrollListView<T> class
A widget that provides infinite scrolling functionality to ListView or GridView.
This widget fetches data of type T
using either of the fetchNext or fetchPrevious
methods
when the user scrolls the list or grid and calls the itemBuilder.
Each item's Widget has a Provider in its parent, and you can access each item's information
using methods like context.read<T>()
or context.read<InfiniteScrollItemInformation>()
, etc.
If the item implements Listenable, the parent uses InheritedProvider
to register listeners.
This enables the detection of changes to the item and the rebuilding of the item accordingly.
It also supports pull-to-refresh functionality.
For example:
const int kMaxFetchCount = 20;
const int kItemMaxLength = 100;
InfiniteScrollListView<String>.list(
initialIndex: () => 0,
fetchPrev: (index, crossAxisCount) async {
final tStartIndex = max(0, index - kMaxFetchCount + 1);
final tFetchCount = index + 1 - tStartIndex;
return List.generate(tFetchCount, (i) => 'Item ${tStartIndex + i}');
},
fetchNext: (index, crossAxisCount) async {
final tFetchCount = min(kMaxFetchCount, kItemMaxLength - index);
return List.generate(tFetchCount, (i) => 'Item ${index + i}');
},
canFetchPrev: (index) => index >= 0,
canFetchNext: (index) => index < kItemMaxLength,
itemBuilder: (context, item, index) {
return ListTile(
title: Text(item),
);
},
);
- Inheritance
-
- Object
- DiagnosticableTree
- Widget
- StatefulWidget
- InfiniteScrollListView
Constructors
-
InfiniteScrollListView.grid({Key? key, required Future<
List< fetchNext(int index, int crossAxisCount), Future<T> >List< fetchPrev(int index, int crossAxisCount)?, required Widget itemBuilder(BuildContext context, T item, int index), required SliverGridDelegate? gridDelegate, void onIndexChanged(int index, int crossAxisCount)?, Key? dataKey, ScrollPhysics? physics, Axis scrollDirection = Axis.vertical, bool? primary, int initialIndex()?, bool initialIndexNotFoundCallback(int index, bool giveup)?, ScrollController? controller, Clip clipBehavior = Clip.hardEdge, double? cacheExtent, List<T> >Widget> ? overlaySlivers, EdgeInsets? padding, bool canRefresh = true, Widget refreshIndicatorBuilder(BuildContext context, Widget child, Future<void> refresh())?, Widget? prefix, Widget? suffix, Widget? empty, Widget? loading, Widget? loadingMore, Widget errorBuilder(BuildContext context, Object error)?, void onRefresh()?, bool canFetchNext(int index)?, bool canFetchPrev(int index)?}) - Creates an infinite scrolling grid view.
-
InfiniteScrollListView.list({Key? key, required Future<
List< fetchNext(int index, int crossAxisCount), Future<T> >List< fetchPrev(int index, int crossAxisCount)?, required Widget itemBuilder(BuildContext context, T item, int index), void onIndexChanged(int index, int crossAxisCount)?, Key? dataKey, ScrollPhysics? physics, Axis scrollDirection = Axis.vertical, bool? primary, int initialIndex()?, bool initialIndexNotFoundCallback(int index, bool giveup)?, ScrollController? controller, Clip clipBehavior = Clip.hardEdge, double? cacheExtent, List<T> >Widget> ? overlaySlivers, EdgeInsets? padding, bool canRefresh = true, Widget refreshIndicatorBuilder(BuildContext context, Widget child, Future<void> refresh())?, Widget? prefix, Widget? suffix, Widget? empty, Widget? loading, Widget? loadingMore, Widget errorBuilder(BuildContext context, Object error)?, void onRefresh()?, bool canFetchNext(int index)?, bool canFetchPrev(int index)?}) - Creates an infinite scrolling list view.
Properties
- cacheExtent → double?
-
The extent of the area in which content is cached.
final
- canFetchNext → bool Function(int index)?
-
Determines whether more data can be fetched when scrolling forward.
final
- canFetchPrev → bool Function(int index)?
-
Determines whether more data can be fetched when scrolling backward.
final
- canRefresh → bool
-
Whether pull-to-refresh is enabled.
final
- clipBehavior → Clip
-
How to clip overflowing content.
final
- controller → ScrollController?
-
A controller for an infinite scroll list view.
final
- dataKey → Key?
-
Changing this key resets the data and fetches data again starting from initialIndex.
final
- empty → Widget?
-
Widget to display when the list is empty.
final
- errorBuilder → Widget Function(BuildContext context, Object error)?
-
Builder to display an error message when data loading fails.
final
-
fetchNext
→ Future<
List< Function(int index, int crossAxisCount)T> > -
Function to fetch the next set of items when scrolling forward.
final
-
fetchPrev
→ Future<
List< Function(int index, int crossAxisCount)?T> > -
Function to fetch the previous set of items when scrolling backward.
final
- gridDelegate → SliverGridDelegate?
-
The delegate that controls the layout of the grid tiles.
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- initialIndex → int Function()?
-
Function that returns the initial index to scroll to.
final
- initialIndexNotFoundCallback → bool Function(int index, bool giveup)?
-
Callback invoked when the index specified by initialIndex cannot be fetched by fetchNext
during initialization (e.g., when fetchNext returns an empty List).
final
- itemBuilder → Widget Function(BuildContext context, T item, int index)
-
Builder function to render each item in the list or grid.
final
- key → Key?
-
Controls how one widget replaces another widget in the tree.
finalinherited
- loading → Widget?
-
Widget to display while the initial data is loading.
final
- loadingMore → Widget?
-
Widget to display while loading more data.
final
- onIndexChanged → void Function(int index, int crossAxisCount)?
-
Called when the list scroll ends and returns the topmost index of the list.
final
- onRefresh → void Function()?
-
Callback when a refresh is triggered.
final
-
overlaySlivers
→ List<
Widget> ? -
A list of slivers to overlay on top of the scroll view.
final
- padding → EdgeInsets?
-
The amount of space by which to inset the children.
final
- physics → ScrollPhysics?
-
How the scroll view should respond to user input.
final
- prefix → Widget?
-
A widget to display before the list of items.
final
- primary → bool?
-
Whether this is the primary scroll view associated with the parent PrimaryScrollController.
final
-
refreshIndicatorBuilder
→ Widget Function(BuildContext context, Widget child, Future<
void> refresh())? -
Builder for the refresh indicator widget.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- scrollDirection → Axis
-
The axis along which the scroll view scrolls.
final
- suffix → Widget?
-
A widget to display after the list of items.
final
Methods
-
createElement(
) → StatefulElement -
Creates a StatefulElement to manage this widget's location in the tree.
inherited
-
createState(
) → State< InfiniteScrollListView< T> > -
Creates the mutable state for this widget at a given location in the tree.
override
-
debugDescribeChildren(
) → List< DiagnosticsNode> -
Returns a list of DiagnosticsNode objects describing this node's
children.
inherited
-
debugFillProperties(
DiagnosticPropertiesBuilder properties) → void -
Add additional properties associated with the node.
inherited
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toDiagnosticsNode(
{String? name, DiagnosticsTreeStyle? style}) → DiagnosticsNode -
Returns a debug representation of the object that is used by debugging
tools and by DiagnosticsNode.toStringDeep.
inherited
-
toString(
{DiagnosticLevel minLevel = DiagnosticLevel.info}) → String -
A string representation of this object.
inherited
-
toStringDeep(
{String prefixLineOne = '', String? prefixOtherLines, DiagnosticLevel minLevel = DiagnosticLevel.debug, int wrapWidth = 65}) → String -
Returns a string representation of this node and its descendants.
inherited
-
toStringShallow(
{String joiner = ', ', DiagnosticLevel minLevel = DiagnosticLevel.debug}) → String -
Returns a one-line detailed description of the object.
inherited
-
toStringShort(
) → String -
A short, textual description of this widget.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited