Skip to content

Commit

Permalink
Fix scroll
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-pratik-k committed Aug 2, 2023
1 parent a81f4af commit e0e0b1d
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 59 deletions.
4 changes: 2 additions & 2 deletions lib/ui/admin/leaves/leave_screen/admin_leaves_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ class _AdminLeavesScreenState extends State<AdminLeavesScreen> {
}

void _scrollListener() {
if (_scrollController.offset ==
_scrollController.position.maxScrollExtent) {
if (_scrollController.offset >= _scrollController.position.maxScrollExtent - 200) {
context.read<AdminLeavesBloc>().add(FetchMoreLeavesEvent());
}
}
Expand All @@ -66,6 +65,7 @@ class _AdminLeavesScreenState extends State<AdminLeavesScreen> {
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
scrolledUnderElevation: 0,
title: Text(AppLocalizations.of(context).leaves_tag),
bottom: const PreferredSize(
preferredSize: Size.fromHeight(40),
Expand Down
43 changes: 25 additions & 18 deletions lib/ui/admin/leaves/leave_screen/bloc /admin_leaves_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class AdminLeavesBloc extends Bloc<AdminLeavesEvents, AdminLeavesState> {

List<Employee> _members = [];
DocumentSnapshot<Leave>? _lastDoc;
bool _isLoadedMax = false;

AdminLeavesBloc(this._leaveRepo, this._employeeRepo)
: super(const AdminLeavesState()) {
Expand All @@ -44,6 +45,7 @@ class AdminLeavesBloc extends Bloc<AdminLeavesEvents, AdminLeavesState> {

Future<void> _fetchInitialLeaves(
FetchLeavesInitialEvent event, Emitter<AdminLeavesState> emit) async {
_isLoadedMax = false;
emit(state.copyWith(
assignSelectedEmployeeNull: true,
selectedMember: event.member,
Expand All @@ -66,24 +68,28 @@ class AdminLeavesBloc extends Bloc<AdminLeavesEvents, AdminLeavesState> {

Future<void> _fetchMoreLeaves(
FetchMoreLeavesEvent event, Emitter<AdminLeavesState> emit) async {
emit(state.copyWith(showPaginationLoading: true));
try {
final paginatedData = await _leaveRepo.leaves(
lastDoc: _lastDoc, uid: state.selectedMember?.uid);

_lastDoc = paginatedData.lastDoc;
final leaveApplications = state.leaveApplicationMap.values.merge();
leaveApplications.addAll(getLeaveApplicationFromLeaveEmployee(
leaves: paginatedData.leaves, members: _members));
emit(state.copyWith(
leavesFetchStatus: Status.success,
showPaginationLoading: false,
leaveApplicationMap: convertListToMap(leaveApplications)));
} on Exception {
emit(state.copyWith(
leavesFetchStatus: Status.error,
error: firestoreFetchDataError,
showPaginationLoading: false));
if (!state.showPaginationLoading && !_isLoadedMax) {
emit(state.copyWith(showPaginationLoading: true));
try {
final paginatedData = await _leaveRepo.leaves(
lastDoc: _lastDoc, uid: state.selectedMember?.uid);
if (paginatedData.lastDoc == _lastDoc) {
_isLoadedMax = true;
}
_lastDoc = paginatedData.lastDoc;
final leaveApplications = state.leaveApplicationMap.values.merge();
leaveApplications.addAll(getLeaveApplicationFromLeaveEmployee(
leaves: paginatedData.leaves, members: _members));
emit(state.copyWith(
leavesFetchStatus: Status.success,
showPaginationLoading: false,
leaveApplicationMap: convertListToMap(leaveApplications)));
} on Exception {
emit(state.copyWith(
leavesFetchStatus: Status.error,
error: firestoreFetchDataError,
showPaginationLoading: false));
}
}
}

Expand Down Expand Up @@ -111,6 +117,7 @@ class AdminLeavesBloc extends Bloc<AdminLeavesEvents, AdminLeavesState> {
@override
Future<void> close() {
_members.clear();
_lastDoc = null;
return super.close();
}
}
72 changes: 33 additions & 39 deletions lib/ui/user/members/members_screen/user_members_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,45 +41,39 @@ class _UserMembersScreenState extends State<UserMembersScreen> {
title: Text(AppLocalizations.of(context).members_tag),
),
backgroundColor: AppColors.whiteColor,
body: RefreshIndicator(
color: AppColors.textDark,
onRefresh: () async {
context.read<UserEmployeesBloc>().add(FetchEmployeesEvent());
},
child: BlocConsumer<UserEmployeesBloc, UserEmployeesState>(
listenWhen: (previous, current) =>
current is UserEmployeesFailureState,
listener: (context, state) {
if (state is UserEmployeesFailureState) {
showSnackBar(context: context, error: state.error);
}
},
builder: (context, state) {
if (state is UserEmployeesLoadingState) {
return const AppCircularProgressIndicator();
} else if (state is UserEmployeesSuccessState) {
return ListView.separated(
padding: const EdgeInsets.all(primaryVerticalSpacing),
itemBuilder: (BuildContext context, int index) {
Employee employee = state.employees[index];
return EmployeeCard(
employee: employee,
onTap: () {
context.goNamed(Routes.userEmployeeDetail,
extra: employee,
params: {
RoutesParamsConst.employeeId: employee.uid
});
},
);
},
separatorBuilder: (context, index) =>
const Divider(endIndent: 8, indent: 8),
itemCount: state.employees.length);
}
return const SizedBox();
}),
),
body: BlocConsumer<UserEmployeesBloc, UserEmployeesState>(
listenWhen: (previous, current) =>
current is UserEmployeesFailureState,
listener: (context, state) {
if (state is UserEmployeesFailureState) {
showSnackBar(context: context, error: state.error);
}
},
builder: (context, state) {
if (state is UserEmployeesLoadingState) {
return const AppCircularProgressIndicator();
} else if (state is UserEmployeesSuccessState) {
return ListView.separated(
padding: const EdgeInsets.all(primaryVerticalSpacing),
itemBuilder: (BuildContext context, int index) {
Employee employee = state.employees[index];
return EmployeeCard(
employee: employee,
onTap: () {
context.goNamed(Routes.userEmployeeDetail,
extra: employee,
params: {
RoutesParamsConst.employeeId: employee.uid
});
},
);
},
separatorBuilder: (context, index) =>
const Divider(endIndent: 8, indent: 8),
itemCount: state.employees.length);
}
return const SizedBox();
}),
);
}
}

0 comments on commit e0e0b1d

Please sign in to comment.