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 'package:flutter_dotenv/flutter_dotenv.dart'; import 'package:google_places_autocomplete_text_field/google_places_autocomplete_text_field.dart'; import 'package:google_places_autocomplete_text_field/model/prediction.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(); TextEditingController startTimepicker = TextEditingController(); TextEditingController endDatepicker = TextEditingController(); TextEditingController endTimepicker = TextEditingController(); String GOOGLEMAP_API_KEY = ""; onTapFunctionDatePicker( {required BuildContext context, required String position}) async { DateTime? pickedDate = await showDatePicker( context: context, firstDate: DateTime.parse(widget.events["date"]), initialDate: DateTime.parse(widget.events["date"]), lastDate: DateTime(2104)); if (pickedDate == null) return; if (position == "start") { startDatepicker.text = DateFormat("dd-MM-yyyy").format(pickedDate); } if (position == "end") { endDatepicker.text = DateFormat("dd-MM-yyyy").format(pickedDate); } } onTapFunctionTimePicker( {required BuildContext context, required String position}) async { TimeOfDay? pickedDate = await showTimePicker( context: context, initialTime: TimeOfDay.fromDateTime(DateTime.parse(widget.events["date"]))); if (pickedDate == null) return; if (position == "start") { startTimepicker.text = pickedDate.format(context); } if (position == "end") { endTimepicker.text = pickedDate.format(context); } } 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 { await dotenv.load(); GOOGLEMAP_API_KEY = dotenv.env['GOOGLEMAP_API_KEY']!; inputName.text = widget.events["name"]; inputPlace.text = widget.events["place"]; print("date start : ${widget.events["date"]}"); DateTime pickedDate = DateTime.parse(widget.events["date"]); startDatepicker.text = DateFormat("dd-MM-yyyy").format(pickedDate); endDatepicker.text = DateFormat("dd-MM-yyyy").format(pickedDate); startTimepicker.text = DateFormat("HH-mm").format(pickedDate); endTimepicker.text = DateFormat("HH-mm").format(pickedDate); } @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: GooglePlacesAutoCompleteTextFormField( textEditingController: inputPlace, googleAPIKey: GOOGLEMAP_API_KEY, decoration: const InputDecoration( hintText: 'Enter your address', labelText: 'Lieu', labelStyle: TextStyle(color: Colors.purple), border: OutlineInputBorder(), ), validator: (value) { if (value!.isEmpty) { return 'Please enter some text'; } return null; }, // proxyURL: _yourProxyURL, maxLines: 1, overlayContainer: (child) => Material( elevation: 1.0, color: Colors.green, borderRadius: BorderRadius.circular(12), child: child, ), getPlaceDetailWithLatLng: (prediction) { print('placeDetails${prediction.lng}'); }, itmClick: (Prediction prediction) => inputPlace.text = prediction.description!, ), ), 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, position: "start")), ), 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(), labelText: 'Heure de debut de l\'évènement', hintText: 'Cliquez ici pour selectionner une heure'), onTap: () => onTapFunctionTimePicker( context: context, position: "start")), ), 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(), labelText: 'Date de fin de l\'évènement', hintText: 'Cliquez ici pour selectionner une date'), onTap: () => onTapFunctionDatePicker( context: context, position: "end")), ), 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(), labelText: 'Heure de fin de l\'évènement', hintText: 'Cliquez ici pour selectionner une heure'), onTap: () => onTapFunctionTimePicker( context: context, position: "end")), ), 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), ), ), ) ], ), ), ); } }