fix datepicker

This commit is contained in:
Valentin CZERYBA 2024-11-08 16:57:07 +01:00
parent 44a0691e31
commit 00a9125eb2
2 changed files with 59 additions and 44 deletions

View File

@ -87,38 +87,41 @@ class DisplayPictureScreenState extends State<DisplayPictureScreen>
Future<void> searchEvents(String json, String imagePath) async { Future<void> searchEvents(String json, String imagePath) async {
print(json.replaceAll("'''json", '').replaceAll("'''", "")); print(json.replaceAll("'''json", '').replaceAll("'''", ""));
SharedPreferences prefs = await SharedPreferences.getInstance(); SharedPreferences prefs = await SharedPreferences.getInstance();
try {
Map<String, dynamic> jsonData =
jsonDecode(json.replaceAll("```json", '').replaceAll("```", ""));
print("json : ${jsonData}");
var name = jsonData["name"];
print("name : ${name}");
var place = jsonData["place"];
var accessToken = prefs.getString("access_token") ?? "";
Map<String, dynamic> jsonData = if (accessToken.isNotEmpty) {
jsonDecode(json.replaceAll("```json", '').replaceAll("```", "")); var urlGet = Uri.parse("${globals.api}/events?name=${name}");
print("json : ${jsonData}");
var name = jsonData["name"];
print("name : ${name}");
var place = jsonData["place"];
var accessToken = prefs.getString("access_token") ?? "";
if (accessToken.isNotEmpty) { var responseGet = await http.get(urlGet,
var urlGet = Uri.parse("${globals.api}/events?name=${name}"); headers: {HttpHeaders.cookieHeader: 'access_token=${accessToken}'});
if (responseGet.statusCode == 200) {
var responseGet = await http.get(urlGet, var events = jsonDecode(utf8.decode(responseGet.bodyBytes));
headers: {HttpHeaders.cookieHeader: 'access_token=${accessToken}'}); print("reponse http : ${events.length}");
if (responseGet.statusCode == 200) { if (events.length == 0) {
var events = jsonDecode(utf8.decode(responseGet.bodyBytes)); Navigator.push(
print("reponse http : ${events.length}"); context,
if (events.length == 0) { MaterialPageRoute(
Navigator.push( builder: (_) => UpdateeventImage(
context, events: jsonData, imagePath: imagePath)));
MaterialPageRoute( } else {
builder: (_) => UpdateeventImage( Navigator.push(
events: jsonData, imagePath: imagePath))); context,
} else { MaterialPageRoute(
Navigator.push( builder: (_) => ItemMenu(title: events[0]["id"])));
context, }
MaterialPageRoute(
builder: (_) => ItemMenu(title: events[0]["id"])));
} }
} else {
showErrorDialog(context, "Erreur de token");
} }
} else { } catch (e) {
showErrorDialog(context, "Erreur de token"); showErrorDialog(context, "Erreur de format de donnée fourni par l'IA");
} }
//showDescImageAddDialog(context, message); //showDescImageAddDialog(context, message);

View File

@ -70,10 +70,16 @@ class _UpdateeventImageState extends State<UpdateeventImage>
if (position == "end") { if (position == "end") {
date = "end_date"; date = "end_date";
} }
DateTime dateEvent;
if (widget.events[date].toString().isEmpty) {
dateEvent = DateTime.now();
} else {
dateEvent = DateTime.parse(widget.events[date]);
}
DateTime? pickedDate = await showDatePicker( DateTime? pickedDate = await showDatePicker(
context: context, context: context,
firstDate: DateTime.parse(widget.events[date]), firstDate: dateEvent,
initialDate: DateTime.parse(widget.events[date]), initialDate: dateEvent,
lastDate: DateTime(2104)); lastDate: DateTime(2104));
if (pickedDate == null) return; if (pickedDate == null) return;
if (position == "start") { if (position == "start") {
@ -90,10 +96,14 @@ class _UpdateeventImageState extends State<UpdateeventImage>
if (position == "end") { if (position == "end") {
date = "end_date"; date = "end_date";
} }
TimeOfDay? pickedDate = await showTimePicker( TimeOfDay timeEvent;
context: context, if (widget.events[date].toString().isEmpty) {
initialTime: timeEvent = TimeOfDay.now();
TimeOfDay.fromDateTime(DateTime.parse(widget.events[date]))); } else {
timeEvent = TimeOfDay.fromDateTime(DateTime.parse(widget.events[date]));
}
TimeOfDay? pickedDate =
await showTimePicker(context: context, initialTime: timeEvent);
if (pickedDate == null) return; if (pickedDate == null) return;
if (position == "start") { if (position == "start") {
startTimepicker.text = pickedDate.format(context); startTimepicker.text = pickedDate.format(context);
@ -274,16 +284,18 @@ class _UpdateeventImageState extends State<UpdateeventImage>
inputName.text = convertNulltoEmptyString(widget.events["name"]); inputName.text = convertNulltoEmptyString(widget.events["name"]);
inputGeo.text = convertNulltoEmptyString(widget.events["place"]); inputGeo.text = convertNulltoEmptyString(widget.events["place"]);
inputDesc.text = convertNulltoEmptyString(widget.events["description"]); inputDesc.text = convertNulltoEmptyString(widget.events["description"]);
if (widget.events["start_date"].toString().isNotEmpty) {
DateTime pickedStartDate = DateTime pickedStartDate =
DateTime.parse(convertNulltoEmptyString(widget.events["start_date"])); DateTime.parse(convertNulltoEmptyString(widget.events["start_date"]));
DateTime pickedEndDate = startDatepicker.text = DateFormat("dd-MM-yyyy").format(pickedStartDate);
DateTime.parse(convertNulltoEmptyString(widget.events["end_date"])); startTimepicker.text = DateFormat("HH-mm").format(pickedStartDate);
}
startDatepicker.text = DateFormat("dd-MM-yyyy").format(pickedStartDate); if (widget.events["end_date"].toString().isNotEmpty) {
endDatepicker.text = DateFormat("dd-MM-yyyy").format(pickedEndDate); DateTime pickedEndDate =
startTimepicker.text = DateFormat("HH-mm").format(pickedStartDate); DateTime.parse(convertNulltoEmptyString(widget.events["end_date"]));
endTimepicker.text = DateFormat("HH-mm").format(pickedEndDate); endDatepicker.text = DateFormat("dd-MM-yyyy").format(pickedEndDate);
endTimepicker.text = DateFormat("HH-mm").format(pickedEndDate);
}
initialTags = List<String>.from(widget.events['tags'] as List); initialTags = List<String>.from(widget.events['tags'] as List);
initialOrga = List<String>.from(widget.events['organizers'] as List); initialOrga = List<String>.from(widget.events['organizers'] as List);
} }