|
| 1 | +import 'package:dcc_toolkit/dcc_toolkit.dart'; |
| 2 | +import 'package:flutter/material.dart'; |
| 3 | + |
| 4 | +/// A widget that displays a different widget based on the state of the pagination |
| 5 | +/// |
| 6 | +/// [state] is the state of the pagination |
| 7 | +/// [loadingWidget] is the widget to display when [state] is [PaginationState.isLoading] |
| 8 | +/// [emptyWidget] is the widget to display when [state] is [PaginationState.items] is empty |
| 9 | +/// [errorWidget] is the widget to display when [state] is [PaginationState.hasError] |
| 10 | +/// [builder] is the builder for the actual success view (usually [PaginatedScrollView]) |
| 11 | +/// [onRefresh] is the refresh callback for [RefreshIndicator] |
| 12 | +class PaginationStateView<T> extends StatelessWidget { |
| 13 | + /// Constructor |
| 14 | + const PaginationStateView({ |
| 15 | + required this.state, |
| 16 | + required this.loadingWidget, |
| 17 | + required this.emptyWidget, |
| 18 | + required this.errorWidget, |
| 19 | + required this.builder, |
| 20 | + this.onRefresh, |
| 21 | + super.key, |
| 22 | + }); |
| 23 | + |
| 24 | + /// The state of the pagination |
| 25 | + final PaginationState<T> state; |
| 26 | + |
| 27 | + /// Custom widget for [PaginationState.isLoading] |
| 28 | + final Widget loadingWidget; |
| 29 | + |
| 30 | + /// Custom widget for [PaginationState.items] is empty |
| 31 | + final Widget emptyWidget; |
| 32 | + |
| 33 | + /// Custom widget for [PaginationState.hasError] |
| 34 | + final Widget errorWidget; |
| 35 | + |
| 36 | + /// Builder for the actual success view (usually [PaginatedScrollView]) |
| 37 | + final WidgetBuilder builder; |
| 38 | + |
| 39 | + /// Optional: refresh callback for [RefreshIndicator] |
| 40 | + final RefreshCallback? onRefresh; |
| 41 | + |
| 42 | + @override |
| 43 | + Widget build(BuildContext context) { |
| 44 | + // If there is a refresh callback and the state is not loading, enable the refresh indicator |
| 45 | + return onRefresh != null && !state.isLoading |
| 46 | + ? RefreshIndicator( |
| 47 | + onRefresh: onRefresh!, |
| 48 | + child: switch (state) { |
| 49 | + PaginationState(isLoading: true) => loadingWidget, |
| 50 | + PaginationState(hasError: true) => errorWidget, |
| 51 | + PaginationState(items: const []) => emptyWidget, |
| 52 | + PaginationState() => builder(context), |
| 53 | + }, |
| 54 | + ) |
| 55 | + : switch (state) { |
| 56 | + PaginationState(isLoading: true) => loadingWidget, |
| 57 | + PaginationState(hasError: true) => errorWidget, |
| 58 | + PaginationState(items: const []) => emptyWidget, |
| 59 | + PaginationState() => builder(context), |
| 60 | + }; |
| 61 | + } |
| 62 | +} |
0 commit comments