searchbar works but return empty doesn't refresh

This commit is contained in:
Valentin CZERYBA 2024-11-05 15:11:53 +01:00
parent 951127d7bc
commit 517652df98

View File

@ -113,9 +113,11 @@ class _MyHomePageState extends State<ListItemMenu> {
if (response.statusCode == 200) { if (response.statusCode == 200) {
final List<dynamic> body = json.decode(utf8.decode(response.bodyBytes)); final List<dynamic> body = json.decode(utf8.decode(response.bodyBytes));
setState(() { setState(() {
filteredPosts = body filteredPosts = body.isNotEmpty
.map((e) => Events.fromJson(e as Map<String, dynamic>)) ? body
.toList(); .map((e) => Events.fromJson(e as Map<String, dynamic>))
.toList()
: [];
}); });
} else { } else {
throw Exception('Failed to load posts'); throw Exception('Failed to load posts');
@ -140,6 +142,7 @@ class _MyHomePageState extends State<ListItemMenu> {
inputGeo.clear(); // Clear the text field inputGeo.clear(); // Clear the text field
geographicalZone = ''; // Reset the geographical zone state geographicalZone = ''; // Reset the geographical zone state
suggestions.clear(); // Optionally clear suggestions suggestions.clear(); // Optionally clear suggestions
filteredPosts.clear();
}); });
}, },
), ),
@ -155,9 +158,8 @@ class _MyHomePageState extends State<ListItemMenu> {
Container( Container(
height: 200, height: 200,
decoration: BoxDecoration( decoration: BoxDecoration(
border: Border.all(color: Colors.blue), // Add a border color border: Border.all(color: Colors.blue),
borderRadius: borderRadius: BorderRadius.circular(8),
BorderRadius.circular(8), // Optional: rounded corners
), ),
child: ListView.builder( child: ListView.builder(
shrinkWrap: true, shrinkWrap: true,
@ -243,16 +245,17 @@ class _MyHomePageState extends State<ListItemMenu> {
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}'),
onTap: () { onTap: () {
Navigator.push(context, Navigator.push(
MaterialPageRoute(builder: (_) => ItemMenu(title: post.id!))); context,
}); MaterialPageRoute(builder: (_) => ItemMenu(title: post.id!)),
}, );
separatorBuilder: (context, index) { },
return Divider(); );
}, },
separatorBuilder: (context, index) => Divider(),
); );
} }
} }
@ -264,7 +267,8 @@ class SearchDelegateExample extends SearchDelegate {
IconButton( IconButton(
icon: const Icon(Icons.clear), icon: const Icon(Icons.clear),
onPressed: () { onPressed: () {
query = ''; query = ''; // Clear the query text
showSuggestions(context); // Show suggestions
}, },
), ),
]; ];
@ -273,18 +277,18 @@ class SearchDelegateExample extends SearchDelegate {
@override @override
Widget buildLeading(BuildContext context) { Widget buildLeading(BuildContext context) {
return IconButton( return IconButton(
icon: const Icon(Icons.arrow_back), icon: const Icon(Icons.arrow_back), // Default back icon
onPressed: () { onPressed: () {
close(context, null); close(context, null); // Close the search delegate
}, },
); );
} }
@override @override
Widget buildResults(BuildContext context) { Widget buildResults(BuildContext context) {
// Perform the search and return the results // Here, you can return the actual results based on the query
return FutureBuilder<List<Events>>( return FutureBuilder<List<Events>>(
future: searchPosts(query), future: searchPosts(query), // Implement your own search logic
builder: (context, snapshot) { builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) { if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator()); return const Center(child: CircularProgressIndicator());
@ -317,7 +321,7 @@ class SearchDelegateExample extends SearchDelegate {
@override @override
Widget buildSuggestions(BuildContext context) { Widget buildSuggestions(BuildContext context) {
return Container(); // Implement suggestions if needed return Container(); // This is for showing search suggestions if needed
} }
Future<List<Events>> searchPosts(String query) async { Future<List<Events>> searchPosts(String query) async {