import 'package:flutter/material.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:image_picker/image_picker.dart'; import 'package:prosappco/blocs/authentication_bloc/authentication_bloc.dart'; import 'package:prosappco/blocs/my_user_bloc/my_user_bloc.dart'; import 'package:prosappco/blocs/update_user_info_bloc/update_user_info_bloc.dart'; import 'package:prosappco/src/components/pop_appbar.dart'; class ProfileScreen extends StatefulWidget { const ProfileScreen({super.key}); @override State createState() => _ProfileScreenState(); } class _ProfileScreenState extends State { @override Widget build(BuildContext context) { return MultiBlocProvider( providers: [ BlocProvider( create: (context) => MyUserBloc( myUserRepository: context.read().userRepository) ..add(GetMyUser( myUserId: context.read().state.user!.uid)), ), BlocProvider( create: (context) => UpdateUserInfoBloc( userRepository: context.read().userRepository, ), ) ], child: BlocListener( listener: (context, state) { if (state is UploadPictureSuccess) { setState(() { context.read().state.user!.picture = state.userImage; }); } }, child: Scaffold( appBar: PopAppbar( onPressed: () { Navigator.pop(context); }, label: 'Perfil', ), body: BlocBuilder(builder: (context, state) { if (state.status == MyUserStatus.success) { return Padding( padding: const EdgeInsets.all(20.0), 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().add( UploadPicture(image.path, state.user!.id), ); } }, child: state.user!.picture == "" ? 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: 120, height: 120, decoration: BoxDecoration( color: Colors.grey, shape: BoxShape.circle, image: DecorationImage( image: NetworkImage( state.user!.picture!, ), fit: BoxFit.cover, ), ), ), ), const SizedBox(height: 20.0), TextFormField( initialValue: state.user?.name, decoration: const InputDecoration( labelText: 'Nombre', prefixIcon: Icon(Icons.person), hintText: 'Nombre (obligatorio)', border: OutlineInputBorder( borderRadius: BorderRadius.all( Radius.circular(25.0), )), errorBorder: OutlineInputBorder( borderSide: BorderSide(color: Colors.red), ), focusedErrorBorder: OutlineInputBorder( borderSide: BorderSide(color: Colors.red, width: 2.0), ), ), validator: (value) { if (value == null || value.isEmpty) { return 'Por favor, ingrese su nombre'; } return null; }, ), const SizedBox(height: 20.0), TextFormField( initialValue: state.user?.email, decoration: const InputDecoration( labelText: 'Nombre', prefixIcon: Icon(Icons.person), hintText: 'Nombre (obligatorio)', border: OutlineInputBorder( borderRadius: BorderRadius.all( Radius.circular(25.0), )), errorBorder: OutlineInputBorder( borderSide: BorderSide(color: Colors.red), ), focusedErrorBorder: OutlineInputBorder( borderSide: BorderSide(color: Colors.red, width: 2.0), ), ), validator: (value) { if (value == null || value.isEmpty) { return 'Por favor, ingrese su nombre'; } return null; }, ), ElevatedButton( onPressed: () {}, child: const Text('Guardar'), ), ], ), ); } else { return const Center(child: CircularProgressIndicator()); } })), ), ); } }