changed pagination in liked tenders tanlet and web
This commit is contained in:
@@ -62,5 +62,6 @@ class AssetsManager {
|
||||
|
||||
//Liked tenders
|
||||
static const trash = 'assets/icons/trash.svg';
|
||||
static const arrowdownSmall = 'assets/icons/arrow_down_small.svg';
|
||||
|
||||
}
|
||||
|
||||
@@ -17,67 +17,75 @@ class LikedTendersViewModel with ChangeNotifier {
|
||||
LikedTendersResponse? data;
|
||||
|
||||
int _offset = 0;
|
||||
final int _limit = 20;
|
||||
final int _limit = 7;
|
||||
|
||||
Future<void> getLikedTenders({bool reset = false}) async {
|
||||
if (!reset && (isLoadingMore || !hasMore)) {
|
||||
return;
|
||||
}
|
||||
int currentPage = 1;
|
||||
|
||||
if (reset) {
|
||||
_offset = 0;
|
||||
hasMore = true;
|
||||
}
|
||||
Future<void> getLikedTenders({bool reset = false}) async {
|
||||
if (!reset && (isLoadingMore || !hasMore)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (reset) {
|
||||
_offset = (currentPage - 1) * _limit;
|
||||
hasMore = true;
|
||||
|
||||
data = null;
|
||||
isLoading = true;
|
||||
notifyListeners();
|
||||
} else {
|
||||
if (_offset == 0) {
|
||||
isLoading = true;
|
||||
} else {
|
||||
isLoadingMore = true;
|
||||
}
|
||||
errorMessage = null;
|
||||
notifyListeners();
|
||||
|
||||
final result = await likedTendersRepository.getLikedTenders(
|
||||
offset: _offset,
|
||||
limit: _limit,
|
||||
);
|
||||
|
||||
switch (result) {
|
||||
case Ok<LikedTendersResponse>():
|
||||
final incoming = result.value;
|
||||
final incomingList = incoming.data?.feedback ?? [];
|
||||
|
||||
if (reset || data == null) {
|
||||
data = incoming;
|
||||
} else {
|
||||
final existingList = data!.data!.feedback ?? [];
|
||||
final combined = List<LikedTenderFeedback>.of(existingList)
|
||||
..addAll(incomingList);
|
||||
|
||||
final newMeta = incoming.data?.meta ?? data!.data!.meta;
|
||||
|
||||
data = data!.copyWith(
|
||||
data: data!.data!.copyWith(feedback: combined, meta: newMeta),
|
||||
);
|
||||
}
|
||||
|
||||
// چون offset بر اساس limit حرکت میکنه
|
||||
hasMore = incomingList.length == _limit;
|
||||
_offset += _limit;
|
||||
|
||||
errorMessage = null;
|
||||
break;
|
||||
|
||||
case Error<LikedTendersResponse>():
|
||||
errorMessage = result.error.toString();
|
||||
break;
|
||||
}
|
||||
|
||||
isLoading = false;
|
||||
isLoadingMore = false;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
errorMessage = null;
|
||||
|
||||
final result = await likedTendersRepository.getLikedTenders(
|
||||
offset: _offset,
|
||||
limit: _limit,
|
||||
);
|
||||
|
||||
switch (result) {
|
||||
case Ok<LikedTendersResponse>():
|
||||
final incoming = result.value;
|
||||
final incomingList = incoming.data?.feedback ?? [];
|
||||
|
||||
if (reset) {
|
||||
data = incoming;
|
||||
} else {
|
||||
final existingList = data?.data?.feedback ?? [];
|
||||
final combined = List<LikedTenderFeedback>.of(existingList)
|
||||
..addAll(incomingList);
|
||||
|
||||
data = data?.copyWith(
|
||||
data: data?.data?.copyWith(
|
||||
feedback: combined,
|
||||
meta: incoming.data?.meta ?? data!.data!.meta,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
hasMore = incomingList.length == _limit;
|
||||
_offset += _limit;
|
||||
|
||||
errorMessage = null;
|
||||
break;
|
||||
|
||||
case Error<LikedTendersResponse>():
|
||||
errorMessage = result.error.toString();
|
||||
break;
|
||||
}
|
||||
|
||||
isLoading = false;
|
||||
isLoadingMore = false;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
|
||||
Future<void> loadMoreIfNeeded() async {
|
||||
if (isLoadingMore || !hasMore) {
|
||||
return;
|
||||
@@ -85,6 +93,13 @@ class LikedTendersViewModel with ChangeNotifier {
|
||||
await getLikedTenders(reset: false);
|
||||
}
|
||||
|
||||
Future<void> jumpToPage(int page) async {
|
||||
currentPage = page;
|
||||
_offset = (page - 1) * _limit;
|
||||
hasMore = true;
|
||||
await getLikedTenders(reset: true);
|
||||
}
|
||||
|
||||
void removeTenderById(String id) {
|
||||
final current = data;
|
||||
final list = current?.data?.feedback;
|
||||
@@ -92,14 +107,16 @@ class LikedTendersViewModel with ChangeNotifier {
|
||||
return;
|
||||
}
|
||||
|
||||
final List<LikedTenderFeedback> updatedList = List<LikedTenderFeedback>.of(
|
||||
list,
|
||||
)..removeWhere((e) => e.id == id);
|
||||
final updatedList = List<LikedTenderFeedback>.of(list)
|
||||
..removeWhere((e) => e.id == id);
|
||||
|
||||
final updatedMeta = current.data?.meta?.copyWith(total: updatedList.length);
|
||||
|
||||
data = current.copyWith(
|
||||
data: current.data?.copyWith(feedback: updatedList, meta: updatedMeta),
|
||||
data: current.data?.copyWith(
|
||||
feedback: updatedList,
|
||||
meta: updatedMeta,
|
||||
),
|
||||
);
|
||||
|
||||
notifyListeners();
|
||||
|
||||
@@ -19,39 +19,10 @@ class LikedTendersDesktopPage extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _LikedTendersDesktopPageState extends State<LikedTendersDesktopPage> {
|
||||
late ScrollController _scrollController;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_scrollController = ScrollController()..addListener(_onScroll);
|
||||
}
|
||||
|
||||
void _onScroll() {
|
||||
if (!_scrollController.hasClients) {
|
||||
return;
|
||||
}
|
||||
final max = _scrollController.position.maxScrollExtent;
|
||||
final pos = _scrollController.position.pixels;
|
||||
const threshold = 200.0;
|
||||
if (max - pos <= threshold) {
|
||||
final vm = Provider.of<LikedTendersViewModel>(context, listen: false);
|
||||
vm.loadMoreIfNeeded();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_scrollController.removeListener(_onScroll);
|
||||
_scrollController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
backgroundColor: AppColors.backgroundColor,
|
||||
|
||||
body: Column(
|
||||
children: [
|
||||
DesktopNavigationWidget(
|
||||
@@ -72,93 +43,171 @@ class _LikedTendersDesktopPageState extends State<LikedTendersDesktopPage> {
|
||||
Expanded(
|
||||
child: SizedBox(
|
||||
width: 740,
|
||||
child: Column(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Consumer<LikedTendersViewModel>(
|
||||
builder: (context, viewModel, child) {
|
||||
if (viewModel.isLoading &&
|
||||
(viewModel.data?.data?.feedback ?? []).isEmpty) {
|
||||
return const Center(
|
||||
child: CircularProgressIndicator(
|
||||
color: AppColors.secondary50,
|
||||
),
|
||||
);
|
||||
}
|
||||
child: Consumer<LikedTendersViewModel>(
|
||||
builder: (context, viewModel, child) {
|
||||
if (viewModel.isLoading &&
|
||||
(viewModel.data?.data?.feedback ?? []).isEmpty) {
|
||||
return const Center(
|
||||
child: CircularProgressIndicator(
|
||||
color: AppColors.secondary50,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
if (viewModel.errorMessage != null) {
|
||||
return Center(child: Text(viewModel.errorMessage!));
|
||||
}
|
||||
if (viewModel.errorMessage != null) {
|
||||
return Center(child: Text(viewModel.errorMessage!));
|
||||
}
|
||||
|
||||
final feedback = viewModel.data?.data?.feedback ?? [];
|
||||
final feedback = viewModel.data?.data?.feedback ?? [];
|
||||
|
||||
if (feedback.isEmpty) {
|
||||
return const Center(
|
||||
child: Text('Nothing to show'),
|
||||
);
|
||||
}
|
||||
if (feedback.isEmpty) {
|
||||
return const Center(child: Text('Nothing to show'));
|
||||
}
|
||||
|
||||
final itemCount =
|
||||
feedback.length + (viewModel.isLoadingMore ? 1 : 0);
|
||||
final currentPage = viewModel.currentPage;
|
||||
final totalPages = viewModel.data?.data?.meta?.pages ?? 1;
|
||||
|
||||
return RefreshIndicator(
|
||||
return Column(
|
||||
children: [
|
||||
Expanded(
|
||||
child: RefreshIndicator(
|
||||
onRefresh: () async {
|
||||
await viewModel.getLikedTenders(reset: true);
|
||||
},
|
||||
child: ListView.builder(
|
||||
controller: _scrollController,
|
||||
padding: EdgeInsets.symmetric(
|
||||
horizontal: 24.0.w(),
|
||||
vertical: 15.0.h(),
|
||||
),
|
||||
itemCount: itemCount,
|
||||
itemCount: feedback.length,
|
||||
itemBuilder: (context, index) {
|
||||
if (index < feedback.length) {
|
||||
final item = feedback[index];
|
||||
return Padding(
|
||||
padding: EdgeInsets.only(bottom: 20.0.h()),
|
||||
child: Dismissible(
|
||||
key: ValueKey(item.id ?? index),
|
||||
direction: DismissDirection.endToStart,
|
||||
background: Container(
|
||||
alignment: Alignment.centerRight,
|
||||
padding: EdgeInsets.symmetric(
|
||||
horizontal: 20.0.w(),
|
||||
),
|
||||
child: SvgPicture.asset(
|
||||
AssetsManager.trash,
|
||||
),
|
||||
final item = feedback[index];
|
||||
return Padding(
|
||||
padding: EdgeInsets.only(bottom: 20.0.h()),
|
||||
child: Dismissible(
|
||||
key: ValueKey(item.id ?? index),
|
||||
direction: DismissDirection.endToStart,
|
||||
background: Container(
|
||||
alignment: Alignment.centerRight,
|
||||
padding: EdgeInsets.symmetric(
|
||||
horizontal: 20.0.w(),
|
||||
),
|
||||
onDismissed: (_) {
|
||||
if (item.id != null) {
|
||||
viewModel.removeTenderById(item.id!);
|
||||
}
|
||||
},
|
||||
child: LikedListItem(tender: item.tender!),
|
||||
),
|
||||
);
|
||||
} else {
|
||||
return Padding(
|
||||
padding: EdgeInsets.symmetric(vertical: 20.0.h()),
|
||||
child: Center(
|
||||
child: SizedBox(
|
||||
width: 24.0.w(),
|
||||
height: 24.0.h(),
|
||||
child: CircularProgressIndicator(
|
||||
color: AppColors.secondary50,
|
||||
strokeWidth: 2,
|
||||
),
|
||||
child: SvgPicture.asset(
|
||||
AssetsManager.trash,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
onDismissed: (_) {
|
||||
if (item.id != null) {
|
||||
viewModel.removeTenderById(item.id!);
|
||||
}
|
||||
},
|
||||
child: LikedListItem(tender: item.tender!),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
SizedBox(height: 10.0.h()),
|
||||
|
||||
Row(
|
||||
children: [
|
||||
const Spacer(),
|
||||
Text(
|
||||
'Page',
|
||||
style: TextStyle(
|
||||
fontSize: 13.0.sp(),
|
||||
fontWeight: FontWeight.w300,
|
||||
color: AppColors.grey60,
|
||||
),
|
||||
),
|
||||
SizedBox(width: 10.0.w()),
|
||||
|
||||
GestureDetector(
|
||||
onTap: () async {
|
||||
final selectedPage = await showDialog<int>(
|
||||
context: context,
|
||||
builder: (context) {
|
||||
return AlertDialog(
|
||||
title: const Text('Select Page'),
|
||||
content: SizedBox(
|
||||
width: 200,
|
||||
height: 300,
|
||||
child: ListView.builder(
|
||||
itemCount: totalPages,
|
||||
itemBuilder: (context, index) {
|
||||
final pageNumber = index + 1;
|
||||
return ListTile(
|
||||
title: Text('Page $pageNumber'),
|
||||
onTap: () =>
|
||||
Navigator.pop(context, pageNumber),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
if (selectedPage != null) {
|
||||
await viewModel.jumpToPage(selectedPage);
|
||||
}
|
||||
},
|
||||
child: Container(
|
||||
decoration: BoxDecoration(
|
||||
border: Border.all(
|
||||
color: AppColors.grey30,
|
||||
width: 1,
|
||||
),
|
||||
borderRadius: BorderRadius.circular(4),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
SizedBox(width: 5.0.w()),
|
||||
Text(
|
||||
'$currentPage',
|
||||
style: TextStyle(
|
||||
fontSize: 14.0.sp(),
|
||||
fontWeight: FontWeight.w500,
|
||||
color: AppColors.grey70,
|
||||
),
|
||||
),
|
||||
SizedBox(width: 5.0.w()),
|
||||
SvgPicture.asset(
|
||||
AssetsManager.arrowdownSmall,
|
||||
),
|
||||
SizedBox(width: 5.0.w()),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
SizedBox(width: 5.0.w()),
|
||||
Text(
|
||||
'Of',
|
||||
style: TextStyle(
|
||||
fontSize: 14.0.sp(),
|
||||
fontWeight: FontWeight.w400,
|
||||
color: AppColors.grey60,
|
||||
),
|
||||
),
|
||||
SizedBox(width: 5.0.w()),
|
||||
Text(
|
||||
'$totalPages',
|
||||
style: TextStyle(
|
||||
fontSize: 13.0.sp(),
|
||||
fontWeight: FontWeight.w400,
|
||||
color: AppColors.grey60,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
SizedBox(height: 30.0.h()),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
@@ -21,34 +21,6 @@ class LikedTendersTabletPage extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _LikedTendersTabletPageState extends State<LikedTendersTabletPage> {
|
||||
late final ScrollController _scrollController;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_scrollController = ScrollController()..addListener(_onScroll);
|
||||
}
|
||||
|
||||
void _onScroll() {
|
||||
if (!_scrollController.hasClients) {
|
||||
return;
|
||||
}
|
||||
final max = _scrollController.position.maxScrollExtent;
|
||||
final pos = _scrollController.position.pixels;
|
||||
const threshold = 200.0;
|
||||
if (max - pos <= threshold) {
|
||||
final vm = Provider.of<LikedTendersViewModel>(context, listen: false);
|
||||
vm.loadMoreIfNeeded();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_scrollController.removeListener(_onScroll);
|
||||
_scrollController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final GlobalKey<ScaffoldState> key = GlobalKey();
|
||||
@@ -99,21 +71,23 @@ class _LikedTendersTabletPageState extends State<LikedTendersTabletPage> {
|
||||
return const Center(child: Text('Nothing to show'));
|
||||
}
|
||||
|
||||
final itemCount =
|
||||
feedback.length + (viewModel.isLoadingMore ? 1 : 0);
|
||||
return RefreshIndicator(
|
||||
onRefresh: () async {
|
||||
await viewModel.getLikedTenders(reset: true);
|
||||
},
|
||||
child: ListView.builder(
|
||||
controller: _scrollController,
|
||||
padding: EdgeInsets.symmetric(
|
||||
horizontal: 24.0.w(),
|
||||
vertical: 15.0.h(),
|
||||
),
|
||||
itemCount: itemCount,
|
||||
itemBuilder: (context, index) {
|
||||
if (index < feedback.length) {
|
||||
final currentPage = viewModel.currentPage;
|
||||
final totalPages = viewModel.data?.data?.meta?.pages ?? 1;
|
||||
|
||||
return Column(
|
||||
children: [
|
||||
Expanded(
|
||||
child: RefreshIndicator(
|
||||
onRefresh: () async {
|
||||
await viewModel.getLikedTenders(reset: true);
|
||||
},
|
||||
child: ListView.builder(
|
||||
padding: EdgeInsets.symmetric(
|
||||
horizontal: 24.0.w(),
|
||||
vertical: 15.0.h(),
|
||||
),
|
||||
itemCount: feedback.length,
|
||||
itemBuilder: (context, index) {
|
||||
final item = feedback[index];
|
||||
return Padding(
|
||||
padding: EdgeInsets.only(bottom: 20.0.h()),
|
||||
@@ -137,24 +111,114 @@ class _LikedTendersTabletPageState extends State<LikedTendersTabletPage> {
|
||||
child: LikedListItem(tender: item.tender!),
|
||||
),
|
||||
);
|
||||
} else {
|
||||
return Padding(
|
||||
padding: EdgeInsets.symmetric(vertical: 20.0.h()),
|
||||
child: Center(
|
||||
child: SizedBox(
|
||||
width: 24.0.w(),
|
||||
height: 24.0.h(),
|
||||
child: CircularProgressIndicator(
|
||||
color: AppColors.secondary50,
|
||||
strokeWidth: 2,
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
SizedBox(height: 10.0.h()),
|
||||
|
||||
Row(
|
||||
children: [
|
||||
const Spacer(),
|
||||
Text(
|
||||
'Page',
|
||||
style: TextStyle(
|
||||
fontSize: 13.0.sp(),
|
||||
fontWeight: FontWeight.w300,
|
||||
color: AppColors.grey60,
|
||||
),
|
||||
),
|
||||
SizedBox(width: 10.0.w()),
|
||||
|
||||
GestureDetector(
|
||||
onTap: () async {
|
||||
final selectedPage = await showDialog<int>(
|
||||
context: context,
|
||||
builder: (context) {
|
||||
return AlertDialog(
|
||||
title: const Text('Select Page'),
|
||||
content: SizedBox(
|
||||
width: 200,
|
||||
height: 300,
|
||||
child: ListView.builder(
|
||||
itemCount: totalPages,
|
||||
itemBuilder: (context, index) {
|
||||
final pageNumber = index + 1;
|
||||
return ListTile(
|
||||
title: Text('Page $pageNumber'),
|
||||
onTap:
|
||||
() => Navigator.pop(
|
||||
context,
|
||||
pageNumber,
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
if (selectedPage != null) {
|
||||
await viewModel.jumpToPage(selectedPage);
|
||||
}
|
||||
},
|
||||
child: Container(
|
||||
decoration: BoxDecoration(
|
||||
border: Border.all(
|
||||
color: AppColors.grey30,
|
||||
width: 1,
|
||||
),
|
||||
borderRadius: BorderRadius.circular(4),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
SizedBox(width: 5.0.w()),
|
||||
Text(
|
||||
'$currentPage',
|
||||
style: TextStyle(
|
||||
fontSize: 14.0.sp(),
|
||||
fontWeight: FontWeight.w500,
|
||||
color: AppColors.grey70,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
},
|
||||
),
|
||||
); },
|
||||
SizedBox(width: 5.0.w()),
|
||||
SvgPicture.asset(
|
||||
AssetsManager.arrowdownSmall,
|
||||
),
|
||||
SizedBox(width: 5.0.w()),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
SizedBox(width: 5.0.w()),
|
||||
Text(
|
||||
'Of',
|
||||
style: TextStyle(
|
||||
fontSize: 14.0.sp(),
|
||||
fontWeight: FontWeight.w400,
|
||||
color: AppColors.grey60,
|
||||
),
|
||||
),
|
||||
SizedBox(width: 5.0.w()),
|
||||
Text(
|
||||
'$totalPages',
|
||||
style: TextStyle(
|
||||
fontSize: 13.0.sp(),
|
||||
fontWeight: FontWeight.w400,
|
||||
color: AppColors.grey60,
|
||||
),
|
||||
),
|
||||
SizedBox(width: 30.0.w()),
|
||||
],
|
||||
),
|
||||
|
||||
SizedBox(height: 30.0.h()),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user