add start_date and end_date

This commit is contained in:
Valentin CZERYBA 2024-11-23 21:52:50 +01:00
parent 431b8d78ad
commit bae64ebd35
2 changed files with 92 additions and 22 deletions

View File

@ -46,7 +46,8 @@ class _MyHomePageState extends State<ListItemMenu> {
String query = '';
List<Map<String, dynamic>> suggestions = [];
TextEditingController inputGeo = TextEditingController();
TextEditingController Datepicker = TextEditingController();
TextEditingController startDatepicker = TextEditingController();
TextEditingController endDatepicker = TextEditingController();
// Fetching events from API
static Future<List<Events>> getPosts() async {
@ -130,10 +131,21 @@ class _MyHomePageState extends State<ListItemMenu> {
DateTime currentDateTime = DateTime.now();
var url = Uri.parse(
"${globals.api}/events?current_datetime=${currentDateTime.toString()}");
if (Datepicker.text.isNotEmpty) {
var date = DateTime.parse(formatDate(Datepicker.text));
if (startDatepicker.text.isNotEmpty) {
var date = DateTime.parse(formatDate(startDatepicker.text));
url = Uri.parse("${globals.api}/events?date_event=${date.toString()}");
}
if (endDatepicker.text.isNotEmpty) {
var date = DateTime.parse(formatDate(endDatepicker.text));
url = Uri.parse("${globals.api}/events?date_event=${date.toString()}");
}
if ((startDatepicker.text.isNotEmpty) &&
(endDatepicker.text.isNotEmpty)) {
var start_date = DateTime.parse(formatDate(startDatepicker.text));
var end_date = DateTime.parse(formatDate(endDatepicker.text));
url = Uri.parse(
"${globals.api}/events?start_date=${start_date.toString()}&end_date=${end_date.toString()}");
}
final response = await http.get(url, headers: {
"Content-Type": "application/json",
HttpHeaders.cookieHeader: "access_token=${accessToken}"
@ -320,15 +332,31 @@ class _MyHomePageState extends State<ListItemMenu> {
var url = Uri.parse("${globals.api}/events/search"
"?min_lat=$minLat&max_lat=$maxLat"
"&min_lon=$minLon&max_lon=$maxLon&current_datetime=${currentDate.toString()}");
if (Datepicker.text.isNotEmpty) {
var date = DateTime.parse(formatDate(Datepicker.text));
print("date_event ${date.toString()}");
if (startDatepicker.text.isNotEmpty) {
var date = DateTime.parse(formatDate(startDatepicker.text));
url = Uri.parse("${globals.api}/events/search"
"?min_lat=$minLat&max_lat=$maxLat"
"&min_lon=$minLon&max_lon=$maxLon&date_event=" +
date.toString());
}
if (endDatepicker.text.isNotEmpty) {
var date = DateTime.parse(formatDate(endDatepicker.text));
url = Uri.parse("${globals.api}/events/search"
"?min_lat=$minLat&max_lat=$maxLat"
"&min_lon=$minLon&max_lon=$maxLon&date_event=" +
date.toString());
}
if ((startDatepicker.text.isNotEmpty) &&
(endDatepicker.text.isNotEmpty)) {
var start_date = DateTime.parse(formatDate(startDatepicker.text));
var end_date = DateTime.parse(formatDate(endDatepicker.text));
url = Uri.parse("${globals.api}/events/search"
"?min_lat=$minLat&max_lat=$maxLat"
"&min_lon=$minLon&max_lon=$maxLon&start_date=" +
start_date.toString() +
"&end_date=" +
end_date.toString());
}
final response = await http.get(url, headers: {
"Content-Type": "application/json",
HttpHeaders.cookieHeader: "access_token=$accessToken"
@ -355,15 +383,33 @@ class _MyHomePageState extends State<ListItemMenu> {
}
}
onTapFunctionDatePicker({required BuildContext context}) async {
onTapFunctionDatePicker(
{required BuildContext context, String position = ""}) async {
DateTime dateEvent = DateTime.now();
if (startDatepicker.text.isNotEmpty) {
dateEvent = DateTime.parse(formatDate(startDatepicker.text));
}
DateTime? pickedDate = await showDatePicker(
context: context, firstDate: dateEvent, lastDate: DateTime(2104));
if (pickedDate == null) return;
Datepicker.text = DateFormat("dd-MM-yyyy").format(pickedDate);
if (position == "start") {
startDatepicker.text = DateFormat("dd-MM-yyyy").format(pickedDate);
} else if (position == "end") {
endDatepicker.text = DateFormat("dd-MM-yyyy").format(pickedDate);
}
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString("date_event", Datepicker.text);
if (startDatepicker.text.isNotEmpty) {
prefs.setString("date_event", startDatepicker.text);
prefs.setString("start_date", startDatepicker.text);
}
if (endDatepicker.text.isNotEmpty) {
prefs.setString("date_event", endDatepicker.text);
prefs.setString("end_date", endDatepicker.text);
}
if ((startDatepicker.text.isNotEmpty) && (endDatepicker.text.isNotEmpty)) {
prefs.remove("date_event");
}
if (inputGeo.text.isEmpty) {
_fetchInitialData();
} else {
@ -371,25 +417,39 @@ class _MyHomePageState extends State<ListItemMenu> {
}
}
Padding _BuildDateField() {
Padding _buildDateField(String position) {
TextEditingController datePicker = startDatepicker;
String hintText = "Date de début";
if (position == "end") {
datePicker = endDatepicker;
hintText = "Date de fin";
}
return Padding(
padding: const EdgeInsets.all(8.0),
//padding: EdgeInsets.symmetric(horizontal: 15),
child: TextFormField(
controller: Datepicker,
controller: datePicker,
readOnly: true,
decoration: InputDecoration(
border: OutlineInputBorder(),
suffixIcon: IconButton(
icon: const Icon(Icons.clear),
onPressed: () async {
setState(() {
datePicker.text = '';
});
SharedPreferences prefs =
await SharedPreferences.getInstance();
prefs.remove("date_event");
setState(() {
Datepicker.text = '';
});
if (position == "start") {
prefs.remove("start_date");
}
if (position == "end") {
prefs.remove("end_date");
}
if ((startDatepicker.text.isEmpty) &&
(endDatepicker.text.isEmpty)) {
prefs.remove("date_event");
}
if (inputGeo.text.isNotEmpty) {
fetchPostsByLocation();
} else {
@ -397,8 +457,9 @@ class _MyHomePageState extends State<ListItemMenu> {
}
},
),
hintText: 'Recherche par date'),
onTap: () => onTapFunctionDatePicker(context: context)),
hintText: hintText),
onTap: () =>
onTapFunctionDatePicker(context: context, position: position)),
);
}
@ -501,7 +562,8 @@ class _MyHomePageState extends State<ListItemMenu> {
body: Column(
children: [
_buildGeographicalZoneSearchField(),
_BuildDateField(),
_buildDateField("start"),
_buildDateField("end"),
Expanded(
child: FutureBuilder<List<Events>>(
future: postsFuture,

View File

@ -94,12 +94,20 @@ class SearchDelegateExample extends SearchDelegate {
String parameter = "current_datetime";
String currentDate = DateTime.now().toString();
String date = prefs.getString("date_event") ?? "";
String startDate = prefs.getString("start_date") ?? "";
String endDate = prefs.getString("end_date") ?? "";
if (date.isNotEmpty) {
currentDate = DateTime.parse(formatDate(date)).toString();
parameter = "date_event";
}
String parameterString = "${parameter}=${currentDate}";
if ((startDate.isNotEmpty) && (endDate.isNotEmpty)) {
startDate = DateTime.parse(formatDate(startDate)).toString();
endDate = DateTime.parse(formatDate(endDate)).toString();
parameterString = "start_date=${startDate}&end_date=${endDate}";
}
var url = Uri.parse(
"${globals.api}/events/search?item=${query}&${parameter}=${currentDate}");
"${globals.api}/events/search?item=${query}&${parameterString}");
if ((latitude != 0.0) && (longitude != 0.0)) {
// Now use the latitude and longitude to get events within a 50km radius
double radiusInKm = 50;
@ -111,7 +119,7 @@ class SearchDelegateExample extends SearchDelegate {
double maxLon = longitude + lonDistance;
// Construct the search URL with the item query and latitude/longitude bounds
url = Uri.parse(
"${globals.api}/events/search?item=${query}&min_lat=${minLat}&max_lat=${maxLat}&min_lon=${minLon}&max_lon=${maxLon}&${parameter}=${currentDate}");
"${globals.api}/events/search?item=${query}&min_lat=${minLat}&max_lat=${maxLat}&min_lon=${minLon}&max_lon=${maxLon}&${parameterString}");
}
final response = await http.get(url, headers: {
"Content-Type": "application/json",