Skip to content

Commit

Permalink
fix: ImpaktfullUiPagination now correctly handles the amount of pages
Browse files Browse the repository at this point in the history
  • Loading branch information
vanlooverenkoen committed Jan 14, 2025
1 parent 877c47d commit dbe1a90
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .flutter-plugins-dependencies
Original file line number Diff line number Diff line change
@@ -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}
{"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}
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 0.30.1

## Fix

- ImpaktfullUiPagination now correctly handles the amount of pages

# 0.30.0

## Feat
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
38 changes: 31 additions & 7 deletions lib/src/components/pagination/pagination.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<int> 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<ImpaktfullUiPaginationTheme>(
overrideComponentTheme: theme,
builder: (context, componentTheme) => ImpaktfullUiAutoLayout.horizontal(
Expand All @@ -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,
),
Expand Down
1 change: 0 additions & 1 deletion lib/src/components/pagination/pagination.describe.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit dbe1a90

Please sign in to comment.