datos para el servicio

This commit is contained in:
Felipe
2024-04-03 10:58:46 -05:00
parent d7fe33cf30
commit f46450e5db
15 changed files with 470 additions and 169 deletions
+225 -125
View File
@@ -1,15 +1,19 @@
import 'dart:async';
import 'dart:developer';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:geocoding/geocoding.dart';
import 'package:geolocator/geolocator.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:injector/injector.dart';
import 'package:intl/intl.dart';
import 'package:prosappco/blocs/my_user_bloc/my_user_bloc.dart';
import 'package:prosappco/blocs/professional_list_bloc/professional_list_bloc.dart';
import 'package:prosappco/blocs/service_bloc/service_bloc.dart';
import 'package:prosappco/screens/lists/professional_list_screen.dart';
import 'package:prosappco/utils/time_of_day_extension.dart';
import 'package:service_repository/service_repository.dart';
import 'package:setting_repository/setting_repository.dart';
class UserMapScreen extends StatefulWidget {
@@ -24,13 +28,21 @@ class _UserMapScreenState extends State<UserMapScreen> {
Completer<GoogleMapController>();
LatLng? _currentP;
final TextEditingController _addressController = TextEditingController();
final TextEditingController _observationController = TextEditingController();
final settingRepository = Injector.appInstance.get<SettingRepository>();
SettingEntity? settings;
final DateFormat formatter = DateFormat('dd/MM/yyyy');
LatLng coordenadas = const LatLng(7.1253900, -73.1198000);
DateTime? fechaSeleccionada;
TimeOfDay? horaSeleccionada;
ServiceLocationPreferences? serviceLocationPreference;
UserProfessional? profesionalSeleccionado;
bool isClearButtonVisible = false;
@override
void initState() {
@@ -49,105 +61,120 @@ class _UserMapScreenState extends State<UserMapScreen> {
@override
Widget build(BuildContext context) {
return BlocBuilder<MyUserBloc, MyUserState>(
builder: (context, state) {
return Column(
children: [
Expanded(
child: Stack(
children: [
GoogleMap(
myLocationEnabled: true,
onMapCreated: (GoogleMapController controller) {
_mapController.complete(controller);
},
initialCameraPosition: const CameraPosition(
target: LatLng(
// 7.078511421411328, -73.08832615613937
7.1253900,
-73.1198000
// currentPosition.latitude, currentPosition.longitude
),
zoom: 16,
),
myLocationButtonEnabled: false,
),
Positioned(
top: 10,
left: 0,
child: Builder(
builder: (context) {
return ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: Colors.white,
shape: const CircleBorder(),
elevation: 3,
minimumSize: const Size(50, 50),
),
child: const Icon(
Icons.menu,
color: Colors.black,
size: 35,
),
onPressed: () {
Scaffold.of(context).openDrawer();
},
);
return BlocProvider<ServiceBloc>(
create: (context) => Injector.appInstance.get<ServiceBloc>(),
child: BlocBuilder<MyUserBloc, MyUserState>(
builder: (context, state) {
return Column(
children: [
Expanded(
child: Stack(
children: [
GoogleMap(
myLocationEnabled: true,
// polylines: Set<Polyline>.of(polylines.values),
onMapCreated: (GoogleMapController controller) {
_mapController.complete(controller);
},
),
),
const Positioned(
bottom: 30,
right: 0,
left: 0,
top: 0,
child: Icon(
Icons.location_on,
size: 40,
color: Color(0xFFFF0000),
),
),
Positioned(
top: 10,
right: 10,
child: FloatingActionButton(
onPressed: () async {
try {
Position position = await _determinePosition();
if (mounted) {
setState(() {
_currentP = LatLng(
position.latitude,
position.longitude,
);
});
_animateCameraToPosition(_currentP!);
}
} catch (e) {
ScaffoldMessenger.of(context).clearSnackBars();
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Por favor activa la ubicacion'),
),
);
onCameraIdle: () {
getLocationName(
coordenadas.latitude, coordenadas.longitude)
.then((value) => setState(() {
_addressController.text = value;
}));
},
onCameraMove: (position) {
if (serviceLocationPreference !=
ServiceLocationPreferences.office) {
coordenadas = position.target;
}
},
elevation: 0,
child: const Icon(
Icons.gps_fixed,
size: 30,
initialCameraPosition: const CameraPosition(
target: LatLng(7.1253900, -73.1198000),
zoom: 16,
),
myLocationButtonEnabled: false,
),
Positioned(
top: 10,
left: 0,
child: Builder(
builder: (context) {
return ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: Colors.white,
shape: const CircleBorder(),
elevation: 3,
minimumSize: const Size(50, 50),
),
child: const Icon(
Icons.menu,
color: Colors.black,
size: 35,
),
onPressed: () {
Scaffold.of(context).openDrawer();
},
);
},
),
),
),
],
const Positioned(
bottom: 30,
right: 0,
left: 0,
top: 0,
child: Icon(
Icons.location_on,
size: 40,
color: Color(0xFFFF0000),
),
),
Positioned(
top: 10,
right: 10,
child: FloatingActionButton(
onPressed: () async {
try {
Position position = await _determinePosition();
if (mounted) {
setState(() {
_currentP = LatLng(
position.latitude,
position.longitude,
);
});
_animateCameraToPosition(_currentP!);
}
} catch (e) {
ScaffoldMessenger.of(context).clearSnackBars();
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Por favor activa la ubicacion'),
),
);
}
},
elevation: 0,
child: const Icon(
Icons.gps_fixed,
size: 30,
),
),
),
],
),
),
),
buildBottom(context, state),
],
);
},
BlocListener<ServiceBloc, ServiceState>(
listener: (context, state) {},
child: buildBottom(context, state),
),
],
);
},
),
);
}
@@ -159,6 +186,11 @@ class _UserMapScreenState extends State<UserMapScreen> {
crossAxisAlignment: CrossAxisAlignment.start,
children: [
TextField(
controller: TextEditingController(
text: profesionalSeleccionado == null
? ''
: '${profesionalSeleccionado?.myUser.name ?? ''} - ${profesionalSeleccionado?.professionalInfo.profession ?? ''}',
),
readOnly: true,
onTap: () async {
var datos = await Navigator.push(context,
@@ -166,16 +198,31 @@ class _UserMapScreenState extends State<UserMapScreen> {
return const ProfessionalListScreen();
}));
log('xd -- datos $datos');
if (datos != null) {
fechaSeleccionada = datos[0];
horaSeleccionada = datos[1];
log('xd -- $fechaSeleccionada');
log('xd -- $horaSeleccionada');
serviceLocationPreference = datos[2];
profesionalSeleccionado = datos[3];
setState(() {});
if (serviceLocationPreference ==
ServiceLocationPreferences.office) {
_addressController.text =
profesionalSeleccionado?.professionalInfo.address ?? '';
_animateCameraToPosition(
LatLng(
profesionalSeleccionado!.professionalInfo.latitude,
profesionalSeleccionado!.professionalInfo.longitude,
),
);
coordenadas = LatLng(
profesionalSeleccionado!.professionalInfo.latitude,
profesionalSeleccionado!.professionalInfo.longitude,
);
} else {}
isClearButtonVisible = true;
}
},
decoration: const InputDecoration(
@@ -185,9 +232,10 @@ class _UserMapScreenState extends State<UserMapScreen> {
),
),
const SizedBox(height: 15),
const TextField(
TextField(
readOnly: true,
decoration: InputDecoration(
controller: _addressController,
decoration: const InputDecoration(
prefixIcon: Icon(Icons.location_on),
hintText: 'Dirección',
),
@@ -221,7 +269,7 @@ class _UserMapScreenState extends State<UserMapScreen> {
? ''
: horaSeleccionada!.format(context),
),
decoration: InputDecoration(
decoration: const InputDecoration(
prefixIcon: Icon(Icons.watch_later_outlined),
hintText: 'Hora',
),
@@ -245,7 +293,37 @@ class _UserMapScreenState extends State<UserMapScreen> {
children: [
Expanded(
child: FilledButton(
onPressed: () {},
onPressed: () {
if (serviceLocationPreference ==
ServiceLocationPreferences.office) {
context.read<ServiceBloc>().add(
CreateService(
professionalId:
profesionalSeleccionado!.professionalInfo.id,
userId: state.user!.id,
address: profesionalSeleccionado!
.professionalInfo.address,
aditionalAddress: profesionalSeleccionado!
.professionalInfo.address,
latitude: profesionalSeleccionado!
.professionalInfo.latitude,
longitude: profesionalSeleccionado!
.professionalInfo.longitude,
day: fechaSeleccionada.toString(),
createdAt: Timestamp.now(),
description: _observationController.text,
range1Hour1: horaSeleccionada!,
range1Hour2: horaSeleccionada!.add(hour: 2),
rate: '0',
location: serviceLocationPreference!,
),
);
} else if (serviceLocationPreference ==
ServiceLocationPreferences.delivery) {
} else {
// TODO: error inesperado
}
},
style: FilledButton.styleFrom(
backgroundColor: Theme.of(context).colorScheme.primary,
padding: const EdgeInsets.symmetric(vertical: 15),
@@ -253,34 +331,41 @@ class _UserMapScreenState extends State<UserMapScreen> {
borderRadius: BorderRadius.circular(10),
),
),
// style: ElevatedButton.styleFrom(
// backgroundColor: Theme.of(context).colorScheme.primary,
// padding: const EdgeInsets.symmetric(vertical: 15),
// shape: RoundedRectangleBorder(
// borderRadius: BorderRadius.circular(10),
// ),
// ),
child: const Text(
'Pedir cita',
style: TextStyle(color: Colors.white, fontSize: 18),
),
),
),
const SizedBox(width: 10),
FilledButton(
onPressed: () {},
style: FilledButton.styleFrom(
backgroundColor: Colors.red,
padding: const EdgeInsets.symmetric(vertical: 15),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
Visibility(
visible: isClearButtonVisible,
child: const SizedBox(width: 10),
),
Visibility(
visible: isClearButtonVisible,
child: FilledButton(
onPressed: () {
fechaSeleccionada = null;
horaSeleccionada = null;
profesionalSeleccionado = null;
isClearButtonVisible = false;
_observationController.text = '';
serviceLocationPreference = null;
setState(() {});
},
style: FilledButton.styleFrom(
backgroundColor: Colors.red,
padding: const EdgeInsets.symmetric(vertical: 15),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
),
//x
child: const Icon(
CupertinoIcons.xmark,
color: Colors.white,
size: 30,
),
),
//x
child: const Icon(
CupertinoIcons.xmark,
color: Colors.white,
size: 30,
),
),
],
@@ -302,6 +387,21 @@ class _UserMapScreenState extends State<UserMapScreen> {
);
}
Future<String> getLocationName(double latitude, double longitude) async {
String address;
List<Placemark> 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<Position> _determinePosition() async {
bool serviceEnabled;
LocationPermission permission;