import 'package:flutter/material.dart'; import 'package:flutter/widgets.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:http/http.dart' as http; import 'package:intl/intl.dart'; import 'dart:convert'; import 'dart:io'; //import 'MyHomePage.dart'; import 'ListItemMenu.dart'; import '../classes/alert.dart'; import '../classes/eventAdded.dart'; import '../variable/globals.dart' as globals; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { Map events = {}; @override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, home: UpdateeventImage(events: events), ); } } class UpdateeventImage extends StatefulWidget { const UpdateeventImage({Key? key, required this.events}) : super(key: key); final Map events; @override _UpdateeventImageState createState() => _UpdateeventImageState(); } class _UpdateeventImageState extends State with ShowErrorDialog, ShowEventDialog { TextEditingController inputName = TextEditingController(); TextEditingController inputPlace = TextEditingController(); TextEditingController inputDate = TextEditingController(); TextEditingController startDatepicker = TextEditingController(); onTapFunctionDatePicker({required BuildContext context}) async { DateTime? pickedDate = await showDatePicker( context: context, firstDate: DateTime.now(), initialDate: DateTime.now(), lastDate: DateTime(2026)); if (pickedDate == null) return; startDatepicker.text = DateFormat("dd-MM-yyyy").format(pickedDate); } Future _updateEvent(BuildContext context) async { var url = Uri.parse("${globals.api}/token"); var name = inputName.text; var place = inputPlace.text; SharedPreferences prefs = await SharedPreferences.getInstance(); var accessToken = prefs.getString("access_token") ?? ""; List send = ["toto"]; if (accessToken.isNotEmpty) { try { //String credentials = "${pseudo}:${password}"; //Codec 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': widget.events["date"], 'end_date': widget.events['date'], 'organizers': send, 'latitude': '0.0', 'longitude': '0.0', })); 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); } } catch (e) { showErrorDialog(context, "${e}"); } } else { showErrorDialog(context, "Champ vide"); } } void start() async { inputName.text = widget.events["name"]; inputPlace.text = widget.events["place"]; } @override void initState() { start(); super.initState(); } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Colors.white, appBar: AppBar( title: Text("Login Page"), backgroundColor: Colors.blue, foregroundColor: Colors.white, ), body: SingleChildScrollView( child: Column( children: [ Padding( padding: const EdgeInsets.only(top: 60.0), child: Center( child: Container( width: 200, height: 150, decoration: BoxDecoration( color: Colors.red, borderRadius: BorderRadius.circular(50.0)), child: Image.asset('./images/flutter.png')), ), ), Padding( //padding: const EdgeInsets.only(left:15.0,right: 15.0,top:0,bottom: 0), padding: EdgeInsets.symmetric(horizontal: 15), child: TextField( controller: inputName, decoration: InputDecoration( border: OutlineInputBorder(), labelText: 'Nom', hintText: 'Modifier le nom de l\'évènement'), ), ), Padding( padding: const EdgeInsets.only( left: 15.0, right: 15.0, top: 15, bottom: 0), //padding: EdgeInsets.symmetric(horizontal: 15), child: TextField( controller: inputPlace, decoration: InputDecoration( border: OutlineInputBorder(), labelText: 'Lieu', hintText: 'Entrer le lieu'), ), ), 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(), labelText: 'Date de debut de l\'évènement', hintText: 'Cliquez ici pour selectionner une date'), onTap: () => onTapFunctionDatePicker(context: context)), ), SizedBox( height: 30, ), Container( height: 50, width: 250, decoration: BoxDecoration( color: Colors.blue, borderRadius: BorderRadius.circular(20)), child: TextButton( onPressed: () { _updateEvent(context); }, child: Text( 'Ajouter', style: TextStyle(color: Colors.white, fontSize: 25), ), ), ) ], ), ), ); } }