|
| 1 | +import 'package:dcc_toolkit/dcc_toolkit.dart'; |
| 2 | +import 'package:flutter/material.dart'; |
| 3 | + |
| 4 | +/// A widget that displays a list of items with pagination. |
| 5 | +/// |
| 6 | +/// This widget is a [CustomScrollView] that displays a paginated list of items. |
| 7 | +/// It uses a [PaginationState] to manage the behavior of the widget. |
| 8 | +/// It also uses a [NotificationListener] to listen for scroll events and load more items when the user scrolls to the bottom of the list. |
| 9 | +class PaginatedScrollView<T> extends StatelessWidget { |
| 10 | + /// Creates a new [PaginatedScrollView]. |
| 11 | + const PaginatedScrollView({ |
| 12 | + required this.state, |
| 13 | + required this.itemBuilder, |
| 14 | + this.onLoadMore, |
| 15 | + this.topWidget, |
| 16 | + this.bottomWidget, |
| 17 | + super.key, |
| 18 | + }); |
| 19 | + |
| 20 | + /// The state of the pagination. |
| 21 | + final PaginationState<T> state; |
| 22 | + |
| 23 | + /// The builder for the items. |
| 24 | + final Widget Function(BuildContext, T) itemBuilder; |
| 25 | + |
| 26 | + /// The function to call when the user scrolls to the bottom of the list, to load more items. |
| 27 | + final void Function()? onLoadMore; |
| 28 | + |
| 29 | + /// Optional widget to display at the top of the list. |
| 30 | + final Widget? topWidget; |
| 31 | + |
| 32 | + /// Optional widget to display at the bottom of the list. |
| 33 | + final Widget? bottomWidget; |
| 34 | + |
| 35 | + int get _itemCount => state.items.length; |
| 36 | + |
| 37 | + T _fetchItem(int index) { |
| 38 | + return state.items[index]; |
| 39 | + } |
| 40 | + |
| 41 | + @override |
| 42 | + Widget build(BuildContext context) { |
| 43 | + return NotificationListener<ScrollNotification>( |
| 44 | + onNotification: |
| 45 | + onLoadMore != null |
| 46 | + ? (notification) { |
| 47 | + final metrics = notification.metrics; |
| 48 | + if (metrics.extentAfter == metrics.minScrollExtent) { |
| 49 | + // We don't trigger loading if no next page is available or while we are already fetching more |
| 50 | + if (state.hasNextPage && !state.isLoading) { |
| 51 | + onLoadMore?.call(); |
| 52 | + return true; |
| 53 | + } |
| 54 | + } |
| 55 | + return false; |
| 56 | + } |
| 57 | + : null, |
| 58 | + child: CustomScrollView( |
| 59 | + physics: const AlwaysScrollableScrollPhysics(), |
| 60 | + slivers: [ |
| 61 | + if (topWidget != null) SliverToBoxAdapter(child: topWidget), |
| 62 | + SliverList.builder( |
| 63 | + itemCount: _itemCount, |
| 64 | + itemBuilder: (context, index) => itemBuilder(context, _fetchItem(index)), |
| 65 | + ), |
| 66 | + if (state.hasNextPage) |
| 67 | + const SliverToBoxAdapter( |
| 68 | + child: Padding(padding: EdgeInsets.all(Sizes.m), child: Center(child: CircularProgressIndicator())), |
| 69 | + ), |
| 70 | + if (bottomWidget != null) SliverToBoxAdapter(child: bottomWidget), |
| 71 | + //Bottom insets to be able to scroll the entire content above the FloatingActionButton |
| 72 | + const SliverPadding(padding: Paddings.vertical48), |
| 73 | + ], |
| 74 | + ), |
| 75 | + ); |
| 76 | + } |
| 77 | +} |
0 commit comments