Skip to content

Commit

Permalink
fix: resolve the pagination issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Lzyct committed Oct 1, 2024
1 parent 63d42a9 commit 03f751c
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 15 deletions.
24 changes: 10 additions & 14 deletions lib/features/users/pages/dashboard/cubit/users_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,12 @@ part 'users_cubit.freezed.dart';
part 'users_state.dart';

class UsersCubit extends Cubit<UsersState> {
late final ScrollController scrollController = ScrollController()
..addListener(() {
scrollController.addListener(() async {
if (scrollController.position.atEdge) {
if (scrollController.position.pixels != 0) {
if (currentPage < lastPage) {
currentPage++;
await fetchUsers(UsersParams(page: currentPage));
}
}
}
});
});
UsersCubit(this._getUser) : super(const _Loading());

int currentPage = 1;
int lastPage = 1;
final List<User> users = [];

UsersCubit(this._getUser) : super(const _Loading());
final GetUsers _getUser;

Future<void> refresh() async {
Expand All @@ -41,6 +29,13 @@ class UsersCubit extends Cubit<UsersState> {
await fetchUsers(UsersParams(page: currentPage));
}

Future<void> nextPage() async {
if (currentPage < lastPage) {
currentPage++;
await fetchUsers(UsersParams(page: currentPage));
}
}

Future<void> fetchUsers(UsersParams usersParams) async {
if (currentPage == 1) emit(const _Loading());

Expand All @@ -58,6 +53,7 @@ class UsersCubit extends Cubit<UsersState> {
currentPage = r.currentPage ?? 1;
lastPage = r.lastPage ?? 1;

if (currentPage != 1) emit(const _Initial());
emit(_Success(users));
},
);
Expand Down
Loading

0 comments on commit 03f751c

Please sign in to comment.