add increment and decrement to pagination
This commit is contained in:
parent
ce2a061bf0
commit
4f7c8f60d0
@ -48,6 +48,9 @@ class ListItemMenu extends StatefulWidget {
|
|||||||
class _MyHomePageState extends State<ListItemMenu> {
|
class _MyHomePageState extends State<ListItemMenu> {
|
||||||
BannerAd? _bannerAd;
|
BannerAd? _bannerAd;
|
||||||
final AuthService _authService = AuthService();
|
final AuthService _authService = AuthService();
|
||||||
|
late ScrollController _scrollController;
|
||||||
|
int _fetchCount = 0;
|
||||||
|
bool _isLoading = false;
|
||||||
|
|
||||||
Future<List<Events>> postsFuture = getPosts();
|
Future<List<Events>> postsFuture = getPosts();
|
||||||
List<Events> filteredPosts = [];
|
List<Events> filteredPosts = [];
|
||||||
@ -144,6 +147,46 @@ class _MyHomePageState extends State<ListItemMenu> {
|
|||||||
return "${year}-${month}-${day}";
|
return "${year}-${month}-${day}";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _incrementFetchCount() {
|
||||||
|
setState(() {
|
||||||
|
_fetchCount++;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void _decrementFetchCount() {
|
||||||
|
setState(() {
|
||||||
|
if (_fetchCount > 0 && filteredPosts.isNotEmpty) {
|
||||||
|
_fetchCount--;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void _scrollListener() {
|
||||||
|
if (_scrollController.position.pixels ==
|
||||||
|
_scrollController.position.maxScrollExtent) {
|
||||||
|
_incrementFetchCount();
|
||||||
|
} else if (_scrollController.position.pixels ==
|
||||||
|
_scrollController.position.minScrollExtent) {
|
||||||
|
_decrementFetchCount();
|
||||||
|
}
|
||||||
|
_fetchData();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _fetchData() async {
|
||||||
|
print("Counter : ${_fetchCount}");
|
||||||
|
if (_isLoading) return;
|
||||||
|
setState(() {
|
||||||
|
_isLoading = true;
|
||||||
|
});
|
||||||
|
|
||||||
|
await Future.delayed(Duration(seconds: 2));
|
||||||
|
fetchPostsByLocation();
|
||||||
|
|
||||||
|
setState(() {
|
||||||
|
_isLoading = false;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
@ -153,10 +196,19 @@ class _MyHomePageState extends State<ListItemMenu> {
|
|||||||
_bannerAd = ad;
|
_bannerAd = ad;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
_scrollController = ScrollController();
|
||||||
|
_scrollController.addListener(_scrollListener);
|
||||||
|
|
||||||
// Initialize data fetch when the page loads
|
// Initialize data fetch when the page loads
|
||||||
_getCurrentLocation();
|
_getCurrentLocation();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void dispose() {
|
||||||
|
_scrollController.dispose();
|
||||||
|
super.dispose();
|
||||||
|
}
|
||||||
|
|
||||||
// Get the device's current location
|
// Get the device's current location
|
||||||
Future<void> _getCurrentLocation() async {
|
Future<void> _getCurrentLocation() async {
|
||||||
PermissionStatus status = await Permission.location.status;
|
PermissionStatus status = await Permission.location.status;
|
||||||
@ -347,7 +399,8 @@ class _MyHomePageState extends State<ListItemMenu> {
|
|||||||
queryParameters = dateParameter;
|
queryParameters = dateParameter;
|
||||||
}
|
}
|
||||||
|
|
||||||
return Uri.parse("${globals.api}/$endpoint?$queryParameters");
|
return Uri.parse(
|
||||||
|
"${globals.api}/$endpoint?$queryParameters&skip=${_fetchCount}");
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> searchSuggestionsByItem(String input) async {
|
Future<void> searchSuggestionsByItem(String input) async {
|
||||||
@ -770,12 +823,22 @@ class _MyHomePageState extends State<ListItemMenu> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return ListView.separated(
|
return ListView.separated(
|
||||||
itemCount: displayedPosts.length,
|
controller: _scrollController,
|
||||||
|
itemCount: displayedPosts.isNotEmpty
|
||||||
|
? displayedPosts.length +
|
||||||
|
(_isLoading ? 1 : 0) // Add 1 only if loading
|
||||||
|
: 0,
|
||||||
itemBuilder: (context, index) {
|
itemBuilder: (context, index) {
|
||||||
|
if (index >= displayedPosts.length) {
|
||||||
|
return _isLoading
|
||||||
|
? Center(child: CircularProgressIndicator())
|
||||||
|
: SizedBox.shrink();
|
||||||
|
}
|
||||||
final post = displayedPosts[index];
|
final post = displayedPosts[index];
|
||||||
final startDate = DateTime.parse(post.startDate!);
|
final startDate = DateTime.parse(post.startDate!);
|
||||||
final date = DateFormat.yMd().format(startDate);
|
final date = DateFormat.yMd().format(startDate);
|
||||||
final time = DateFormat.Hm().format(startDate);
|
final time = DateFormat.Hm().format(startDate);
|
||||||
|
|
||||||
return ListTile(
|
return ListTile(
|
||||||
title: Text('${post.name!}'),
|
title: Text('${post.name!}'),
|
||||||
subtitle: Text('${post.place!}\n${date} ${time}'),
|
subtitle: Text('${post.place!}\n${date} ${time}'),
|
||||||
|
@ -13,10 +13,10 @@ packages:
|
|||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
name: args
|
name: args
|
||||||
sha256: bf9f5caeea8d8fe6721a9c358dd8a5c1947b27f1cfaa18b39c301273594919e6
|
sha256: d0481093c50b1da8910eb0bb301626d4d8eb7284aa739614d2b394ee09e3ea04
|
||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.6.0"
|
version: "2.7.0"
|
||||||
asn1lib:
|
asn1lib:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user