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 = [];
|
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,27 +129,43 @@ class _MyHomePageState extends State<ListItemMenu> {
|
|||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
body: Center(
|
body: Column(
|
||||||
// FutureBuilder
|
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>>(
|
child: FutureBuilder<List<Events>>(
|
||||||
future: postsFuture,
|
future: postsFuture,
|
||||||
builder: (context, snapshot) {
|
builder: (context, snapshot) {
|
||||||
if (snapshot.connectionState == ConnectionState.waiting) {
|
if (snapshot.connectionState == ConnectionState.waiting) {
|
||||||
// until data is fetched, show loader
|
|
||||||
return const CircularProgressIndicator();
|
return const CircularProgressIndicator();
|
||||||
} else if (snapshot.hasData) {
|
} else if (snapshot.hasData) {
|
||||||
// once data is fetched, display it on screen (call buildPosts())
|
|
||||||
final posts = snapshot.data!;
|
final posts = snapshot.data!;
|
||||||
final displayedPosts =
|
final displayedPosts =
|
||||||
filteredPosts.isEmpty ? posts : filteredPosts;
|
filteredPosts.isEmpty ? posts : filteredPosts;
|
||||||
return buildPosts(displayedPosts);
|
return buildPosts(displayedPosts);
|
||||||
} else {
|
} else {
|
||||||
// if no data, show simple Text
|
|
||||||
return const Text("No data available");
|
return const Text("No data available");
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user