Merge pull request 'fixed_paginatio_w_t_liked_tenders' (#92) from fixed_paginatio_w_t_liked_tenders into main

Reviewed-on: https://repo.ravanertebat.com/TM/tm_app/pulls/92
This commit is contained in:
a.ghabeli
2025-09-03 13:25:25 +03:30
7 changed files with 351 additions and 217 deletions
+3
View File
@@ -0,0 +1,3 @@
<svg width="11" height="6" viewBox="0 0 11 6" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M9.75977 1.71094L5.60352 5.62109C5.4668 5.75781 5.30273 5.8125 5.16602 5.8125C5.00195 5.8125 4.83789 5.75781 4.70117 5.64844L0.544922 1.71094C0.271484 1.46484 0.271484 1.05469 0.517578 0.78125C0.763672 0.507812 1.17383 0.507812 1.44727 0.753906L5.16602 4.25391L8.85742 0.753906C9.13086 0.507812 9.54102 0.507812 9.78711 0.78125C10.0332 1.05469 10.0332 1.46484 9.75977 1.71094Z" fill="black" fill-opacity="0.6"/>
</svg>

After

Width:  |  Height:  |  Size: 522 B

+1
View File
@@ -62,5 +62,6 @@ class AssetsManager {
//Liked tenders //Liked tenders
static const trash = 'assets/icons/trash.svg'; static const trash = 'assets/icons/trash.svg';
static const arrowdownSmall = 'assets/icons/arrow_down_small.svg';
} }
+34 -17
View File
@@ -17,25 +17,32 @@ class LikedTendersViewModel with ChangeNotifier {
LikedTendersResponse? data; LikedTendersResponse? data;
int _offset = 0; int _offset = 0;
final int _limit = 20; final int _limit = 7;
Future<void> getLikedTenders({bool reset = false}) async { int currentPage = 1;
Future<void> getLikedTenders({bool reset = false}) async {
if (!reset && (isLoadingMore || !hasMore)) { if (!reset && (isLoadingMore || !hasMore)) {
return; return;
} }
if (reset) { if (reset) {
_offset = 0; _offset = (currentPage - 1) * _limit;
hasMore = true; hasMore = true;
}
data = null;
isLoading = true;
notifyListeners();
} else {
if (_offset == 0) { if (_offset == 0) {
isLoading = true; isLoading = true;
} else { } else {
isLoadingMore = true; isLoadingMore = true;
} }
errorMessage = null;
notifyListeners(); notifyListeners();
}
errorMessage = null;
final result = await likedTendersRepository.getLikedTenders( final result = await likedTendersRepository.getLikedTenders(
offset: _offset, offset: _offset,
@@ -47,21 +54,21 @@ class LikedTendersViewModel with ChangeNotifier {
final incoming = result.value; final incoming = result.value;
final incomingList = incoming.data?.feedback ?? []; final incomingList = incoming.data?.feedback ?? [];
if (reset || data == null) { if (reset) {
data = incoming; data = incoming;
} else { } else {
final existingList = data!.data!.feedback ?? []; final existingList = data?.data?.feedback ?? [];
final combined = List<LikedTenderFeedback>.of(existingList) final combined = List<LikedTenderFeedback>.of(existingList)
..addAll(incomingList); ..addAll(incomingList);
final newMeta = incoming.data?.meta ?? data!.data!.meta; data = data?.copyWith(
data: data?.data?.copyWith(
data = data!.copyWith( feedback: combined,
data: data!.data!.copyWith(feedback: combined, meta: newMeta), meta: incoming.data?.meta ?? data!.data!.meta,
),
); );
} }
// چون offset بر اساس limit حرکت می‌کنه
hasMore = incomingList.length == _limit; hasMore = incomingList.length == _limit;
_offset += _limit; _offset += _limit;
@@ -76,7 +83,8 @@ class LikedTendersViewModel with ChangeNotifier {
isLoading = false; isLoading = false;
isLoadingMore = false; isLoadingMore = false;
notifyListeners(); notifyListeners();
} }
Future<void> loadMoreIfNeeded() async { Future<void> loadMoreIfNeeded() async {
if (isLoadingMore || !hasMore) { if (isLoadingMore || !hasMore) {
@@ -85,6 +93,13 @@ class LikedTendersViewModel with ChangeNotifier {
await getLikedTenders(reset: false); 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) { void removeTenderById(String id) {
final current = data; final current = data;
final list = current?.data?.feedback; final list = current?.data?.feedback;
@@ -92,14 +107,16 @@ class LikedTendersViewModel with ChangeNotifier {
return; return;
} }
final List<LikedTenderFeedback> updatedList = List<LikedTenderFeedback>.of( final updatedList = List<LikedTenderFeedback>.of(list)
list, ..removeWhere((e) => e.id == id);
)..removeWhere((e) => e.id == id);
final updatedMeta = current.data?.meta?.copyWith(total: updatedList.length); final updatedMeta = current.data?.meta?.copyWith(total: updatedList.length);
data = current.copyWith( data = current.copyWith(
data: current.data?.copyWith(feedback: updatedList, meta: updatedMeta), data: current.data?.copyWith(
feedback: updatedList,
meta: updatedMeta,
),
); );
notifyListeners(); notifyListeners();
@@ -19,39 +19,10 @@ class LikedTendersDesktopPage extends StatefulWidget {
} }
class _LikedTendersDesktopPageState extends State<LikedTendersDesktopPage> { 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 @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
backgroundColor: AppColors.backgroundColor, backgroundColor: AppColors.backgroundColor,
body: Column( body: Column(
children: [ children: [
DesktopNavigationWidget( DesktopNavigationWidget(
@@ -72,9 +43,6 @@ class _LikedTendersDesktopPageState extends State<LikedTendersDesktopPage> {
Expanded( Expanded(
child: SizedBox( child: SizedBox(
width: 740, width: 740,
child: Column(
children: [
Expanded(
child: Consumer<LikedTendersViewModel>( child: Consumer<LikedTendersViewModel>(
builder: (context, viewModel, child) { builder: (context, viewModel, child) {
if (viewModel.isLoading && if (viewModel.isLoading &&
@@ -93,27 +61,26 @@ class _LikedTendersDesktopPageState extends State<LikedTendersDesktopPage> {
final feedback = viewModel.data?.data?.feedback ?? []; final feedback = viewModel.data?.data?.feedback ?? [];
if (feedback.isEmpty) { if (feedback.isEmpty) {
return const Center( return const Center(child: Text('Nothing to show'));
child: Text('چیزی برای نمایش نیست'),
);
} }
final itemCount = final currentPage = viewModel.currentPage;
feedback.length + (viewModel.isLoadingMore ? 1 : 0); final totalPages = viewModel.data?.data?.meta?.pages ?? 1;
return RefreshIndicator( return Column(
children: [
Expanded(
child: RefreshIndicator(
onRefresh: () async { onRefresh: () async {
await viewModel.getLikedTenders(reset: true); await viewModel.getLikedTenders(reset: true);
}, },
child: ListView.builder( child: ListView.builder(
controller: _scrollController,
padding: EdgeInsets.symmetric( padding: EdgeInsets.symmetric(
horizontal: 24.0.w(), horizontal: 24.0.w(),
vertical: 15.0.h(), vertical: 15.0.h(),
), ),
itemCount: itemCount, itemCount: feedback.length,
itemBuilder: (context, index) { itemBuilder: (context, index) {
if (index < feedback.length) {
final item = feedback[index]; final item = feedback[index];
return Padding( return Padding(
padding: EdgeInsets.only(bottom: 20.0.h()), padding: EdgeInsets.only(bottom: 20.0.h()),
@@ -137,29 +104,111 @@ class _LikedTendersDesktopPageState extends State<LikedTendersDesktopPage> {
child: LikedListItem(tender: item.tender!), child: LikedListItem(tender: item.tender!),
), ),
); );
} else { },
return const Padding(
padding: EdgeInsets.symmetric(vertical: 20),
child: Center(
child: SizedBox(
width: 24,
height: 24,
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(height: 30.0.h()),
],
);
},
),
), ),
), ),
], ],
@@ -71,7 +71,7 @@ class _LikedTendersMobilePageState extends State<LikedTendersMobilePage> {
final feedback = viewModel.data?.data?.feedback ?? []; final feedback = viewModel.data?.data?.feedback ?? [];
if (feedback.isEmpty) { if (feedback.isEmpty) {
return const Center(child: Text('چیزی برای نمایش نیست')); return const Center(child: Text('Nothing to show'));
} }
final itemCount = feedback.length + (viewModel.isLoadingMore ? 1 : 0); final itemCount = feedback.length + (viewModel.isLoadingMore ? 1 : 0);
@@ -109,12 +109,12 @@ class _LikedTendersMobilePageState extends State<LikedTendersMobilePage> {
), ),
); );
} else { } else {
return const Padding( return Padding(
padding: EdgeInsets.symmetric(vertical: 20), padding: EdgeInsets.symmetric(vertical: 20.0.h()),
child: Center( child: Center(
child: SizedBox( child: SizedBox(
width: 24, width: 24.0.w(),
height: 24, height: 24.0.h(),
child: CircularProgressIndicator( child: CircularProgressIndicator(
color: AppColors.secondary50, color: AppColors.secondary50,
strokeWidth: 2, strokeWidth: 2,
@@ -21,34 +21,6 @@ class LikedTendersTabletPage extends StatefulWidget {
} }
class _LikedTendersTabletPageState extends State<LikedTendersTabletPage> { 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 @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final GlobalKey<ScaffoldState> key = GlobalKey(); final GlobalKey<ScaffoldState> key = GlobalKey();
@@ -96,24 +68,26 @@ class _LikedTendersTabletPageState extends State<LikedTendersTabletPage> {
final feedback = viewModel.data?.data?.feedback ?? []; final feedback = viewModel.data?.data?.feedback ?? [];
if (feedback.isEmpty) { if (feedback.isEmpty) {
return const Center(child: Text('چیزی برای نمایش نیست')); return const Center(child: Text('Nothing to show'));
} }
final itemCount = final currentPage = viewModel.currentPage;
feedback.length + (viewModel.isLoadingMore ? 1 : 0); final totalPages = viewModel.data?.data?.meta?.pages ?? 1;
return RefreshIndicator(
return Column(
children: [
Expanded(
child: RefreshIndicator(
onRefresh: () async { onRefresh: () async {
await viewModel.getLikedTenders(reset: true); await viewModel.getLikedTenders(reset: true);
}, },
child: ListView.builder( child: ListView.builder(
controller: _scrollController,
padding: EdgeInsets.symmetric( padding: EdgeInsets.symmetric(
horizontal: 24.0.w(), horizontal: 24.0.w(),
vertical: 15.0.h(), vertical: 15.0.h(),
), ),
itemCount: itemCount, itemCount: feedback.length,
itemBuilder: (context, index) { itemBuilder: (context, index) {
if (index < feedback.length) {
final item = feedback[index]; final item = feedback[index];
return Padding( return Padding(
padding: EdgeInsets.only(bottom: 20.0.h()), padding: EdgeInsets.only(bottom: 20.0.h()),
@@ -137,24 +111,114 @@ class _LikedTendersTabletPageState extends State<LikedTendersTabletPage> {
child: LikedListItem(tender: item.tender!), child: LikedListItem(tender: item.tender!),
), ),
); );
} else { },
return const Padding(
padding: EdgeInsets.symmetric(vertical: 20),
child: Center(
child: SizedBox(
width: 24,
height: 24,
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()),
],
);
},
), ),
), ),
], ],
@@ -131,7 +131,7 @@ class _LoginDesktopPageState extends State<LoginDesktopPage> {
? Padding( ? Padding(
padding: EdgeInsets.symmetric(vertical: 20.0.h()), padding: EdgeInsets.symmetric(vertical: 20.0.h()),
child: ClipRRect( child: ClipRRect(
borderRadius: BorderRadiusGeometry.circular(32), borderRadius: BorderRadius.circular(32),
child: Image.asset( child: Image.asset(
AssetsManager.webLoginImage, AssetsManager.webLoginImage,
width: 796.0, width: 796.0,