add suggestion for search item

This commit is contained in:
Valentin CZERYBA 2024-11-30 00:39:57 +01:00
parent edd2cd1581
commit 559b35c7c2

View File

@ -42,6 +42,7 @@ class _MyHomePageState extends State<ListItemMenu> {
Future<List<Events>> postsFuture = getPosts(); Future<List<Events>> postsFuture = getPosts();
List<Events> filteredPosts = []; List<Events> filteredPosts = [];
String geographicalZone = ''; String geographicalZone = '';
String itemName = '';
String query = ''; String query = '';
List<Map<String, dynamic>> suggestions = []; List<Map<String, dynamic>> suggestions = [];
List<Map<String, dynamic>> suggestionsItem = []; List<Map<String, dynamic>> suggestionsItem = [];
@ -268,37 +269,8 @@ class _MyHomePageState extends State<ListItemMenu> {
} }
} }
Future<void> searchSuggestionsByItem(String input) async { Future<Uri> getUrlForEvents() async {
SharedPreferences prefs = await SharedPreferences.getInstance(); SharedPreferences prefs = await SharedPreferences.getInstance();
var accessToken = prefs.getString("access_token") ?? "";
if (accessToken.isNotEmpty) {
final url =
'https://api.mapbox.com/geocoding/v5/mapbox.places/${input}.json?access_token=${mapboxAccessToken}&proximity=ip';
final response = await http.get(Uri.parse(url));
if (response.statusCode == 200) {
final data = json.decode(response.body);
setState(() {
suggestions = (data['features'] as List)
.map((feature) => {
'place_name': feature['place_name'],
'geometry': feature[
'geometry'], // Include geometry for latitude/longitude
})
.toList();
});
} else {
throw Exception('Failed to load suggestions');
}
}
}
Future<void> fetchPostsByLocation() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
var accessToken = prefs.getString("access_token") ?? "";
if (accessToken.isNotEmpty) {
double latitude = prefs.getDouble("city_lat") ?? 0.0; double latitude = prefs.getDouble("city_lat") ?? 0.0;
double longitude = prefs.getDouble("city_long") ?? 0.0; double longitude = prefs.getDouble("city_long") ?? 0.0;
String stringParameter = ""; String stringParameter = "";
@ -330,8 +302,7 @@ class _MyHomePageState extends State<ListItemMenu> {
dateParameter = "date_event=" + date.toString(); dateParameter = "date_event=" + date.toString();
endpoint = "events/search"; endpoint = "events/search";
} }
if ((startDatepicker.text.isNotEmpty) && if ((startDatepicker.text.isNotEmpty) && (endDatepicker.text.isNotEmpty)) {
(endDatepicker.text.isNotEmpty)) {
var startDate = DateTime.parse(formatDate(startDatepicker.text)); var startDate = DateTime.parse(formatDate(startDatepicker.text));
var endDate = DateTime.parse(formatDate(endDatepicker.text)); var endDate = DateTime.parse(formatDate(endDatepicker.text));
dateParameter = "&start_date=" + dateParameter = "&start_date=" +
@ -350,8 +321,38 @@ class _MyHomePageState extends State<ListItemMenu> {
} else { } else {
stringParameter = dateParameter; stringParameter = dateParameter;
} }
print("stringParameter : ${stringParameter}"); return Uri.parse("${globals.api}/${endpoint}?${stringParameter}");
var url = Uri.parse("${globals.api}/${endpoint}?${stringParameter}"); }
Future<void> searchSuggestionsByItem(String input) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
var accessToken = prefs.getString("access_token") ?? "";
if (accessToken.isNotEmpty) {
var url = await getUrlForEvents();
final response = await http.get(url, headers: {
"Content-Type": "application/json",
HttpHeaders.cookieHeader: "access_token=$accessToken"
});
if (response.statusCode == 200) {
final data = json.decode(response.body);
setState(() {
suggestionsItem = (data as List)
.map((feature) => {'name': feature['name']})
.toList();
});
print("status code : ${response.statusCode}");
}
}
}
Future<void> fetchPostsByLocation() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
var accessToken = prefs.getString("access_token") ?? "";
if (accessToken.isNotEmpty) {
var url = await getUrlForEvents();
final response = await http.get(url, headers: { final response = await http.get(url, headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
HttpHeaders.cookieHeader: "access_token=$accessToken" HttpHeaders.cookieHeader: "access_token=$accessToken"
@ -532,14 +533,54 @@ class _MyHomePageState extends State<ListItemMenu> {
onPressed: () { onPressed: () {
setState(() { setState(() {
inputItem.clear(); inputItem.clear();
itemName = ''; // Reset the geographical zone state
suggestionsItem.clear(); //
}); });
fetchPostsByLocation(); fetchPostsByLocation();
}, },
), ),
), ),
onSubmitted: (value) { onChanged: (value) {
if (value.isNotEmpty) {
setState(() {
itemName = value;
searchSuggestionsByItem(value);
});
} else {
setState(() {
inputItem.clear(); // Clear the text field
itemName = ''; // Reset the geographical zone state
suggestionsItem.clear(); // Optionally clear suggestions
/// Clear the filted posts
});
fetchPostsByLocation(); fetchPostsByLocation();
}
}),
if (suggestionsItem.isNotEmpty)
Container(
height: 200,
decoration: BoxDecoration(
border: Border.all(color: Colors.blue),
borderRadius: BorderRadius.circular(8),
),
child: ListView.builder(
shrinkWrap: true,
itemCount: suggestionsItem.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(suggestionsItem[index]['name']),
onTap: () async {
setState(() {
itemName = suggestionsItem[index]['name'];
inputItem.text = itemName;
suggestionsItem.clear();
});
await fetchPostsByLocation();
}, },
);
},
),
), ),
], ],
), ),