From 4f900db559a7791ed579bc5ff93f26e6a8af2c5f Mon Sep 17 00:00:00 2001 From: Juan Felipe Duarte <70235683+DuarteJFelipe@users.noreply.github.com> Date: Sat, 22 Apr 2023 19:02:24 -0500 Subject: [PATCH] mapa perfil profesional --- .../authentication_repository.dart | 14 +- lib/src/screens/professional_direccion.dart | 257 ++++++++++++++++++ lib/src/screens/professional_info.dart | 15 - lib/src/screens/profile.dart | 13 +- lib/src/screens/profile_pro.dart | 130 +++------ lib/src/screens/service.dart | 19 +- 6 files changed, 320 insertions(+), 128 deletions(-) create mode 100644 lib/src/screens/professional_direccion.dart diff --git a/lib/src/authentication/authentication_repository.dart b/lib/src/authentication/authentication_repository.dart index a816d8c..38b11bb 100644 --- a/lib/src/authentication/authentication_repository.dart +++ b/lib/src/authentication/authentication_repository.dart @@ -1,6 +1,5 @@ import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:firebase_auth/firebase_auth.dart'; -import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'package:google_sign_in/google_sign_in.dart'; import 'package:prosappco/src/authentication/exceptions/register_failed.dart'; @@ -163,6 +162,19 @@ class AuthenticationRepository extends GetxController { return city; } + Future getAddress(String uid) async { + String address = ''; + try { + final snapshot = + await FirebaseFirestore.instance.collection('users').doc(uid).get(); + final Map? data = snapshot.data(); + address = data?['address'] ?? ''; + } catch (e) { + print('Error getting address: $e'); + } + return address; + } + Future getPhoto(String uid) async { String photo = ''; try { diff --git a/lib/src/screens/professional_direccion.dart b/lib/src/screens/professional_direccion.dart new file mode 100644 index 0000000..31e52f5 --- /dev/null +++ b/lib/src/screens/professional_direccion.dart @@ -0,0 +1,257 @@ +import 'package:cloud_firestore/cloud_firestore.dart'; +import 'package:flutter/foundation.dart'; +import 'package:flutter/gestures.dart'; +import 'package:flutter/material.dart'; +import 'package:geocoding/geocoding.dart'; +import 'package:geolocator/geolocator.dart'; +import 'package:get/get.dart'; +import 'package:google_maps_flutter/google_maps_flutter.dart'; +import 'package:prosappco/src/authentication/authentication_repository.dart'; +import 'package:prosappco/src/components/pop_appbar.dart'; +import 'package:prosappco/src/components/primary_btn.dart'; + +class ProfessionalDireccionScreen extends StatefulWidget { + const ProfessionalDireccionScreen({super.key}); + + @override + State createState() => + _ProfessionalDireccionScreenState(); +} + +class _ProfessionalDireccionScreenState + extends State { + final TextEditingController _locationController = TextEditingController(); + final String _locationPosition = ''; + final uid = AuthenticationRepository.instance.getCurrentUserUid(); + + late GoogleMapController googleMapController; + + static const CameraPosition initialCameraPosition = CameraPosition( + target: LatLng(7.8939100, -72.5078200), + zoom: 14.4746, + ); + + Set markers = {}; + + Future _determinePosition() async { + bool serviceEnabled; + LocationPermission permission; + + serviceEnabled = await Geolocator.isLocationServiceEnabled(); + + if (!serviceEnabled) { + return Future.error('Location services are disabled'); + } + + permission = await Geolocator.checkPermission(); + + if (permission == LocationPermission.denied) { + permission = await Geolocator.requestPermission(); + + if (permission == LocationPermission.denied) { + return Future.error('Location permission denied'); + } + } + + if (permission == LocationPermission.deniedForever) { + return Future.error('Location permissions are permanently denied'); + } + + Position position = await Geolocator.getCurrentPosition(); + + return position; + } + + @override + void initState() { + super.initState(); + } + + late String lat; + late String long; + var coordinates; + + Future getLocationName(double latitude, double longitude) async { + String address; + List placemarks = + await placemarkFromCoordinates(latitude, longitude); + Placemark place = placemarks[0]; + + if (place.thoroughfare != '' || place.subThoroughfare != '') { + address = + "${place.thoroughfare} ${place.subThoroughfare} ${place.subLocality}, ${place.locality}, ${place.administrativeArea}"; + } else { + address = ''; + } + return address; + } + + Future updateAddress( + String addressName, double latitude, double longitude) async { + try { + await FirebaseFirestore.instance.collection('users').doc(uid).update({ + 'address': addressName, + 'latitude': latitude, + 'longitude': longitude, + }); + + print('Ciudad actualizada correctamente'); + } catch (e) { + try { + await FirebaseFirestore.instance.collection('users').doc(uid).set({ + 'address': addressName, + 'latitude': latitude, + 'longitude': longitude, + }); + } catch (e) { + print('Error al agregar la ciudad: $e'); + } + + print('Error al actualizar la ciudad: $e'); + } + } + + @override + Widget build(BuildContext context) { + return SafeArea( + child: Scaffold( + appBar: PopAppbar( + onPressed: () { + Navigator.pop(context); + }, + label: 'Ubicación'), + backgroundColor: const Color(0xFFD6F4FF), + body: Stack( + children: [ + GoogleMap( + mapType: MapType.normal, + initialCameraPosition: initialCameraPosition, + markers: markers, + zoomControlsEnabled: false, + onMapCreated: (GoogleMapController controller) { + googleMapController = controller; + }, + onCameraIdle: () { + if (coordinates != null) { + getLocationName(coordinates.latitude, coordinates.longitude) + .then((locationName) { + setState(() { + _locationController.text = locationName; + }); + }); + } + }, + onCameraMove: (position) { + setState(() { + coordinates = position.target; + }); + }, + gestureRecognizers: >{ + Factory( + () => EagerGestureRecognizer(), + ), + }, + ), + Container( + decoration: BoxDecoration( + color: Colors.white, + boxShadow: [ + BoxShadow( + color: Colors.grey.withOpacity(0.5), + spreadRadius: 1, + blurRadius: 5, + offset: const Offset(0, 2), + ), + ], + ), + child: Padding( + padding: const EdgeInsets.only( + left: 35, right: 35, bottom: 15, top: 5), + child: TextFormField( + controller: _locationController, + decoration: const InputDecoration( + prefixIcon: Icon(Icons.near_me), + hintText: 'Dirección', + ), + ), + ), + ), + const Positioned( + bottom: 10, + right: 0, + left: 0, + top: 0, + child: Icon( + Icons.location_on, + size: 40, + color: Colors.red, + ), + ), + Positioned( + bottom: 130, + right: 20, + child: FloatingActionButton( + onPressed: () async { + try { + Position position = await _determinePosition(); + + googleMapController.animateCamera( + CameraUpdate.newCameraPosition( + CameraPosition( + target: LatLng( + position.latitude, + position.longitude, + ), + zoom: 17), + ), + ); + setState(() {}); + } catch (e) { + Get.snackbar( + 'ubicación desactivada', + 'Por favor activa la ubicacion de tu telefono.', + snackPosition: SnackPosition.TOP, + ); + } + + // markers.clear(); + + // markers.add(Marker( + // markerId: const MarkerId('currentLocation'), + // position: + // LatLng(position.latitude, position.longitude))); + }, + elevation: 0, + child: const Icon( + Icons.gps_fixed, + size: 30, + ), + ), + ), + Positioned( + bottom: 30, + left: 0, + right: 0, + child: SizedBox( + width: MediaQuery.of(context).size.width, + child: Align( + alignment: Alignment.center, + child: PrimaryButtom( + onPressed: () { + updateAddress( + _locationController.text, + coordinates.latitude, + coordinates.longitude, + ); + Navigator.pop(context, _locationController.text); + }, + label: 'Guardar'), + ), + ), + ) + ], + ), + ), + ); + } +} diff --git a/lib/src/screens/professional_info.dart b/lib/src/screens/professional_info.dart index b3d83c7..9f80a34 100644 --- a/lib/src/screens/professional_info.dart +++ b/lib/src/screens/professional_info.dart @@ -23,21 +23,6 @@ class _ProfessionalInfoScreenState extends State { super.initState(); } - List months = [ - 'enero', - 'febrero', - 'marzo', - 'abril', - 'mayo', - 'junio', - 'julio', - 'agosto', - 'septiembre', - 'octubre', - 'noviembre', - 'diciembre', - ]; - @override Widget build(BuildContext context) { return SafeArea( diff --git a/lib/src/screens/profile.dart b/lib/src/screens/profile.dart index b3c6edb..e7bb09b 100644 --- a/lib/src/screens/profile.dart +++ b/lib/src/screens/profile.dart @@ -132,14 +132,14 @@ class _ProfileScreenState extends State { _showChoiceDialog(context); }, child: Container( - margin: EdgeInsets.symmetric(vertical: 50), + margin: const EdgeInsets.symmetric(vertical: 50), width: 100, height: 100, decoration: BoxDecoration( - color: Color(0xFF2BA4EC), + color: const Color(0xFF2BA4EC), borderRadius: BorderRadius.circular(50), ), - child: Icon( + child: const Icon( Icons.person, color: Colors.white, size: 90, @@ -189,7 +189,6 @@ class _ProfileScreenState extends State { } } } catch (e) { - print('XD $e'); return GestureDetector( onTap: () { _showChoiceDialog(context); @@ -302,7 +301,7 @@ class _ProfileScreenState extends State { child: ListBody( children: [ GestureDetector( - child: Text( + child: const Text( textAlign: TextAlign.center, "Tomar foto", style: TextStyle(color: Color(0xFF2BA4EC)), @@ -315,9 +314,9 @@ class _ProfileScreenState extends State { Navigator.of(context).pop(); }, ), - Divider(color: Colors.black54), + const Divider(color: Colors.black54), GestureDetector( - child: Text( + child: const Text( textAlign: TextAlign.center, "Abrir Galería", style: TextStyle(color: Color(0xFF2BA4EC)), diff --git a/lib/src/screens/profile_pro.dart b/lib/src/screens/profile_pro.dart index 18425a1..212ef38 100644 --- a/lib/src/screens/profile_pro.dart +++ b/lib/src/screens/profile_pro.dart @@ -13,6 +13,7 @@ import 'package:prosappco/src/components/primary_btn.dart'; import 'package:prosappco/src/components/schedule_picker.dart'; import 'package:prosappco/src/screens/horario.dart'; import 'package:prosappco/src/screens/professional.dart'; +import 'package:prosappco/src/screens/professional_direccion.dart'; import 'package:prosappco/src/services/select_image_profile.dart'; class ProfilePro extends StatefulWidget { @@ -33,6 +34,8 @@ class _ProfileProState extends State { var photoTemp = ''; var _photo = '...'; + var _direccion = '...'; + Map? _horarios; @override @@ -46,7 +49,6 @@ class _ProfileProState extends State { (String s) => setState( () { _photo = s; - print('HOLA $s'); }, ), ); @@ -61,6 +63,14 @@ class _ProfileProState extends State { ), ); } + + if (_direccion == '...') { + AuthenticationRepository.instance + .getAddress(uid.toString()) + .then((String s) => setState(() { + _direccion = s; + })); + } } Future updateInfo() async { @@ -77,94 +87,6 @@ class _ProfileProState extends State { } } - // Future downloadImage(Reference ref) async { - // try { - // if (_photo == '...' || _photo.isEmpty) { - // return GestureDetector( - // onTap: () { - // _showChoiceDialog(context); - // }, - // child: Container( - // margin: EdgeInsets.symmetric(vertical: 50), - // width: 100, - // height: 100, - // decoration: BoxDecoration( - // color: Color(0xFF2BA4EC), - // borderRadius: BorderRadius.circular(50), - // ), - // child: Icon( - // Icons.person, - // color: Colors.white, - // size: 90, - // ), - // ), - // ); - // } else { - // final imageData = await ref.getData(); - // if (imageData != null) { - // final widgetImage = GestureDetector( - // onTap: () { - // _showChoiceDialog(context); - // }, - // child: Container( - // margin: const EdgeInsets.symmetric(vertical: 50), - // child: ClipOval( - // child: Image.memory( - // imageData, - // width: 60, - // height: 60, - // fit: BoxFit.cover, - // ), - // ), - // ), - // ); - // return widgetImage; - // } else { - // return GestureDetector( - // onTap: () { - // _showChoiceDialog(context); - // }, - // child: Container( - // margin: const EdgeInsets.symmetric(vertical: 50), - // width: 100, - // height: 100, - // decoration: BoxDecoration( - // color: const Color(0xFF2BA4EC), - // borderRadius: BorderRadius.circular(50), - // ), - // child: const Icon( - // Icons.person, - // color: Colors.white, - // size: 90, - // ), - // ), - // ); - // } - // } - // } catch (e) { - // print('XD $e'); - // return GestureDetector( - // onTap: () { - // _showChoiceDialog(context); - // }, - // child: Container( - // margin: const EdgeInsets.symmetric(vertical: 50), - // width: 100, - // height: 100, - // decoration: BoxDecoration( - // color: const Color(0xFF2BA4EC), - // borderRadius: BorderRadius.circular(50), - // ), - // child: const Icon( - // Icons.person, - // color: Colors.white, - // size: 90, - // ), - // ), - // ); - // } - // } - Future updateImage(image) async { try { await FirebaseFirestore.instance @@ -257,6 +179,8 @@ class _ProfileProState extends State { @override Widget build(BuildContext context) { + String address = _direccion.toString(); + return SafeArea( child: Scaffold( appBar: AppBar( @@ -317,9 +241,31 @@ class _ProfileProState extends State { children: [ TextFormField( // controller: _emailController, - decoration: const InputDecoration( - prefixIcon: Icon(Icons.near_me), - hintText: 'Dirección'), + readOnly: true, + onTap: () async { + final String? direccion = await Navigator.push( + context, + CupertinoPageRoute( + builder: (BuildContext context) { + return const ProfessionalDireccionScreen(); + }, + ), + ) as String?; + + if (direccion != null) { + setState(() { + _direccion = direccion; + }); + } + }, + + decoration: InputDecoration( + prefixIcon: const Icon(Icons.near_me), + hintStyle: address == '' + ? const TextStyle() + : const TextStyle(color: Colors.black87), + hintText: + address == '' ? 'Dirección' : address), ), const SizedBox(height: 10), TextFormField( diff --git a/lib/src/screens/service.dart b/lib/src/screens/service.dart index 6ac6f85..834b537 100644 --- a/lib/src/screens/service.dart +++ b/lib/src/screens/service.dart @@ -121,7 +121,7 @@ class _ServiceScreenState extends State { if (place.thoroughfare != '' || place.subThoroughfare != '') { address = - "${place.thoroughfare}, ${place.subThoroughfare} ${place.subLocality}, ${place.locality}, ${place.administrativeArea}"; + "${place.thoroughfare} ${place.subThoroughfare} ${place.subLocality}, ${place.locality}, ${place.administrativeArea}"; } else { address = ''; } @@ -164,16 +164,6 @@ class _ServiceScreenState extends State { }); } }, - - // onCameraIdle: () { - // setState(() { - // if (coordinates != null) { - - // placemarkFromCoordinates(coordinates.target.latitude, coordinates.target.longitude); - // _locationController.text = coordinates.toString(); - // } - // }); - // }, onCameraMove: (position) { setState(() { coordinates = position.target; @@ -188,7 +178,7 @@ class _ServiceScreenState extends State { ), ), const Positioned( - bottom: 0, + bottom: 10, right: 0, left: 0, top: 0, @@ -233,7 +223,10 @@ class _ServiceScreenState extends State { // LatLng(position.latitude, position.longitude))); }, elevation: 0, - child: const Icon(Icons.location_on), + child: const Icon( + Icons.gps_fixed, + size: 30, + ), ), ), Padding(