From dbe1a90791f4dfc6147a5fa9fb9d1d5884260451 Mon Sep 17 00:00:00 2001 From: Koen Van Looveren Date: Tue, 14 Jan 2025 17:53:21 +0100 Subject: [PATCH] fix: ImpaktfullUiPagination now correctly handles the amount of pages --- .flutter-plugins-dependencies | 2 +- CHANGELOG.md | 6 +++ .../pagination/pagination_library_item.dart | 6 +-- .../pagination_library_variant.dart | 6 +-- lib/src/components/pagination/pagination.dart | 38 +++++++++++++++---- .../pagination/pagination.describe.dart | 1 - 6 files changed, 44 insertions(+), 15 deletions(-) diff --git a/.flutter-plugins-dependencies b/.flutter-plugins-dependencies index 136b1b26..47149af9 100644 --- a/.flutter-plugins-dependencies +++ b/.flutter-plugins-dependencies @@ -1 +1 @@ -{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"rive_common","path":"/Users/vanlooverenkoen/.pub-cache/hosted/pub.dev/rive_common-0.4.11/","native_build":true,"dependencies":[]}],"android":[{"name":"rive_common","path":"/Users/vanlooverenkoen/.pub-cache/hosted/pub.dev/rive_common-0.4.11/","native_build":true,"dependencies":[]}],"macos":[{"name":"rive_common","path":"/Users/vanlooverenkoen/.pub-cache/hosted/pub.dev/rive_common-0.4.11/","native_build":true,"dependencies":[]}],"linux":[{"name":"rive_common","path":"/Users/vanlooverenkoen/.pub-cache/hosted/pub.dev/rive_common-0.4.11/","native_build":true,"dependencies":[]}],"windows":[{"name":"rive_common","path":"/Users/vanlooverenkoen/.pub-cache/hosted/pub.dev/rive_common-0.4.11/","native_build":true,"dependencies":[]}],"web":[{"name":"rive_common","path":"/Users/vanlooverenkoen/.pub-cache/hosted/pub.dev/rive_common-0.4.11/","dependencies":[]}]},"dependencyGraph":[{"name":"rive_common","dependencies":[]}],"date_created":"2025-01-09 23:07:21.028373","version":"3.24.4","swift_package_manager_enabled":false} \ No newline at end of file +{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"rive_common","path":"/Users/vanlooverenkoen/.pub-cache/hosted/pub.dev/rive_common-0.4.11/","native_build":true,"dependencies":[]}],"android":[{"name":"rive_common","path":"/Users/vanlooverenkoen/.pub-cache/hosted/pub.dev/rive_common-0.4.11/","native_build":true,"dependencies":[]}],"macos":[{"name":"rive_common","path":"/Users/vanlooverenkoen/.pub-cache/hosted/pub.dev/rive_common-0.4.11/","native_build":true,"dependencies":[]}],"linux":[{"name":"rive_common","path":"/Users/vanlooverenkoen/.pub-cache/hosted/pub.dev/rive_common-0.4.11/","native_build":true,"dependencies":[]}],"windows":[{"name":"rive_common","path":"/Users/vanlooverenkoen/.pub-cache/hosted/pub.dev/rive_common-0.4.11/","native_build":true,"dependencies":[]}],"web":[{"name":"rive_common","path":"/Users/vanlooverenkoen/.pub-cache/hosted/pub.dev/rive_common-0.4.11/","dependencies":[]}]},"dependencyGraph":[{"name":"rive_common","dependencies":[]}],"date_created":"2025-01-14 15:34:09.714435","version":"3.24.4","swift_package_manager_enabled":false} \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 69029cfe..362f4231 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# 0.30.1 + +## Fix + +- ImpaktfullUiPagination now correctly handles the amount of pages + # 0.30.0 ## Feat diff --git a/example/lib/src/component_library/items/pagination/pagination_library_item.dart b/example/lib/src/component_library/items/pagination/pagination_library_item.dart index 21e9478c..462050e9 100644 --- a/example/lib/src/component_library/items/pagination/pagination_library_item.dart +++ b/example/lib/src/component_library/items/pagination/pagination_library_item.dart @@ -20,13 +20,13 @@ class PaginationLibraryItem extends ComponentLibraryItem { class PaginationLibraryInputs extends ComponentLibraryInputs { final page = ComponentLibraryIntInput( 'Page', - initialValue: 1, - min: 1, + initialValue: 0, + min: 0, ); final amountOfItems = ComponentLibraryIntInput( 'amountOfItems', initialValue: 55, - min: 1, + min: 0, ); final itemsPerPage = ComponentLibraryIntInput( 'itemsPerPage', diff --git a/example/lib/src/component_library/items/pagination/pagination_library_variant.dart b/example/lib/src/component_library/items/pagination/pagination_library_variant.dart index 33c6e711..97c09455 100644 --- a/example/lib/src/component_library/items/pagination/pagination_library_variant.dart +++ b/example/lib/src/component_library/items/pagination/pagination_library_variant.dart @@ -15,9 +15,9 @@ class PaginationLibraryVariant BuildContext context, PaginationLibraryPrimaryInputs inputs) { return [ ImpaktfullUiPagination( - amountOfItems: inputs.amountOfItems.value ?? 1, - itemsPerPage: inputs.itemsPerPage.value ?? 1, - page: inputs.page.value ?? 1, + amountOfItems: inputs.amountOfItems.value ?? 0, + itemsPerPage: inputs.itemsPerPage.value ?? 0, + page: inputs.page.value ?? 0, onLoadPage: (page) { inputs.page.updateState(page); ImpaktfullUiNotification.show(title: 'Load page $page'); diff --git a/lib/src/components/pagination/pagination.dart b/lib/src/components/pagination/pagination.dart index 153dea32..b294ae83 100644 --- a/lib/src/components/pagination/pagination.dart +++ b/lib/src/components/pagination/pagination.dart @@ -1,3 +1,5 @@ +import 'dart:math'; + import 'package:flutter/material.dart'; import 'package:impaktfull_ui/src/components/auto_layout/auto_layout.dart'; import 'package:impaktfull_ui/src/components/button/button.dart'; @@ -11,26 +13,48 @@ part 'pagination.describe.dart'; class ImpaktfullUiPagination extends StatelessWidget with ComponentDescriptorMixin { + /// The page should be zero index based final int page; final int itemsPerPage; - final int amountOfItems; + final int? _amountOfPages; + final int? _amountOfItems; final ValueChanged onLoadPage; final ImpaktfullUiPaginationTheme? theme; - int get amountOfPages => (amountOfItems / itemsPerPage).ceil(); - bool get isFinalPage => page >= amountOfPages; + int get amountOfPages { + final amountOfPages = _amountOfPages; + if (amountOfPages != null) return amountOfPages; + final amountOfItems = _amountOfItems; + if (amountOfItems != null) return (amountOfItems / itemsPerPage).ceil(); + throw Exception('amountOfPages or amountOfItems is required'); + } + + bool get isFinalPage => page >= amountOfPages - 1; const ImpaktfullUiPagination({ required this.page, required this.itemsPerPage, - required this.amountOfItems, + required int amountOfItems, + required this.onLoadPage, + this.theme, + super.key, + }) : _amountOfItems = amountOfItems, + _amountOfPages = null; + + const ImpaktfullUiPagination.withAmountOfPages({ + required this.page, + required this.itemsPerPage, required this.onLoadPage, + required int amountOfPages, this.theme, super.key, - }); + }) : _amountOfPages = amountOfPages, + _amountOfItems = null; @override Widget build(BuildContext context) { + final humanReadablePage = page + 1; + final humanReadableAmountOfPages = max(amountOfPages, 1); return ImpaktfullUiComponentThemeBuilder( overrideComponentTheme: theme, builder: (context, componentTheme) => ImpaktfullUiAutoLayout.horizontal( @@ -40,11 +64,11 @@ class ImpaktfullUiPagination extends StatelessWidget ImpaktfullUiButton( type: ImpaktfullUiButtonType.secondaryGrey, leadingAsset: componentTheme.assets.arrowLeft, - onTap: page == 1 ? null : () => onLoadPage(page - 1), + onTap: page == 0 ? null : () => onLoadPage(page - 1), ), Expanded( child: Text( - 'Page $page of $amountOfPages', + 'Page $humanReadablePage of $humanReadableAmountOfPages', style: componentTheme.textStyles.text, textAlign: TextAlign.center, ), diff --git a/lib/src/components/pagination/pagination.describe.dart b/lib/src/components/pagination/pagination.describe.dart index a0319acb..8f5f06f2 100644 --- a/lib/src/components/pagination/pagination.describe.dart +++ b/lib/src/components/pagination/pagination.describe.dart @@ -3,7 +3,6 @@ part of 'pagination.dart'; String _describeInstance( BuildContext context, ImpaktfullUiPagination instance) { final descriptor = ComponentDescriptor(); - descriptor.add('amountOfItems', instance.amountOfItems); descriptor.add('itemsPerPage', instance.itemsPerPage); descriptor.add('page', instance.page); descriptor.add('theme', instance.onLoadPage);