From 517652df9887a59629e13cdfaa3e7b6d2a9caac9 Mon Sep 17 00:00:00 2001 From: Valentin CZERYBA Date: Tue, 5 Nov 2024 15:11:53 +0100 Subject: [PATCH] searchbar works but return empty doesn't refresh --- covas_mobile/lib/pages/ListItemMenu.dart | 46 +++++++++++++----------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/covas_mobile/lib/pages/ListItemMenu.dart b/covas_mobile/lib/pages/ListItemMenu.dart index f6ffa13..372f228 100644 --- a/covas_mobile/lib/pages/ListItemMenu.dart +++ b/covas_mobile/lib/pages/ListItemMenu.dart @@ -113,9 +113,11 @@ class _MyHomePageState extends State { if (response.statusCode == 200) { final List body = json.decode(utf8.decode(response.bodyBytes)); setState(() { - filteredPosts = body - .map((e) => Events.fromJson(e as Map)) - .toList(); + filteredPosts = body.isNotEmpty + ? body + .map((e) => Events.fromJson(e as Map)) + .toList() + : []; }); } else { throw Exception('Failed to load posts'); @@ -140,6 +142,7 @@ class _MyHomePageState extends State { inputGeo.clear(); // Clear the text field geographicalZone = ''; // Reset the geographical zone state suggestions.clear(); // Optionally clear suggestions + filteredPosts.clear(); }); }, ), @@ -155,9 +158,8 @@ class _MyHomePageState extends State { Container( height: 200, decoration: BoxDecoration( - border: Border.all(color: Colors.blue), // Add a border color - borderRadius: - BorderRadius.circular(8), // Optional: rounded corners + border: Border.all(color: Colors.blue), + borderRadius: BorderRadius.circular(8), ), child: ListView.builder( shrinkWrap: true, @@ -243,16 +245,17 @@ class _MyHomePageState extends State { final time = DateFormat.Hm().format(startDate); return ListTile( - title: Text('${post.name!}'), - subtitle: Text('${post.place!}\n${date} ${time}'), - onTap: () { - Navigator.push(context, - MaterialPageRoute(builder: (_) => ItemMenu(title: post.id!))); - }); - }, - separatorBuilder: (context, index) { - return Divider(); + title: Text('${post.name!}'), + subtitle: Text('${post.place!}\n${date} ${time}'), + onTap: () { + Navigator.push( + context, + MaterialPageRoute(builder: (_) => ItemMenu(title: post.id!)), + ); + }, + ); }, + separatorBuilder: (context, index) => Divider(), ); } } @@ -264,7 +267,8 @@ class SearchDelegateExample extends SearchDelegate { IconButton( icon: const Icon(Icons.clear), onPressed: () { - query = ''; + query = ''; // Clear the query text + showSuggestions(context); // Show suggestions }, ), ]; @@ -273,18 +277,18 @@ class SearchDelegateExample extends SearchDelegate { @override Widget buildLeading(BuildContext context) { return IconButton( - icon: const Icon(Icons.arrow_back), + icon: const Icon(Icons.arrow_back), // Default back icon onPressed: () { - close(context, null); + close(context, null); // Close the search delegate }, ); } @override Widget buildResults(BuildContext context) { - // Perform the search and return the results + // Here, you can return the actual results based on the query return FutureBuilder>( - future: searchPosts(query), + future: searchPosts(query), // Implement your own search logic builder: (context, snapshot) { if (snapshot.connectionState == ConnectionState.waiting) { return const Center(child: CircularProgressIndicator()); @@ -317,7 +321,7 @@ class SearchDelegateExample extends SearchDelegate { @override Widget buildSuggestions(BuildContext context) { - return Container(); // Implement suggestions if needed + return Container(); // This is for showing search suggestions if needed } Future> searchPosts(String query) async {