add second searchbar to search by geographical zone
This commit is contained in:
parent
c43eb789b1
commit
4e0222d4bb
@ -45,6 +45,9 @@ class _MyHomePageState extends State<ListItemMenu> {
|
||||
List<Events> filteredPosts = [];
|
||||
late SearchBar searchBar;
|
||||
|
||||
String geographicalZone = '';
|
||||
String query = '';
|
||||
|
||||
// function to fetch data from api and return future list of posts
|
||||
static Future<List<Events>> getPosts() async {
|
||||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||
@ -83,20 +86,29 @@ class _MyHomePageState extends State<ListItemMenu> {
|
||||
MaterialPageRoute(builder: (_) => Camera(camera: value.first))));
|
||||
}
|
||||
|
||||
void _filterPosts(String query) async {
|
||||
if (query.isNotEmpty) {
|
||||
void _filterPosts() async {
|
||||
if (query.isNotEmpty || geographicalZone.isNotEmpty) {
|
||||
List<Events> results = await searchPosts(query);
|
||||
setState(() {
|
||||
filteredPosts = results;
|
||||
filteredPosts = _applyFilters(results);
|
||||
});
|
||||
} else {
|
||||
// Reset to full list or clear results
|
||||
setState(() {
|
||||
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
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@ -117,26 +129,42 @@ class _MyHomePageState extends State<ListItemMenu> {
|
||||
),
|
||||
],
|
||||
),
|
||||
body: Center(
|
||||
// FutureBuilder
|
||||
child: FutureBuilder<List<Events>>(
|
||||
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<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");
|
||||
}
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user