From c0b88fea86cd7ad1a9fdd2a2957b7343cbf03025 Mon Sep 17 00:00:00 2001 From: imlildud Date: Sat, 23 May 2026 01:46:01 -0600 Subject: [PATCH] hotfix: map sketch --- lib/src/services/geolocation_service.dart | 79 ++++---------- lib/src/views/domicilios.dart | 15 +-- lib/src/views/mapa_expandible.dart | 127 +++++++++++----------- 3 files changed, 92 insertions(+), 129 deletions(-) diff --git a/lib/src/services/geolocation_service.dart b/lib/src/services/geolocation_service.dart index 2317f4c..98e9d34 100644 --- a/lib/src/services/geolocation_service.dart +++ b/lib/src/services/geolocation_service.dart @@ -1,44 +1,41 @@ // src/services/geolocation_service.dart import 'package:geolocator/geolocator.dart'; -import 'dart:async'; -import 'package:universal_platform/universal_platform.dart'; + +class SimplePosition { + final double latitude; + final double longitude; + final DateTime timestamp; + + SimplePosition({ + required this.latitude, + required this.longitude, + required this.timestamp, + }); +} class GeolocationService { static Future isLocationServiceEnabled() async { - if (UniversalPlatform.isWeb) return true; // Web no necesita GPS activado return await Geolocator.isLocationServiceEnabled(); } static Future checkPermission() async { - if (UniversalPlatform.isWeb) return LocationPermission.always; return await Geolocator.checkPermission(); } static Future requestPermission() async { - if (UniversalPlatform.isWeb) return LocationPermission.always; return await Geolocator.requestPermission(); } static Future hasPermission() async { - if (UniversalPlatform.isWeb) return true; LocationPermission permission = await Geolocator.checkPermission(); return permission == LocationPermission.always || permission == LocationPermission.whileInUse; } - static Future getCurrentLocation() async { + static Future getCurrentLocation() async { try { - // Web usa la API de navegador - if (UniversalPlatform.isWeb) { - return await _getWebLocation(); - } - - // Móvil: usar menor precisión para más velocidad bool serviceEnabled = await Geolocator.isLocationServiceEnabled(); - if (!serviceEnabled) { - print('❌ GPS desactivado'); - return null; - } + if (!serviceEnabled) return null; LocationPermission permission = await Geolocator.checkPermission(); if (permission == LocationPermission.denied) { @@ -48,62 +45,28 @@ class GeolocationService { if (permission == LocationPermission.deniedForever) return null; - // Usar menor precisión para obtener ubicación más rápido Position position = await Geolocator.getCurrentPosition( - desiredAccuracy: LocationAccuracy.low, // ← Cambiado de best a low para más velocidad + desiredAccuracy: LocationAccuracy.low, timeLimit: const Duration(seconds: 5), ); - return position; + return SimplePosition( + latitude: position.latitude, + longitude: position.longitude, + timestamp: position.timestamp, + ); } catch (e) { print('Error: $e'); return null; } } - // Método específico para web - static Future _getWebLocation() async { - try { - // Usar la API de geolocalización del navegador - final GeolocationPosition position = await _getWebPosition(); - return Position( - latitude: position.latitude, - longitude: position.longitude, - timestamp: DateTime.now(), - accuracy: 0, - altitude: 0, - heading: 0, - speed: 0, - speedAccuracy: 0, - altitudeAccuracy: 0, - headingAccuracy: 0, - ); - } catch (e) { - print('Error en web: $e'); - return null; - } - } - - // Placeholder para web - usar HTML5 geolocation - static Future _getWebPosition() async { - // Implementación simplificada - en realidad usarías js_interop - throw UnimplementedError('Usar package:geolocator_web'); - } - - static Future getCurrentLocationWithRetry({int maxRetries = 3}) async { + static Future getCurrentLocationWithRetry({int maxRetries = 2}) async { for (int i = 0; i < maxRetries; i++) { - print('🔄 Intento ${i + 1} de $maxRetries'); final position = await getCurrentLocation(); if (position != null) return position; if (i < maxRetries - 1) await Future.delayed(const Duration(seconds: 1)); } return null; } -} - -// Clase auxiliar para web -class GeolocationPosition { - final double latitude; - final double longitude; - GeolocationPosition({required this.latitude, required this.longitude}); } \ No newline at end of file diff --git a/lib/src/views/domicilios.dart b/lib/src/views/domicilios.dart index c5e943a..5820241 100644 --- a/lib/src/views/domicilios.dart +++ b/lib/src/views/domicilios.dart @@ -1,3 +1,4 @@ +// domicilios.dart import 'package:flutter/material.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:geolocator/geolocator.dart'; @@ -132,13 +133,13 @@ class _DomiciliosViewState extends State { return; } - final position = await GeolocationService.getCurrentLocation(); + final simplePosition = await GeolocationService.getCurrentLocation(); setState(() { _isLoadingLocation = false; }); - if (position == null) { + if (simplePosition == null) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar( content: Text('No se pudo obtener tu ubicación.'), @@ -148,10 +149,10 @@ class _DomiciliosViewState extends State { return; } - _mostrarDialogoAgregarConUbicacion(position); + _mostrarDialogoAgregarConUbicacion(simplePosition.latitude, simplePosition.longitude); } - void _mostrarDialogoAgregarConUbicacion(Position position) { + void _mostrarDialogoAgregarConUbicacion(double lat, double lng) { nombreController.clear(); coloniaController.clear(); calleController.clear(); @@ -182,8 +183,8 @@ class _DomiciliosViewState extends State { crossAxisAlignment: CrossAxisAlignment.start, children: [ Text('📍 Ubicación obtenida', style: TextStyle(fontSize: 12, color: Colors.green[700])), - Text('Lat: ${position.latitude.toStringAsFixed(6)}'), - Text('Lng: ${position.longitude.toStringAsFixed(6)}'), + Text('Lat: ${lat.toStringAsFixed(6)}'), + Text('Lng: ${lng.toStringAsFixed(6)}'), ], ), ), @@ -213,7 +214,7 @@ class _DomiciliosViewState extends State { Expanded( child: ElevatedButton( style: ElevatedButton.styleFrom(backgroundColor: colorAzul, shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(15))), - onPressed: () => _agregarDomicilio(latitud: position.latitude, longitud: position.longitude), + onPressed: () => _agregarDomicilio(latitud: lat, longitud: lng), child: const Text('Agregar', style: TextStyle(color: Colors.white)), ), ), diff --git a/lib/src/views/mapa_expandible.dart b/lib/src/views/mapa_expandible.dart index cec452c..56ff1db 100644 --- a/lib/src/views/mapa_expandible.dart +++ b/lib/src/views/mapa_expandible.dart @@ -1,7 +1,6 @@ +// src/views/mapa_expandible.dart import 'package:flutter/material.dart'; -import 'package:flutter_map/flutter_map.dart'; -import 'package:latlong2/latlong.dart'; -import 'package:universal_platform/universal_platform.dart'; +import 'package:url_launcher/url_launcher.dart'; import 'rutas.dart'; class MapaExpandible extends StatefulWidget { @@ -22,20 +21,48 @@ class MapaExpandible extends StatefulWidget { class _MapaExpandibleState extends State { bool _isExpanded = false; - bool _hasError = false; + + bool get _isValidLatLng { + final lat = widget.latitud; + final lng = widget.longitud; + return lat.isFinite && lng.isFinite && lat != 0 && lng != 0; + } + + Future _abrirGoogleMaps() async { + final url = 'https://www.google.com/maps?q=${widget.latitud},${widget.longitud}&z=15'; + try { + if (await canLaunch(url)) { + await launch(url); + } + } catch (e) { + if (mounted) { + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar(content: Text('No se pudo abrir el mapa'), backgroundColor: Colors.red), + ); + } + } + } @override Widget build(BuildContext context) { - // Verificar coordenadas válidas - final isValid = widget.latitud.isFinite && widget.longitud.isFinite; + if (!_isValidLatLng) { + return Container( + padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 12), + decoration: BoxDecoration( + color: Colors.grey[200], + borderRadius: BorderRadius.circular(20), + ), + child: const Text('Ubicación no disponible', style: TextStyle(fontSize: 12)), + ); + } return Column( + mainAxisSize: MainAxisSize.min, children: [ GestureDetector( onTap: () { setState(() { _isExpanded = !_isExpanded; - _hasError = false; }); }, child: Container( @@ -55,11 +82,7 @@ class _MapaExpandibleState extends State { const SizedBox(width: 4), Text( _isExpanded ? 'Ocultar mapa' : 'Ver mapa', - style: TextStyle( - color: colorAzul, - fontSize: 12, - fontWeight: FontWeight.w500, - ), + style: TextStyle(color: colorAzul, fontSize: 12, fontWeight: FontWeight.w500), ), ], ), @@ -68,66 +91,42 @@ class _MapaExpandibleState extends State { if (_isExpanded) Padding( padding: const EdgeInsets.only(top: 12), - child: Container( - height: 250, - decoration: BoxDecoration( - borderRadius: BorderRadius.circular(15), - border: Border.all(color: colorAzul.withOpacity(0.3), width: 1), - ), - child: _hasError - ? Center( + child: GestureDetector( + onTap: _abrirGoogleMaps, + child: Container( + width: 200, // Ancho fijo en lugar de infinity + height: 160, + decoration: BoxDecoration( + borderRadius: BorderRadius.circular(15), + border: Border.all(color: colorAzul.withOpacity(0.3), width: 1), + color: Colors.grey[100], + ), child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ - Icon(Icons.map_outlined, size: 50, color: Colors.grey), - const SizedBox(height: 10), + Icon(Icons.map, size: 40, color: colorAzul), + const SizedBox(height: 8), Text( - 'No se pudo cargar el mapa', - style: TextStyle(color: Colors.grey[600]), + 'Ver en Google Maps', + style: TextStyle(color: colorAzul, fontWeight: FontWeight.bold, fontSize: 14), ), - const SizedBox(height: 5), + const SizedBox(height: 4), Text( - 'Coordenadas: ${widget.latitud.toStringAsFixed(4)}, ${widget.longitud.toStringAsFixed(4)}', - style: TextStyle(fontSize: 12, color: Colors.grey[500]), + '${widget.latitud.toStringAsFixed(4)}, ${widget.longitud.toStringAsFixed(4)}', + style: TextStyle(fontSize: 11, color: Colors.grey[600]), ), - ], - ), - ) - : ClipRRect( - borderRadius: BorderRadius.circular(15), - child: FlutterMap( - options: MapOptions( - initialCenter: LatLng( - isValid ? widget.latitud : 20.5111, - isValid ? widget.longitud : -100.9037, - ), - initialZoom: 15, - ), - children: [ - TileLayer( - urlTemplate: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png', - userAgentPackageName: 'com.example.app', - errorCallback: (error, stackTrace) { - setState(() { - _hasError = true; - }); - }, - ), - if (isValid) - MarkerLayer( - markers: [ - Marker( - point: LatLng(widget.latitud, widget.longitud), - width: 40, - height: 40, - child: const Icon( - Icons.location_pin, - color: Colors.red, - size: 40, - ), - ), - ], + const SizedBox(height: 8), + Container( + padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 3), + decoration: BoxDecoration( + color: colorAzul, + borderRadius: BorderRadius.circular(12), ), + child: const Text( + 'Tocar para abrir', + style: TextStyle(color: Colors.white, fontSize: 11), + ), + ), ], ), ),