add second searchbar to search by geographical zone

This commit is contained in:
Valentin CZERYBA 2024-10-26 17:51:31 +02:00
parent c43eb789b1
commit 4e0222d4bb

View File

@ -45,6 +45,9 @@ class _MyHomePageState extends State<ListItemMenu> {
List<Events> filteredPosts = []; List<Events> filteredPosts = [];
late SearchBar searchBar; late SearchBar searchBar;
String geographicalZone = '';
String query = '';
// function to fetch data from api and return future list of posts // function to fetch data from api and return future list of posts
static Future<List<Events>> getPosts() async { static Future<List<Events>> getPosts() async {
SharedPreferences prefs = await SharedPreferences.getInstance(); SharedPreferences prefs = await SharedPreferences.getInstance();
@ -83,20 +86,29 @@ class _MyHomePageState extends State<ListItemMenu> {
MaterialPageRoute(builder: (_) => Camera(camera: value.first)))); MaterialPageRoute(builder: (_) => Camera(camera: value.first))));
} }
void _filterPosts(String query) async { void _filterPosts() async {
if (query.isNotEmpty) { if (query.isNotEmpty || geographicalZone.isNotEmpty) {
List<Events> results = await searchPosts(query); List<Events> results = await searchPosts(query);
setState(() { setState(() {
filteredPosts = results; filteredPosts = _applyFilters(results);
}); });
} else { } else {
// Reset to full list or clear results
setState(() { setState(() {
filteredPosts.clear(); filteredPosts.clear();
}); });
} }
} }
List<Events> _applyFilters(List<Events> posts) {
return posts.where((post) {
final matchesQuery =
post.name!.toLowerCase().contains(query.toLowerCase());
final matchesZone = geographicalZone.isEmpty ||
post.place!.toLowerCase().contains(geographicalZone.toLowerCase());
return matchesQuery && matchesZone;
}).toList();
}
// build function // build function
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@ -117,26 +129,42 @@ class _MyHomePageState extends State<ListItemMenu> {
), ),
], ],
), ),
body: Center( body: Column(
// FutureBuilder children: [
child: FutureBuilder<List<Events>>( // New Search Bar for Geographical Zone
future: postsFuture, Padding(
builder: (context, snapshot) { padding: const EdgeInsets.all(8.0),
if (snapshot.connectionState == ConnectionState.waiting) { child: TextField(
// until data is fetched, show loader decoration: InputDecoration(
return const CircularProgressIndicator(); labelText: 'Search by geographical zone',
} else if (snapshot.hasData) { border: OutlineInputBorder(),
// once data is fetched, display it on screen (call buildPosts()) ),
final posts = snapshot.data!; onChanged: (value) {
final displayedPosts = setState(() {
filteredPosts.isEmpty ? posts : filteredPosts; geographicalZone = value;
return buildPosts(displayedPosts); });
} else { _filterPosts(); // Call the filtering function
// if no data, show simple Text },
return const Text("No data available"); ),
} ),
}, Expanded(
), child: FutureBuilder<List<Events>>(
future: postsFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const CircularProgressIndicator();
} else if (snapshot.hasData) {
final posts = snapshot.data!;
final displayedPosts =
filteredPosts.isEmpty ? posts : filteredPosts;
return buildPosts(displayedPosts);
} else {
return const Text("No data available");
}
},
),
),
],
), ),
); );
} }