feat: load Google Maps key dynamically from backend

- MapsService: obtiene la key via API e inyecta el script Maps JS en runtime
- LocationPickerDialog: muestra error descriptivo si la key no esta configurada
- index.html: eliminado script estatico de Maps

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Lizandro Guarnizo
2026-06-27 07:59:40 -05:00
co-authored by Claude Sonnet 4.6
parent 863546f0fc
commit 4a2588f6f2
3 changed files with 89 additions and 7 deletions
+29 -5
View File
@@ -3,8 +3,8 @@ import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:http/http.dart' as http;
const _mapsApiKey = String.fromEnvironment('MAPS_API_KEY', defaultValue: '');
import 'package:prosapp_web_app/services/api_service.dart';
import 'package:prosapp_web_app/services/maps_service.dart';
class LocationResult {
final LatLng position;
@@ -38,10 +38,23 @@ class _LocationPickerDialogState extends State<LocationPickerDialog> {
String _city = '';
bool _loading = true;
bool _geocoding = false;
bool _mapsReady = false;
String? _mapsError;
@override
void initState() {
super.initState();
_initMaps();
}
Future<void> _initMaps() async {
final ok = await MapsService.load();
if (!mounted) return;
if (!ok) {
setState(() { _loading = false; _mapsError = 'Google Maps no configurado. Configura la API Key en el panel de administración.'; });
return;
}
setState(() => _mapsReady = true);
if (widget.initialPosition != null) {
_position = widget.initialPosition!;
_loading = false;
@@ -95,13 +108,15 @@ class _LocationPickerDialogState extends State<LocationPickerDialog> {
}
Future<void> _reverseGeocode(LatLng pos) async {
if (_mapsApiKey.isEmpty) return;
final keyData = await ApiService.instance.get('/settings/maps-key').catchError((_) => <String, dynamic>{});
final apiKey = keyData['api_key'] as String? ?? '';
if (apiKey.isEmpty) return;
setState(() => _geocoding = true);
try {
final url = Uri.parse(
'https://maps.googleapis.com/maps/api/geocode/json'
'?latlng=${pos.latitude},${pos.longitude}'
'&key=$_mapsApiKey'
'&key=$apiKey'
'&language=es',
);
final res = await http.get(url);
@@ -169,7 +184,16 @@ class _LocationPickerDialogState extends State<LocationPickerDialog> {
// Map
Expanded(
child: _loading
child: _mapsError != null
? Center(child: Padding(
padding: const EdgeInsets.all(24),
child: Column(mainAxisSize: MainAxisSize.min, children: [
const Icon(Icons.map_outlined, size: 48, color: Colors.grey),
const SizedBox(height: 12),
Text(_mapsError!, textAlign: TextAlign.center, style: const TextStyle(color: Colors.grey)),
]),
))
: _loading
? const Center(child: CircularProgressIndicator())
: Stack(
children: [