import 'dart:convert'; import 'dart:io'; import 'package:camera/camera.dart'; import 'package:teso/GeneralWidgets/inputTextLimited.dart'; import 'package:teso/Pages/Sub_Pages/@Generic/Camera/Picture/TakeDP.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:image_cropper/image_cropper.dart'; import 'package:image_picker/image_picker.dart'; import 'package:page_transition/page_transition.dart'; import 'package:provider/provider.dart'; import 'package:teso/Classes/TesoUser.dart'; import 'package:teso/GeneralWidgets/inputText.dart'; import 'package:teso/providers/user_provider.dart'; import 'package:teso/util/consts.dart'; class EditProfile extends StatefulWidget { final TesoUser? userProfile; final List? connectedCameras; const EditProfile({Key? key, this.userProfile, this.connectedCameras}) : super(key: key); @override _EditProfileState createState() => _EditProfileState(userProfile: this.userProfile); } enum AppState { free, picked, cropped, } class _EditProfileState extends State { TesoUser? userProfile; _EditProfileState({this.userProfile}); AppState? state; bool imageAvailable = false; File? _image; String? thumb; bool passed = false; bool failed = false; final picker = ImagePicker(); TextEditingController username = new TextEditingController(); TextEditingController firstname = new TextEditingController(); TextEditingController surname = new TextEditingController(); TextEditingController telephone = new TextEditingController(); TextEditingController email = new TextEditingController(); TextEditingController address = new TextEditingController(); TextEditingController description = new TextEditingController(); _imageSelection(context) async { final pickedFile = await Navigator.push( context, PageTransition( type: PageTransitionType.leftToRight, child: TakeDP(connectedCameras: widget.connectedCameras!), )); if (pickedFile != null) { _image = File(pickedFile.path); setState(() { _cropImage(); imageAvailable = true; }); } } Future _cropImage() async { ImageCropper imageCropper = new ImageCropper(); CroppedFile? croppedFile = await imageCropper.cropImage( sourcePath: _image!.path, cropStyle: CropStyle.circle, aspectRatioPresets: Platform.isAndroid ? [CropAspectRatioPreset.original] : [CropAspectRatioPreset.original]); // [ // AndroidUiSettings( // toolbarTitle: 'Teso Profile Picture', // toolbarColor: Theme.of(context).colorScheme.secondary, // toolbarWidgetColor: Colors.white, // initAspectRatio: CropAspectRatioPreset.original, // lockAspectRatio: false), // iosUiSettings: IOSUiSettings( // title: 'Teso Profile Picture', // ))]; if (croppedFile != null) { setState(() { // _image = croppedFile.; state = AppState.cropped; imageAvailable = true; }); } else { _clearImage(); } } void _clearImage() { setState(() { _image = null; state = AppState.free; imageAvailable = false; }); } Future _updateUser() async { try { String? thumbnail; if (_image != null) { List imageBytes = await _image!.readAsBytes(); thumbnail = base64Encode(imageBytes); } else if (thumb != null) { thumbnail = thumb; } TesoUser updateuser = new TesoUser(); updateuser.userGUID = userProfile!.userGUID; updateuser.username = username.text; updateuser.firstname = firstname.text; updateuser.lastname = surname.text; updateuser.address = address.text; updateuser.description = description.text; updateuser.email = email.text; updateuser.phonenumber = telephone.text; updateuser.gold = userProfile!.gold; updateuser.silver = userProfile!.silver; updateuser.friends = userProfile!.friends; updateuser.displaypicture = thumbnail; updateuser.thumbnail_dp = userProfile!.thumbnail_dp; updateuser.country = userProfile!.country; updateuser.gender = userProfile!.gender; updateuser.dateOfBirth = userProfile!.dateOfBirth; print(updateuser.toString()); UserProvider userProvider = Provider.of(context, listen: false); userProvider.updateUser(updateuser); if (mounted) { setState(() { passed = true; }); } Future.delayed(const Duration(seconds: 5), () { if (passed) { if (mounted) { setState(() { passed = false; }); } } }); } catch (e) { print(e); if (mounted) { setState(() { failed = true; }); } Future.delayed(const Duration(seconds: 5), () { if (failed) { if (mounted) { setState(() { failed = false; }); } } }); } } @override void initState() { username.text = userProfile!.username.toString(); firstname.text = userProfile!.firstname!; surname.text = userProfile!.lastname!; address.text = userProfile!.address!; email.text = userProfile!.email!; telephone.text = userProfile!.phonenumber!; description.text = userProfile!.description!; thumb = userProfile!.thumbnail_dp; super.initState(); } @override Widget build(BuildContext context) { return AnnotatedRegion( value: SystemUiOverlayStyle.light, child: GestureDetector( onTap: () => FocusScope.of(context).unfocus(), child: Container( height: MediaQuery.of(context).size.height * 0.9, child: Scaffold( body: SingleChildScrollView( scrollDirection: Axis.vertical, child: new Wrap( children: [ new Container( margin: EdgeInsets.only( left: 20.0, top: 10.0, bottom: 10.0, ), child: Row( children: [ Container( child: InkWell( onTap: () => Navigator.pop(context), child: Icon(Icons.close), ), ), Expanded( child: Container( width: double.infinity, child: Center( child: Text( "Edit Profile", style: TextStyle( fontSize: 17.0, ), ), ), ), ), new Container( margin: EdgeInsets.symmetric( vertical: 0.0, horizontal: 20), child: new Center( child: ElevatedButton( style: ElevatedButton.styleFrom( primary: Theme.of(context).colorScheme.secondary, shape: RoundedRectangleBorder( borderRadius: BorderRadius.all( Radius.circular(25.0), ), ), ), onPressed: () => _updateUser(), child: Text("Done"), ), ), ) ], )), Padding( padding: const EdgeInsets.symmetric(horizontal: 20.0), child: Divider(), ), Visibility( visible: passed, child: Container( margin: EdgeInsets.only(top: 10, bottom: 10), color: Colors.green, width: double.infinity, height: 20, child: Center( child: Text( 'Profile updated successfully', style: TextStyle( color: Colors.white, ), ), ), ), ), Visibility( visible: failed, child: Container( margin: EdgeInsets.only(top: 10, bottom: 10), color: Colors.red, width: double.infinity, height: 20, child: Center( child: Text( 'An error occurred', style: TextStyle( color: Colors.white, ), ), ), ), ), Center( child: InkWell( onTap: () => _imageSelection(context), child: Container( //color: Theme.of(context).accentColor, height: 130, width: 130, decoration: new BoxDecoration( shape: BoxShape.circle, color: Colors.grey[400], ), child: _image == null && thumb == null ? Center( child: Text( username.text.characters .characterAt(0) .toString() .toUpperCase(), ), ) : _image == null && thumb == "null" ? Center( child: Text( username.text.characters .characterAt(0) .toString() .toUpperCase(), style: TextStyle( fontSize: 24, fontWeight: FontWeight.bold, ), ), ) : _image != null ? Container( height: 130, width: 130, decoration: BoxDecoration( shape: BoxShape.circle, ), child: ClipRRect( borderRadius: BorderRadius.only( topLeft: Radius.circular(90.0), topRight: Radius.circular(90.0), bottomLeft: Radius.circular(90), bottomRight: Radius.circular(90), ), child: FadeInImage( placeholder: AssetImage( "assets/images/tesoDP/dp1.png"), height: 130, width: 130, fit: BoxFit.fill, image: FileImage( File( _image!.path, ), ), ), ), ) : Container( height: 130, width: 130, decoration: BoxDecoration( shape: BoxShape.circle, ), child: ClipRRect( borderRadius: BorderRadius.only( topLeft: Radius.circular(90.0), topRight: Radius.circular(90.0), bottomLeft: Radius.circular(90), bottomRight: Radius.circular(90), ), child: FadeInImage( height: 130, width: 130, fit: BoxFit.fill, image: NetworkImage(userdpURL + thumb!), placeholder: AssetImage( "assets/images/tesoDP/dp1.png"), ), ), ), ), ), ), Padding( padding: const EdgeInsets.symmetric( horizontal: 20.0, vertical: 20.0), child: Divider(), ), new ListTile( enabled: false, title: buildInputContainer( context, username, "Username", false), ), Padding( padding: const EdgeInsets.symmetric(horizontal: 20.0), child: Divider(), ), new ListTile( title: buildInputContainer( context, firstname, "Firstname", true), ), Padding( padding: const EdgeInsets.symmetric(horizontal: 20.0), child: Divider(), ), new ListTile( title: buildInputContainer(context, surname, "Lastname", true), ), Padding( padding: const EdgeInsets.symmetric(horizontal: 20.0), child: Divider(), ), new ListTile( title: buildInputContainerLimit( context, description, "About You", true, 120), ), Padding( padding: const EdgeInsets.symmetric(horizontal: 20.0), child: Divider(), ), new ListTile( title: buildInputContainer( context, email, "Contact (email)", true), ), Padding( padding: const EdgeInsets.symmetric(horizontal: 20.0), child: Divider(), ), new ListTile( title: buildInputContainer( context, telephone, "Contact (phone number)", true), ), Padding( padding: const EdgeInsets.symmetric(horizontal: 20.0), child: Divider(), ), // new ListTile( // title: // buildInputContainer(context, address, "Address", true), // ), // Padding( // padding: const EdgeInsets.symmetric(horizontal: 20.0), // child: Divider(), // ), ], ), ), ), ), ), ); } }