pagination wip
This commit is contained in:
parent
6c1c650fa1
commit
ec7a286074
@ -50,8 +50,8 @@ class _MyHomePageState extends State<ListItemMenu> {
|
||||
final AuthService _authService = AuthService();
|
||||
late ScrollController _scrollController;
|
||||
int _fetchCount = 0;
|
||||
bool _last = false;
|
||||
bool _isLoading = false;
|
||||
bool _lastData = false;
|
||||
|
||||
Future<List<Events>> postsFuture = getPosts();
|
||||
List<Events> filteredPosts = [];
|
||||
@ -149,9 +149,12 @@ class _MyHomePageState extends State<ListItemMenu> {
|
||||
}
|
||||
|
||||
void _incrementFetchCount() {
|
||||
setState(() {
|
||||
_fetchCount++;
|
||||
});
|
||||
if (!_lastData) {
|
||||
setState(() {
|
||||
_fetchCount++;
|
||||
});
|
||||
_fetchData();
|
||||
}
|
||||
}
|
||||
|
||||
void _scrollListener() {
|
||||
@ -159,7 +162,8 @@ class _MyHomePageState extends State<ListItemMenu> {
|
||||
_scrollController.position.maxScrollExtent) {
|
||||
_incrementFetchCount();
|
||||
}
|
||||
_fetchData();
|
||||
|
||||
// Scroll to top
|
||||
}
|
||||
|
||||
Future<void> _fetchData() async {
|
||||
@ -467,23 +471,15 @@ class _MyHomePageState extends State<ListItemMenu> {
|
||||
print("results fetch : ${body}");
|
||||
print("fetch count : ${_fetchCount}");
|
||||
// Update state after getting the response
|
||||
if (body.isEmpty) {
|
||||
_lastData = true;
|
||||
}
|
||||
setState(() {
|
||||
if ((body.isNotEmpty) || (_fetchCount > 0)) {
|
||||
if (body.isEmpty) {
|
||||
_fetchCount--;
|
||||
_last = true;
|
||||
}
|
||||
if (body.isNotEmpty) {
|
||||
// If we have results, map them to Events
|
||||
if (!_last) {
|
||||
filteredPosts.addAll(
|
||||
body
|
||||
.map((e) => Events.fromJson(e as Map<String, dynamic>))
|
||||
.toList(),
|
||||
);
|
||||
}
|
||||
} else {
|
||||
// If no results, clear filteredPosts
|
||||
filteredPosts.clear();
|
||||
filteredPosts.addAll(body
|
||||
.map((e) => Events.fromJson(e as Map<String, dynamic>))
|
||||
.toList());
|
||||
}
|
||||
});
|
||||
} else {
|
||||
@ -623,7 +619,7 @@ class _MyHomePageState extends State<ListItemMenu> {
|
||||
labelText: 'Search by item',
|
||||
onChanged: (value) {
|
||||
_fetchCount = 0;
|
||||
_last = false;
|
||||
_lastData = false;
|
||||
if (value.isNotEmpty) {
|
||||
setState(() {
|
||||
itemName = value;
|
||||
@ -642,7 +638,7 @@ class _MyHomePageState extends State<ListItemMenu> {
|
||||
},
|
||||
onClear: () {
|
||||
_fetchCount = 0;
|
||||
_last = false;
|
||||
_lastData = false;
|
||||
setState(() {
|
||||
inputItem.clear();
|
||||
itemName = '';
|
||||
@ -655,7 +651,7 @@ class _MyHomePageState extends State<ListItemMenu> {
|
||||
suggestions: suggestionsItem,
|
||||
onSuggestionTap: (suggestion) async {
|
||||
_fetchCount = 0;
|
||||
_last = false;
|
||||
_lastData = false;
|
||||
setState(() {
|
||||
itemName = suggestion['name'];
|
||||
inputItem.text = itemName;
|
||||
@ -672,7 +668,7 @@ class _MyHomePageState extends State<ListItemMenu> {
|
||||
labelText: 'Search by tags',
|
||||
onChanged: (value) {
|
||||
_fetchCount = 0;
|
||||
_last = false;
|
||||
_lastData = false;
|
||||
if (value.isNotEmpty) {
|
||||
setState(() {
|
||||
itemTags = value;
|
||||
@ -691,6 +687,7 @@ class _MyHomePageState extends State<ListItemMenu> {
|
||||
},
|
||||
onClear: () {
|
||||
_fetchCount = 0;
|
||||
_lastData = false;
|
||||
setState(() {
|
||||
inputTags.clear();
|
||||
});
|
||||
@ -699,7 +696,7 @@ class _MyHomePageState extends State<ListItemMenu> {
|
||||
suggestions: suggestionsTags,
|
||||
onSuggestionTap: (suggestion) async {
|
||||
_fetchCount = 0;
|
||||
_last = false;
|
||||
_lastData = false;
|
||||
|
||||
setState(() {
|
||||
itemTags = suggestion['name'];
|
||||
@ -725,7 +722,7 @@ class _MyHomePageState extends State<ListItemMenu> {
|
||||
labelText: 'Search by geographical zone',
|
||||
onChanged: (value) async {
|
||||
_fetchCount = 0;
|
||||
_last = false;
|
||||
_lastData = false;
|
||||
|
||||
if (value.isNotEmpty) {
|
||||
setState(() {
|
||||
@ -749,7 +746,7 @@ class _MyHomePageState extends State<ListItemMenu> {
|
||||
},
|
||||
onClear: () async {
|
||||
_fetchCount = 0;
|
||||
_last = false;
|
||||
_lastData = false;
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
prefs.remove("city_lat");
|
||||
prefs.remove("city_long");
|
||||
@ -766,7 +763,7 @@ class _MyHomePageState extends State<ListItemMenu> {
|
||||
suggestions: suggestionsGeo,
|
||||
onSuggestionTap: (suggestion) async {
|
||||
_fetchCount = 0;
|
||||
_last = false;
|
||||
_lastData = false;
|
||||
|
||||
final latitude = suggestion['geometry']['coordinates'][1];
|
||||
final longitude = suggestion['geometry']['coordinates'][0];
|
||||
|
Loading…
x
Reference in New Issue
Block a user