import 'package:covas_mobile/pages/CameraEdit.dart'; import 'package:flutter/material.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:textfield_tags/textfield_tags.dart'; import 'dart:convert'; import 'dart:io'; import 'dart:typed_data'; import '../classes/events.dart'; import '../classes/alert.dart'; import '../classes/eventAdded.dart'; import '../variable/globals.dart' as globals; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { Events? events; @override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, home: EditProfile(), ); } } class EditProfile extends StatefulWidget { const EditProfile({super.key}); @override _EditProfileState createState() => _EditProfileState(); } class _EditProfileState extends State with ShowAlertDialog, ShowEventDialog { TextEditingController inputUserName = TextEditingController(); TextEditingController inputName = TextEditingController(); TextEditingController inputFirstName = TextEditingController(); TextEditingController inputEmail = TextEditingController(); TextEditingController inputBirth = TextEditingController(); TextEditingController inputPassword = TextEditingController(); onTapFunctionDatePicker({required BuildContext context}) async { DateTime initialDate = DateTime.parse(formatDate(inputBirth.text)); DateTime? pickedDate = await showDatePicker( context: context, firstDate: DateTime(1900), initialDate: initialDate, lastDate: DateTime(2104)); if (pickedDate == null) return; inputBirth.text = DateFormat("dd/MM/yyyy").format(pickedDate); } convertNulltoEmptyString(var check) { if (check == null) { return ""; } return check; } convertNulltoArray(List check) { if (check == null) { return []; } return check; } String formatDate(String date) { var splitedDate = date.split("/"); var day = splitedDate[0]; var month = splitedDate[1]; var year = splitedDate[2]; return "${year}-${month}-${day}"; } Future _updateProfile(BuildContext context) async { SharedPreferences prefs = await SharedPreferences.getInstance(); var accessToken = prefs.getString("access_token") ?? ""; var username = inputUserName.text; var firstName = inputFirstName.text; var name = inputName.text; var email = inputEmail.text; var password = inputPassword.text; var birth = DateTime.parse(formatDate(inputBirth.text)); var urlPut = Uri.parse("${globals.api}/users/me"); 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, 'username': username, 'firstName': firstName, 'password': password, 'email': email, 'birth': birth })); print(responsePut.statusCode); if (responsePut.statusCode == 200) { showEventDialog(context, " Votre utiliseteur modifie"); } 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; } showAlertDialog(context, "Erreur serveur", text); } } @override void initState() { super.initState(); } final _formKey = GlobalKey(); String? _validateField(String? value) { return value!.isEmpty ? 'Champ requis' : null; } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Colors.white, appBar: AppBar( title: Text("Add or Update a event"), backgroundColor: Colors.blue, foregroundColor: Colors.white, ), body: Form( key: _formKey, child: SingleChildScrollView( child: Column( children: [ Padding( //padding: const EdgeInsets.only(left:15.0,right: 15.0,top:0,bottom: 0), padding: EdgeInsets.symmetric(horizontal: 15), child: TextFormField( controller: inputUserName, validator: (value) => _validateField(value), decoration: InputDecoration( border: OutlineInputBorder(), labelText: 'Pseudo', hintText: 'Modifier le pseudo'), ), ), Padding( //padding: const EdgeInsets.only(left:15.0,right: 15.0,top:0,bottom: 0), padding: EdgeInsets.symmetric(horizontal: 15), child: TextFormField( controller: inputName, validator: (value) => _validateField(value), decoration: InputDecoration( border: OutlineInputBorder(), labelText: 'Nom', hintText: 'Modifier le nom'), ), ), Padding( //padding: const EdgeInsets.only(left:15.0,right: 15.0,top:0,bottom: 0), padding: EdgeInsets.symmetric(horizontal: 15), child: TextFormField( controller: inputFirstName, validator: (value) => _validateField(value), decoration: InputDecoration( border: OutlineInputBorder(), labelText: 'Prénom', hintText: 'Modifier le prénom'), ), ), Padding( //padding: const EdgeInsets.only(left:15.0,right: 15.0,top:0,bottom: 0), padding: EdgeInsets.symmetric(horizontal: 15), child: TextFormField( controller: inputName, validator: (value) => _validateField(value), decoration: InputDecoration( border: OutlineInputBorder(), labelText: 'Email', hintText: 'Modifier l\'adresse mail'), ), ), Padding( padding: const EdgeInsets.only( left: 15.0, right: 15.0, top: 15, bottom: 0), //padding: EdgeInsets.symmetric(horizontal: 15), child: TextFormField( controller: inputBirth, readOnly: true, validator: (value) => _validateField(value), decoration: InputDecoration( border: OutlineInputBorder(), labelText: 'Date de nassance', 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: () { if (_formKey.currentState!.validate()) { _updateProfile(context); } }, child: Text( 'Ajouter', style: TextStyle(color: Colors.white, fontSize: 25), ), ), ) ], ), ), )); } }