mobile-flutter/covas_mobile/lib/pages/UpdateEventImage.dart

634 lines
26 KiB
Dart
Raw Normal View History

2024-07-28 21:54:37 +02:00
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:http/http.dart' as http;
2024-08-14 22:55:41 +02:00
import 'package:intl/intl.dart';
2024-09-17 23:50:23 +02:00
import 'package:flutter_dotenv/flutter_dotenv.dart';
2024-10-03 23:54:54 +02:00
import 'package:textfield_tags/textfield_tags.dart';
2024-07-28 21:54:37 +02:00
import 'dart:convert';
import 'dart:io';
2024-09-12 23:54:59 +02:00
import 'dart:typed_data';
2024-07-28 21:54:37 +02:00
import '../classes/alert.dart';
2024-07-30 23:46:15 +02:00
import '../classes/eventAdded.dart';
2024-07-28 21:54:37 +02:00
import '../variable/globals.dart' as globals;
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
2024-07-30 23:19:37 +02:00
Map<String, dynamic> events = {};
2024-09-01 22:27:18 +02:00
String imagePath = "";
2024-07-28 21:54:37 +02:00
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
2024-09-01 22:27:18 +02:00
home: UpdateeventImage(events: events, imagePath: imagePath),
2024-07-28 21:54:37 +02:00
);
}
}
class UpdateeventImage extends StatefulWidget {
2024-09-01 22:27:18 +02:00
const UpdateeventImage(
{Key? key, required this.events, required this.imagePath})
: super(key: key);
2024-07-30 23:19:37 +02:00
final Map<String, dynamic> events;
2024-09-01 22:27:18 +02:00
final String imagePath;
2024-07-30 23:19:37 +02:00
2024-07-28 21:54:37 +02:00
@override
_UpdateeventImageState createState() => _UpdateeventImageState();
}
class _UpdateeventImageState extends State<UpdateeventImage>
2024-07-30 23:46:15 +02:00
with ShowErrorDialog, ShowEventDialog {
2024-07-30 23:19:37 +02:00
TextEditingController inputName = TextEditingController();
2024-08-30 23:57:10 +02:00
TextEditingController inputAddress = TextEditingController();
TextEditingController inputZipCode = TextEditingController();
TextEditingController inputCity = TextEditingController();
TextEditingController inputCountry = TextEditingController();
2024-07-30 23:19:37 +02:00
TextEditingController inputDate = TextEditingController();
2024-09-01 22:57:16 +02:00
TextEditingController inputDesc = TextEditingController();
2024-08-14 22:55:41 +02:00
TextEditingController startDatepicker = TextEditingController();
2024-08-14 23:15:30 +02:00
TextEditingController startTimepicker = TextEditingController();
2024-08-14 23:20:31 +02:00
TextEditingController endDatepicker = TextEditingController();
TextEditingController endTimepicker = TextEditingController();
2024-10-03 23:54:54 +02:00
final _stringTagController = StringTagController();
2024-10-14 17:49:39 +02:00
List<String> initialTags = [];
2024-08-14 22:55:41 +02:00
2024-10-15 23:57:22 +02:00
final _stringOrgaController = StringTagController();
List<String> initialOrga = [];
2024-08-14 23:34:42 +02:00
onTapFunctionDatePicker(
{required BuildContext context, required String position}) async {
2024-08-14 22:55:41 +02:00
DateTime? pickedDate = await showDatePicker(
context: context,
2024-08-19 23:31:00 +02:00
firstDate: DateTime.parse(widget.events["date"]),
initialDate: DateTime.parse(widget.events["date"]),
2024-08-14 23:52:06 +02:00
lastDate: DateTime(2104));
2024-08-14 22:55:41 +02:00
if (pickedDate == null) return;
2024-08-14 23:34:42 +02:00
if (position == "start") {
startDatepicker.text = DateFormat("dd-MM-yyyy").format(pickedDate);
}
if (position == "end") {
endDatepicker.text = DateFormat("dd-MM-yyyy").format(pickedDate);
}
2024-08-14 22:55:41 +02:00
}
2024-07-30 23:19:37 +02:00
2024-08-14 23:34:42 +02:00
onTapFunctionTimePicker(
{required BuildContext context, required String position}) async {
2024-08-14 23:52:06 +02:00
TimeOfDay? pickedDate = await showTimePicker(
context: context,
2024-08-19 23:31:00 +02:00
initialTime:
TimeOfDay.fromDateTime(DateTime.parse(widget.events["date"])));
2024-08-14 23:15:30 +02:00
if (pickedDate == null) return;
2024-08-14 23:34:42 +02:00
if (position == "start") {
startTimepicker.text = pickedDate.format(context);
}
if (position == "end") {
endTimepicker.text = pickedDate.format(context);
}
2024-08-14 23:15:30 +02:00
}
2024-09-17 23:50:23 +02:00
convertNulltoEmptyString(var check) {
if (check == null) {
return "";
}
return check;
}
2024-10-14 17:49:39 +02:00
convertNulltoArray(List<String> check) {
if (check == null) {
return [];
}
return check;
}
2024-09-03 15:54:37 +02:00
String formatDate(String date) {
var splitedDate = date.split("-");
var day = splitedDate[0];
var month = splitedDate[1];
var year = splitedDate[2];
return "${year}-${month}-${day}";
}
2024-07-30 23:46:15 +02:00
Future<void> _updateEvent(BuildContext context) async {
2024-07-28 21:54:37 +02:00
var url = Uri.parse("${globals.api}/token");
2024-07-30 23:46:15 +02:00
var name = inputName.text;
2024-09-03 15:54:37 +02:00
var place = inputAddress.text;
var city = inputCity.text;
var country = inputCountry.text;
var zipCode = inputZipCode.text;
var description = inputDesc.text;
2024-10-15 23:44:41 +02:00
List<String> tags = List<String>.from(_stringTagController.getTags as List);
2024-09-03 15:54:37 +02:00
var startDateFormat = formatDate(startDatepicker.text);
var endDateFormat = formatDate(endDatepicker.text);
var startDate =
"${startDateFormat}T${startTimepicker.text.replaceAll('-', ':')}";
var endDate = "${endDateFormat}T${endTimepicker.text.replaceAll('-', ':')}";
print("start date : ${startDate}");
print("end date : ${endDate}");
2024-07-30 23:46:15 +02:00
SharedPreferences prefs = await SharedPreferences.getInstance();
var accessToken = prefs.getString("access_token") ?? "";
List<String> send = ["toto"];
if (accessToken.isNotEmpty) {
2024-07-28 21:54:37 +02:00
try {
2024-09-17 23:50:23 +02:00
await dotenv.load();
2024-09-12 23:54:59 +02:00
final params = {
2024-09-17 23:50:23 +02:00
'expiration': '15552000',
'key': dotenv.env["IMGBB_API_KEY"],
2024-09-12 23:54:59 +02:00
};
2024-09-17 23:09:35 +02:00
print("Post Img");
2024-09-12 23:54:59 +02:00
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);
2024-09-17 23:09:35 +02:00
final req = http.MultipartRequest('POST', urlPost)
2024-09-12 23:54:59 +02:00
..fields['image'] = _base64String;
final stream = await req.send();
final res = await http.Response.fromStream(stream);
final status = res.statusCode;
2024-09-17 23:09:35 +02:00
print("code status imgbb ${status}");
2024-09-12 23:54:59 +02:00
if (status == 200) {
2024-09-17 23:09:35 +02:00
var body = json.decode(utf8.decode(res.bodyBytes));
String imgUrl = body["data"]["url"];
2024-09-12 23:54:59 +02:00
//String credentials = "${pseudo}:${password}";
//Codec<String, String> stringToBase64 = utf8.fuse(base64);
//String encoded = stringToBase64.encode(credentials);
var urlPut = Uri.parse("${globals.api}/events");
var responsePut = await http.put(urlPut,
headers: {
HttpHeaders.cookieHeader: 'access_token=${accessToken}',
HttpHeaders.acceptHeader: 'application/json, text/plain, */*',
HttpHeaders.contentTypeHeader: 'application/json'
},
body: jsonEncode({
'name': name,
'place': place,
'start_date': startDate,
'end_date': endDate,
'zip_code': zipCode,
'country': country,
'city': city,
'organizers': send,
'latitude': '0.0',
'longitude': '0.0',
2024-09-17 23:09:35 +02:00
'description': description,
2024-10-15 23:44:41 +02:00
"imgUrl": imgUrl,
"tags": tags
2024-09-12 23:54:59 +02:00
}));
print(responsePut.statusCode);
if ((responsePut.statusCode == 200) ||
(responsePut.statusCode == 201)) {
showEventDialog(context, "Evenement ${name} ajoute");
} else {
var text = "";
switch (responsePut.statusCode) {
case 400:
{
text = "Requête mal construite";
}
break;
case 406:
{
text = "Mot de passe incorrect";
}
break;
case 404:
{
text = "Utilisateur inconnu";
}
break;
case 403:
{
text = "Utilisateur desactive";
}
break;
case 410:
{
text = "Token invalide";
}
break;
case 500:
{
text = "Probleme interne du serveur";
}
break;
default:
{
text = "Probleme d'authentification inconnu";
}
break;
}
showErrorDialog(context, text);
2024-07-28 21:54:37 +02:00
}
2024-09-12 23:54:59 +02:00
} else {
print("imgbb error : ${status}");
2024-07-28 21:54:37 +02:00
}
} catch (e) {
showErrorDialog(context, "${e}");
}
} else {
showErrorDialog(context, "Champ vide");
}
}
void start() async {
2024-09-17 23:50:23 +02:00
inputName.text = convertNulltoEmptyString(widget.events["name"]);
inputCity.text = convertNulltoEmptyString(widget.events["city"]);
inputAddress.text = convertNulltoEmptyString(widget.events["address"]);
inputZipCode.text = convertNulltoEmptyString(widget.events["zip_code"]);
inputCountry.text = convertNulltoEmptyString(widget.events["country"]);
inputDesc.text = convertNulltoEmptyString(widget.events["description"]);
DateTime pickedStartDate =
DateTime.parse(convertNulltoEmptyString(widget.events["start_date"]));
DateTime pickedEndDate =
DateTime.parse(convertNulltoEmptyString(widget.events["end_date"]));
2024-09-02 22:25:46 +02:00
startDatepicker.text = DateFormat("dd-MM-yyyy").format(pickedStartDate);
endDatepicker.text = DateFormat("dd-MM-yyyy").format(pickedEndDate);
startTimepicker.text = DateFormat("HH-mm").format(pickedStartDate);
endTimepicker.text = DateFormat("HH-mm").format(pickedEndDate);
2024-10-14 17:49:39 +02:00
initialTags = List<String>.from(widget.events['tags'] as List);
2024-10-15 23:57:22 +02:00
initialOrga = List<String>.from(widget.events['organizers'] as List);
2024-07-28 21:54:37 +02:00
}
@override
void initState() {
start();
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
2024-09-02 22:25:46 +02:00
title: Text("Add or Update a event"),
2024-07-28 21:54:37 +02:00
backgroundColor: Colors.blue,
foregroundColor: Colors.white,
),
body: SingleChildScrollView(
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(top: 60.0),
child: Center(
child: Container(
width: 200,
height: 150,
decoration: BoxDecoration(
2024-09-01 23:03:18 +02:00
borderRadius: BorderRadius.circular(100.0)),
2024-09-01 22:27:18 +02:00
child: Image.file(File(widget.imagePath))),
2024-07-28 21:54:37 +02:00
),
),
Padding(
//padding: const EdgeInsets.only(left:15.0,right: 15.0,top:0,bottom: 0),
padding: EdgeInsets.symmetric(horizontal: 15),
child: TextField(
2024-07-30 23:19:37 +02:00
controller: inputName,
2024-07-28 21:54:37 +02:00
decoration: InputDecoration(
border: OutlineInputBorder(),
2024-07-30 23:19:37 +02:00
labelText: 'Nom',
hintText: 'Modifier le nom de l\'évènement'),
2024-07-28 21:54:37 +02:00
),
),
2024-08-30 23:27:34 +02:00
Padding(
2024-07-28 21:54:37 +02:00
padding: const EdgeInsets.only(
left: 15.0, right: 15.0, top: 15, bottom: 0),
//padding: EdgeInsets.symmetric(horizontal: 15),
child: TextField(
2024-08-30 23:57:10 +02:00
controller: inputAddress,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Adresse',
hintText: 'Entrer une adresse'),
),
),
Padding(
padding: const EdgeInsets.only(
left: 15.0, right: 15.0, top: 15, bottom: 0),
//padding: EdgeInsets.symmetric(horizontal: 15),
child: TextField(
controller: inputZipCode,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Code postal',
hintText: 'Entrer un code postal'),
),
),
Padding(
padding: const EdgeInsets.only(
left: 15.0, right: 15.0, top: 15, bottom: 0),
//padding: EdgeInsets.symmetric(horizontal: 15),
child: TextField(
controller: inputCity,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Ville',
hintText: 'Entrer une ville'),
),
),
Padding(
padding: const EdgeInsets.only(
left: 15.0, right: 15.0, top: 15, bottom: 0),
//padding: EdgeInsets.symmetric(horizontal: 15),
child: TextField(
controller: inputCountry,
2024-07-28 21:54:37 +02:00
decoration: InputDecoration(
border: OutlineInputBorder(),
2024-08-30 23:57:10 +02:00
labelText: 'Pays',
hintText: 'Entrer un pays'),
2024-07-28 21:54:37 +02:00
),
2024-08-21 23:46:19 +02:00
),
2024-08-14 22:55:41 +02:00
Padding(
padding: const EdgeInsets.only(
left: 15.0, right: 15.0, top: 15, bottom: 0),
//padding: EdgeInsets.symmetric(horizontal: 15),
child: TextField(
controller: startDatepicker,
readOnly: true,
decoration: InputDecoration(
border: OutlineInputBorder(),
2024-10-18 21:28:46 +02:00
labelText: 'Date de debut',
2024-08-14 22:55:41 +02:00
hintText: 'Cliquez ici pour selectionner une date'),
2024-08-14 23:34:42 +02:00
onTap: () => onTapFunctionDatePicker(
context: context, position: "start")),
2024-08-14 22:55:41 +02:00
),
2024-08-14 23:15:30 +02:00
Padding(
padding: const EdgeInsets.only(
left: 15.0, right: 15.0, top: 15, bottom: 0),
//padding: EdgeInsets.symmetric(horizontal: 15),
child: TextField(
controller: startTimepicker,
readOnly: true,
decoration: InputDecoration(
border: OutlineInputBorder(),
2024-10-18 21:28:46 +02:00
labelText: 'Heure de debut',
2024-08-14 23:15:30 +02:00
hintText: 'Cliquez ici pour selectionner une heure'),
2024-08-14 23:34:42 +02:00
onTap: () => onTapFunctionTimePicker(
context: context, position: "start")),
2024-08-14 23:15:30 +02:00
),
2024-08-14 23:20:31 +02:00
Padding(
padding: const EdgeInsets.only(
left: 15.0, right: 15.0, top: 15, bottom: 0),
//padding: EdgeInsets.symmetric(horizontal: 15),
child: TextField(
controller: endDatepicker,
readOnly: true,
decoration: InputDecoration(
border: OutlineInputBorder(),
2024-10-18 21:28:46 +02:00
labelText: 'Date de fin',
2024-08-14 23:20:31 +02:00
hintText: 'Cliquez ici pour selectionner une date'),
2024-08-14 23:34:42 +02:00
onTap: () => onTapFunctionDatePicker(
context: context, position: "end")),
2024-08-14 23:20:31 +02:00
),
Padding(
padding: const EdgeInsets.only(
left: 15.0, right: 15.0, top: 15, bottom: 0),
//padding: EdgeInsets.symmetric(horizontal: 15),
child: TextField(
controller: endTimepicker,
readOnly: true,
decoration: InputDecoration(
border: OutlineInputBorder(),
2024-10-18 21:28:46 +02:00
labelText: 'Heure de fin',
2024-08-14 23:20:31 +02:00
hintText: 'Cliquez ici pour selectionner une heure'),
2024-08-14 23:34:42 +02:00
onTap: () => onTapFunctionTimePicker(
context: context, position: "end")),
2024-08-14 23:20:31 +02:00
),
2024-10-07 23:59:15 +02:00
TextFieldTags<String>(
textfieldTagsController: _stringTagController,
2024-10-14 17:49:39 +02:00
initialTags: initialTags,
2024-10-07 23:59:15 +02:00
textSeparators: const [' ', ','],
validator: (String tag) {
2024-10-09 23:28:24 +02:00
if (_stringTagController.getTags!.contains(tag)) {
return 'Tu as deja rentre ce tag';
2024-10-07 23:59:15 +02:00
}
return null;
},
inputFieldBuilder: (context, inputFieldValues) {
return Padding(
2024-10-09 23:28:24 +02:00
padding: const EdgeInsets.only(
left: 15.0, right: 15.0, top: 15, bottom: 0),
child: TextField(
controller: inputFieldValues.textEditingController,
focusNode: inputFieldValues.focusNode,
onChanged: inputFieldValues.onTagChanged,
onSubmitted: inputFieldValues.onTagSubmitted,
decoration: InputDecoration(
border: OutlineInputBorder(),
2024-10-18 21:28:46 +02:00
labelText: 'Tags',
2024-10-09 23:28:24 +02:00
hintText: inputFieldValues.tags.isNotEmpty
? ''
: "Enter tag...",
errorText: inputFieldValues.error,
prefixIcon: inputFieldValues.tags.isNotEmpty
? SingleChildScrollView(
controller:
inputFieldValues.tagScrollController,
scrollDirection: Axis.vertical,
child: Padding(
padding: const EdgeInsets.only(
top: 8,
bottom: 8,
left: 8,
),
child: Wrap(
runSpacing: 4.0,
spacing: 4.0,
children: inputFieldValues.tags
.map((String tag) {
return Container(
decoration: const BoxDecoration(
borderRadius: BorderRadius.all(
Radius.circular(20.0),
),
color: Colors.blue,
),
margin: const EdgeInsets.symmetric(
horizontal: 5.0),
padding: const EdgeInsets.symmetric(
horizontal: 10.0, vertical: 5.0),
child: Row(
mainAxisAlignment:
MainAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
InkWell(
child: Text(
'#$tag',
style: const TextStyle(
color: Colors.white),
),
onTap: () {
//print("$tag selected");
},
),
const SizedBox(width: 4.0),
InkWell(
child: const Icon(
Icons.cancel,
size: 14.0,
color: Color.fromARGB(
2024-10-15 23:57:22 +02:00
255, 233, 233, 233),
),
onTap: () {
inputFieldValues
.onTagRemoved(tag);
},
)
],
),
);
}).toList()),
),
)
: null,
),
),
);
}),
TextFieldTags<String>(
textfieldTagsController: _stringOrgaController,
initialTags: initialOrga,
textSeparators: const [','],
validator: (String tag) {
if (_stringOrgaController.getTags!.contains(tag)) {
return 'Cet organisateur est déjà rentré';
}
return null;
},
inputFieldBuilder: (context, inputFieldValues) {
return Padding(
padding: const EdgeInsets.only(
left: 15.0, right: 15.0, top: 15, bottom: 0),
child: TextField(
controller: inputFieldValues.textEditingController,
focusNode: inputFieldValues.focusNode,
onChanged: inputFieldValues.onTagChanged,
onSubmitted: inputFieldValues.onTagSubmitted,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Organisateurs',
hintText: inputFieldValues.tags.isNotEmpty
? ''
: "Enter un organisateur...",
errorText: inputFieldValues.error,
prefixIcon: inputFieldValues.tags.isNotEmpty
? SingleChildScrollView(
controller:
inputFieldValues.tagScrollController,
scrollDirection: Axis.vertical,
child: Padding(
padding: const EdgeInsets.only(
top: 8,
bottom: 8,
left: 8,
),
child: Wrap(
runSpacing: 4.0,
spacing: 4.0,
children: inputFieldValues.tags
.map((String tag) {
return Container(
decoration: const BoxDecoration(
borderRadius: BorderRadius.all(
Radius.circular(20.0),
),
color: Colors.blue,
),
margin: const EdgeInsets.symmetric(
horizontal: 5.0),
padding: const EdgeInsets.symmetric(
horizontal: 10.0, vertical: 5.0),
child: Row(
mainAxisAlignment:
MainAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
InkWell(
child: Text(
'$tag',
style: const TextStyle(
color: Colors.white),
),
onTap: () {
//print("$tag selected");
},
),
const SizedBox(width: 4.0),
InkWell(
child: const Icon(
Icons.cancel,
size: 14.0,
color: Color.fromARGB(
2024-10-09 23:28:24 +02:00
255, 233, 233, 233),
),
onTap: () {
inputFieldValues
.onTagRemoved(tag);
},
)
],
),
);
}).toList()),
),
)
: null,
),
),
);
2024-10-07 23:59:15 +02:00
}),
2024-09-01 22:57:16 +02:00
Padding(
padding: const EdgeInsets.only(
left: 15.0, right: 15.0, top: 15, bottom: 0),
//padding: EdgeInsets.symmetric(horizontal: 15),
child: TextField(
controller: inputDesc,
keyboardType: TextInputType.multiline,
maxLines: 10,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Description',
hintText: 'Décrire l\'evènement'),
),
),
2024-07-30 23:19:37 +02:00
SizedBox(
height: 30,
2024-07-28 21:54:37 +02:00
),
Container(
height: 50,
width: 250,
decoration: BoxDecoration(
color: Colors.blue, borderRadius: BorderRadius.circular(20)),
child: TextButton(
onPressed: () {
2024-07-30 23:46:15 +02:00
_updateEvent(context);
2024-07-28 21:54:37 +02:00
},
child: Text(
2024-07-30 23:19:37 +02:00
'Ajouter',
2024-07-28 21:54:37 +02:00
style: TextStyle(color: Colors.white, fontSize: 25),
),
),
2024-07-30 23:19:37 +02:00
)
2024-07-28 21:54:37 +02:00
],
),
),
);
}
}