after rediense

This commit is contained in:
Felipe
2024-02-26 16:32:31 -05:00
parent a1e367bdf3
commit 11d9ee6d35
11 changed files with 256 additions and 223 deletions
+145 -101
View File
@@ -7,9 +7,8 @@ import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:image_picker/image_picker.dart';
import 'package:prosappco/blocs/my_user_bloc/my_user_bloc.dart';
import 'package:prosappco/blocs/profile_bloc/profile_bloc.dart';
import 'package:prosappco/components/birth_date_picker.dart';
import 'package:prosappco/components/birthday_picker.dart';
import 'package:prosappco/components/gender_dropdown.dart';
import 'package:prosappco/components/general_primary_button.dart';
import 'package:prosappco/src/components/pop_appbar.dart';
class ProfileScreen extends StatefulWidget {
@@ -23,16 +22,17 @@ class _ProfileScreenState extends State<ProfileScreen> {
final TextEditingController _nameController = TextEditingController();
final TextEditingController _emailController = TextEditingController();
final TextEditingController _phoneController = TextEditingController();
final TextEditingController _birthDateController = TextEditingController();
final TextEditingController _birthdayController = TextEditingController();
final TextEditingController _genderController = TextEditingController();
XFile? _imageFile;
bool isLoading = false;
@override
void dispose() {
_nameController.dispose();
_emailController.dispose();
_phoneController.dispose();
_birthDateController.dispose();
_birthdayController.dispose();
_genderController.dispose();
super.dispose();
}
@@ -41,8 +41,25 @@ class _ProfileScreenState extends State<ProfileScreen> {
Widget build(BuildContext context) {
return BlocListener<ProfileBloc, ProfileState>(
listener: (context, state) {
if (state is UploadPictureSuccess) {
setState(() {});
if (state is UpdateUserInfoLoading) {
setState(() {
isLoading = true;
});
} else if (state is UpdateUserInfoSuccess) {
// Snack bar
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
content: Text('Información actualizada'),
));
setState(() {
isLoading = false;
});
} else if (state is UpdateUserInfoFailure) {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
content: Text('Error al actualizar la información'),
));
setState(() {
isLoading = false;
});
}
},
child: Scaffold(
@@ -60,75 +77,12 @@ class _ProfileScreenState extends State<ProfileScreen> {
return SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(20.0),
padding:
const EdgeInsets.symmetric(horizontal: 40, vertical: 20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
GestureDetector(
onTap: () async {
final ImagePicker picker = ImagePicker();
final XFile? image = await picker.pickImage(
source: ImageSource.gallery,
maxHeight: 500,
maxWidth: 500,
imageQuality: 40,
);
if (image != null) {
// context
// .read<ProfileBloc>()
// .add(UploadPicture(image.path, state.user!.id));
setState(() {
_imageFile = image;
});
}
},
child: state.user!.picture == "" ||
state.user!.picture == null
? _imageFile != null
? Container(
width: 150,
height: 150,
decoration: BoxDecoration(
color: Colors.grey,
shape: BoxShape.circle,
image: DecorationImage(
image: FileImage(File(_imageFile!.path)),
fit: BoxFit.contain,
),
),
)
: Container(
width: 120,
height: 120,
decoration: BoxDecoration(
color: Colors.grey.shade300,
shape: BoxShape.circle,
),
child: Icon(
CupertinoIcons.person,
color: Colors.grey.shade400,
size: 40,
),
)
: Container(
width: 150,
height: 150,
decoration: BoxDecoration(
color: Colors.grey,
shape: BoxShape.circle,
image: DecorationImage(
image: NetworkImage(context
.read<MyUserBloc>()
.state
.user
?.picture ??
''),
fit: BoxFit.contain,
),
),
),
),
pictureWidget(state, context),
const SizedBox(height: 20.0),
TextFormField(
controller: _nameController,
@@ -193,43 +147,19 @@ class _ProfileScreenState extends State<ProfileScreen> {
),
),
const SizedBox(height: 20.0),
BirthDatePicker(
BirthdayPicker(
onDateSelected: (birthDay) {
_birthDateController.text =
_birthdayController.text =
DateFormat('dd/MM/yyyy').format(birthDay);
},
controller: _birthDateController,
controller: _birthdayController,
),
const SizedBox(height: 20.0),
GenderDropdown(onChanged: (selectedGender) {
_genderController.text = selectedGender;
}),
ElevatedButton(
onPressed: () {
context.read<ProfileBloc>().add(
UpdateUserInfo(
userId: state.user!.id,
picture: _imageFile?.path ??
state.user!.picture ??
'',
name: _nameController.text.isEmpty
? null
: _nameController.text,
nickname:
_nameController.text.trim().toLowerCase(),
email: _emailController.text.isEmpty
? null
: _emailController.text,
phone: _phoneController.text.isEmpty
? null
: _phoneController.text,
birthDate: _birthDateController.text,
gender: _genderController.text,
),
);
},
child: const Text('Guardar'),
),
const SizedBox(height: 60.0),
saveButton(state, context),
],
),
),
@@ -241,4 +171,118 @@ class _ProfileScreenState extends State<ProfileScreen> {
),
);
}
Widget saveButton(MyUserState state, BuildContext context) {
return ElevatedButton(
onPressed: () {
if (isLoading) {
return;
}
final myUser = state.user!.copyWith(
name: _nameController.text,
email: _emailController.text,
phone: _phoneController.text,
birthday: _birthdayController.text,
gender: _genderController.text,
nickname: _nameController.text.trim().toLowerCase(),
);
context.read<ProfileBloc>().add(
UpdateUserInfo(
myUser: myUser,
filePicture: _imageFile?.path,
),
);
// Navigator.pop(context);
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue, // Color de fondo
padding: const EdgeInsets.symmetric(vertical: 15),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20), // Radio de borde
),
shadowColor: Colors.grey, // Sombra
elevation: 5, // Elevación
),
child: Container(
constraints: const BoxConstraints(
maxWidth: 300.0, minHeight: 50.0), // Ajustes de tamaño
alignment: Alignment.center,
child: isLoading
? const CircularProgressIndicator(
color: Colors.white,
)
: const Text(
'Actualizar',
style: TextStyle(
color: Colors.white,
fontSize: 18,
fontWeight: FontWeight.bold, // Texto audaz
),
),
),
);
}
Widget pictureWidget(MyUserState state, BuildContext context) {
final pictureUrl = state.user?.picture;
final pathImageFile = _imageFile?.path;
ImageProvider<Object>? imageProvider;
if (pathImageFile != null && pathImageFile.isNotEmpty) {
imageProvider = FileImage(File(pathImageFile));
} else if (pictureUrl != null && pictureUrl.isNotEmpty) {
imageProvider = NetworkImage(pictureUrl);
}
return GestureDetector(
onTap: () async {
final ImagePicker picker = ImagePicker();
final XFile? image = await picker.pickImage(
source: ImageSource.gallery,
maxHeight: 500,
maxWidth: 500,
imageQuality: 40,
);
if (image != null) {
setState(() {
_imageFile = image;
});
}
},
child: pictureContainerWidget(imageProvider),
);
}
Widget pictureContainerWidget(ImageProvider<Object>? imageProvider) {
final image = imageProvider == null
? null
: DecorationImage(
image: imageProvider,
fit: BoxFit.contain,
);
final widget = image == null
? Icon(
CupertinoIcons.person,
color: Colors.grey.shade400,
size: 40,
)
: null;
return Container(
width: 120,
height: 120,
decoration: BoxDecoration(
color: Colors.grey.shade300,
shape: BoxShape.circle,
image: image,
),
child: widget,
);
}
}