From b796d0206f551ef611871606482e64490be8327b Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Tue, 5 Nov 2024 15:59:31 +0100 Subject: [PATCH] geographical search bars does works --- covas_mobile/lib/pages/ListItemMenu.dart | 71 ++++++++++++++++++------ 1 file changed, 54 insertions(+), 17 deletions(-) diff --git a/covas_mobile/lib/pages/ListItemMenu.dart b/covas_mobile/lib/pages/ListItemMenu.dart index 372f228..87a974e 100644 --- a/covas_mobile/lib/pages/ListItemMenu.dart +++ b/covas_mobile/lib/pages/ListItemMenu.dart @@ -62,6 +62,27 @@ class _MyHomePageState extends State { return body; } + @override + void initState() { + super.initState(); + // Initialize data fetch when the page loads + _fetchInitialData(); + } + + // Fetch initial data from API or any other necessary initialization + Future _fetchInitialData() async { + try { + // Optionally, you can fetch posts initially if needed. + List initialPosts = await getPosts(); + setState(() { + // Assign to the postsFuture and update the filtered posts if needed + filteredPosts = initialPosts; + }); + } catch (e) { + print('Error fetching initial data: $e'); + } + } + Future searchSuggestions(String input) async { await dotenv.load(fileName: ".env"); // Load .env file @@ -109,15 +130,21 @@ class _MyHomePageState extends State { "Content-Type": "application/json", HttpHeaders.cookieHeader: "access_token=$accessToken" }); - + print("status code : ${response.statusCode}"); if (response.statusCode == 200) { final List body = json.decode(utf8.decode(response.bodyBytes)); + print("results fetch : ${body}"); + // Update state after getting the response setState(() { - filteredPosts = body.isNotEmpty - ? body - .map((e) => Events.fromJson(e as Map)) - .toList() - : []; + if (body.isNotEmpty) { + // If we have results, map them to Events + filteredPosts = body + .map((e) => Events.fromJson(e as Map)) + .toList(); + } else { + // If no results, clear filteredPosts + filteredPosts.clear(); + } }); } else { throw Exception('Failed to load posts'); @@ -136,13 +163,13 @@ class _MyHomePageState extends State { labelText: 'Search by geographical zone', border: OutlineInputBorder(), suffixIcon: IconButton( - icon: Icon(Icons.clear), + icon: const Icon(Icons.clear), onPressed: () { setState(() { inputGeo.clear(); // Clear the text field geographicalZone = ''; // Reset the geographical zone state suggestions.clear(); // Optionally clear suggestions - filteredPosts.clear(); + _fetchInitialData(); // Clear the filtered posts }); }, ), @@ -236,10 +263,21 @@ class _MyHomePageState extends State { // Function to display fetched data on screen Widget buildPosts(List posts) { + print("filteredposts : ${filteredPosts}"); + final displayedPosts = filteredPosts; + print("results ${displayedPosts}"); + // If filteredPosts is empty, show a message saying no data is available + if (displayedPosts.isEmpty) { + return const Center( + child: Text('No events available for this location.', + style: TextStyle(fontSize: 18, color: Colors.grey)), + ); + } + return ListView.separated( - itemCount: posts.length, + itemCount: displayedPosts.length, itemBuilder: (context, index) { - final post = posts[index]; + final post = displayedPosts[index]; final startDate = DateTime.parse(post.startDate!); final date = DateFormat.yMd().format(startDate); final time = DateFormat.Hm().format(startDate); @@ -267,8 +305,7 @@ class SearchDelegateExample extends SearchDelegate { IconButton( icon: const Icon(Icons.clear), onPressed: () { - query = ''; // Clear the query text - showSuggestions(context); // Show suggestions + query = ''; }, ), ]; @@ -277,18 +314,18 @@ class SearchDelegateExample extends SearchDelegate { @override Widget buildLeading(BuildContext context) { return IconButton( - icon: const Icon(Icons.arrow_back), // Default back icon + icon: const Icon(Icons.arrow_back), onPressed: () { - close(context, null); // Close the search delegate + close(context, null); }, ); } @override Widget buildResults(BuildContext context) { - // Here, you can return the actual results based on the query + // Perform the search and return the results return FutureBuilder>( - future: searchPosts(query), // Implement your own search logic + future: searchPosts(query), builder: (context, snapshot) { if (snapshot.connectionState == ConnectionState.waiting) { return const Center(child: CircularProgressIndicator()); @@ -321,7 +358,7 @@ class SearchDelegateExample extends SearchDelegate { @override Widget buildSuggestions(BuildContext context) { - return Container(); // This is for showing search suggestions if needed + return Container(); // Implement suggestions if needed } Future> searchPosts(String query) async {