Merge pull request 'feature/upload-image' (#8) from feature/upload-image into main

Reviewed-on: #8
This commit is contained in:
v4l3n71n 2024-09-17 23:51:42 +02:00
commit 5e0d3ab72e
2 changed files with 122 additions and 76 deletions

View File

@ -69,6 +69,7 @@ class _ItemMenuState extends State<ItemMenu> with ShowErrorDialog {
String eventStartDate = ""; String eventStartDate = "";
String organizers = ""; String organizers = "";
String place = ""; String place = "";
String imgUrl = "";
Events? events; Events? events;
@override @override
@ -85,6 +86,7 @@ class _ItemMenuState extends State<ItemMenu> with ShowErrorDialog {
String formerDate = ""; String formerDate = "";
String formerOrga = ""; String formerOrga = "";
String formerMap = ""; String formerMap = "";
String formerImage = "";
if (accessToken.isNotEmpty) { if (accessToken.isNotEmpty) {
var urlGet = Uri.parse("${globals.api}/events/${widget.title}"); var urlGet = Uri.parse("${globals.api}/events/${widget.title}");
@ -102,8 +104,13 @@ class _ItemMenuState extends State<ItemMenu> with ShowErrorDialog {
final time = DateFormat.Hm().format(startDate); final time = DateFormat.Hm().format(startDate);
final endDate = DateTime.parse(events["end_date"]); final endDate = DateTime.parse(events["end_date"]);
final dateE = DateFormat.yMd().format(endDate); final dateE = DateFormat.yMd().format(endDate);
final timeE = DateFormat.Hm().format(endDate); final timeE = DateFormat.Hm().format(endDate);
if (events["imgUrl"] != null) {
formerImage = events["imgUrl"];
}
formerDate = "${date} ${time} à ${dateE} ${timeE}"; formerDate = "${date} ${time} à ${dateE} ${timeE}";
if (events["organizers"].length > 1) { if (events["organizers"].length > 1) {
formerOrga = "${events['organizers'][0]}"; formerOrga = "${events['organizers'][0]}";
@ -168,6 +175,7 @@ class _ItemMenuState extends State<ItemMenu> with ShowErrorDialog {
eventStartDate = formerDate; eventStartDate = formerDate;
organizers = formerOrga; organizers = formerOrga;
place = formerMap; place = formerMap;
imgUrl = formerImage;
}); });
} }
@ -201,8 +209,7 @@ class _ItemMenuState extends State<ItemMenu> with ShowErrorDialog {
Padding( Padding(
padding: const EdgeInsets.only(top: 60.0), padding: const EdgeInsets.only(top: 60.0),
child: Center( child: Center(
child: Container( child: Container(height: 250, child: Image.network(imgUrl)),
height: 250, child: Image.asset('images/flutter.png')),
), ),
), ),
Row( Row(

View File

@ -2,9 +2,11 @@ import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
import 'package:http/http.dart' as http; import 'package:http/http.dart' as http;
import 'package:intl/intl.dart'; import 'package:intl/intl.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'dart:convert'; import 'dart:convert';
import 'dart:io'; import 'dart:io';
import 'dart:typed_data';
import '../classes/alert.dart'; import '../classes/alert.dart';
import '../classes/eventAdded.dart'; import '../classes/eventAdded.dart';
@ -86,6 +88,13 @@ class _UpdateeventImageState extends State<UpdateeventImage>
} }
} }
convertNulltoEmptyString(var check) {
if (check == null) {
return "";
}
return check;
}
String formatDate(String date) { String formatDate(String date) {
var splitedDate = date.split("-"); var splitedDate = date.split("-");
var day = splitedDate[0]; var day = splitedDate[0];
@ -116,6 +125,30 @@ class _UpdateeventImageState extends State<UpdateeventImage>
List<String> send = ["toto"]; List<String> send = ["toto"];
if (accessToken.isNotEmpty) { if (accessToken.isNotEmpty) {
try { try {
await dotenv.load();
final params = {
'expiration': '15552000',
'key': dotenv.env["IMGBB_API_KEY"],
};
print("Post Img");
final urlPost = Uri.parse('https://api.imgbb.com/1/upload')
.replace(queryParameters: params);
File image = File(widget.imagePath);
Uint8List _bytes = await image.readAsBytes();
String _base64String = base64.encode(_bytes);
final req = http.MultipartRequest('POST', urlPost)
..fields['image'] = _base64String;
final stream = await req.send();
final res = await http.Response.fromStream(stream);
final status = res.statusCode;
print("code status imgbb ${status}");
if (status == 200) {
var body = json.decode(utf8.decode(res.bodyBytes));
String imgUrl = body["data"]["url"];
//String credentials = "${pseudo}:${password}"; //String credentials = "${pseudo}:${password}";
//Codec<String, String> stringToBase64 = utf8.fuse(base64); //Codec<String, String> stringToBase64 = utf8.fuse(base64);
//String encoded = stringToBase64.encode(credentials); //String encoded = stringToBase64.encode(credentials);
@ -137,7 +170,8 @@ class _UpdateeventImageState extends State<UpdateeventImage>
'organizers': send, 'organizers': send,
'latitude': '0.0', 'latitude': '0.0',
'longitude': '0.0', 'longitude': '0.0',
'description': description 'description': description,
"imgUrl": imgUrl
})); }));
print(responsePut.statusCode); print(responsePut.statusCode);
if ((responsePut.statusCode == 200) || if ((responsePut.statusCode == 200) ||
@ -184,6 +218,9 @@ class _UpdateeventImageState extends State<UpdateeventImage>
} }
showErrorDialog(context, text); showErrorDialog(context, text);
} }
} else {
print("imgbb error : ${status}");
}
} catch (e) { } catch (e) {
showErrorDialog(context, "${e}"); showErrorDialog(context, "${e}");
} }
@ -193,15 +230,17 @@ class _UpdateeventImageState extends State<UpdateeventImage>
} }
void start() async { void start() async {
inputName.text = widget.events["name"]; inputName.text = convertNulltoEmptyString(widget.events["name"]);
inputCity.text = widget.events["city"]; inputCity.text = convertNulltoEmptyString(widget.events["city"]);
inputAddress.text = widget.events["address"]; inputAddress.text = convertNulltoEmptyString(widget.events["address"]);
inputZipCode.text = widget.events["zip_code"]; inputZipCode.text = convertNulltoEmptyString(widget.events["zip_code"]);
inputCountry.text = widget.events["country"]; inputCountry.text = convertNulltoEmptyString(widget.events["country"]);
inputDesc.text = widget.events["description"]; inputDesc.text = convertNulltoEmptyString(widget.events["description"]);
DateTime pickedStartDate = DateTime.parse(widget.events["start_date"]); DateTime pickedStartDate =
DateTime pickedEndDate = DateTime.parse(widget.events["end_date"]); DateTime.parse(convertNulltoEmptyString(widget.events["start_date"]));
DateTime pickedEndDate =
DateTime.parse(convertNulltoEmptyString(widget.events["end_date"]));
startDatepicker.text = DateFormat("dd-MM-yyyy").format(pickedStartDate); startDatepicker.text = DateFormat("dd-MM-yyyy").format(pickedStartDate);
endDatepicker.text = DateFormat("dd-MM-yyyy").format(pickedEndDate); endDatepicker.text = DateFormat("dd-MM-yyyy").format(pickedEndDate);