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