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) {
final List<dynamic> body = json.decode(utf8.decode(response.bodyBytes));
setState(() {
filteredPosts = body
.map((e) => Events.fromJson(e as Map<String, dynamic>))
.toList();
filteredPosts = body.isNotEmpty
? body
.map((e) => Events.fromJson(e as Map<String, dynamic>))
.toList()
: [];
});
} else {
throw Exception('Failed to load posts');
@ -140,6 +142,7 @@ class _MyHomePageState extends State<ListItemMenu> {
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<ListItemMenu> {
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<ListItemMenu> {
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<List<Events>>(
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<List<Events>> searchPosts(String query) async {