location_selector.dart: triggerCallback

master
Rafael 2024-05-10 23:17:35 +02:00
parent e8702c713a
commit 3fa52d3e85
1 changed files with 16 additions and 11 deletions

View File

@ -1,7 +1,7 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:geocoding/geocoding.dart'; import 'package:geocoding/geocoding.dart';
import 'package:geolocator/geolocator.dart'; import 'package:geolocator/geolocator.dart';
import 'package:cofounderella/models/location.dart'; import '../models/location.dart';
class LocationSelector extends StatefulWidget { class LocationSelector extends StatefulWidget {
final Function(MyLocation) onLocationChanged; // Callback function final Function(MyLocation) onLocationChanged; // Callback function
@ -92,16 +92,7 @@ class LocationSelectorState extends State<LocationSelector> {
errorText = null; errorText = null;
}); });
// location is found, trigger callback // location is found, trigger callback
widget.onLocationChanged(MyLocation( triggerCallback();
street: _street,
country: _country,
administrativeArea: _administrativeArea,
locality: _city,
subLocality: _subLocality,
postalCode: _postalCode,
latitude: _latitude,
longitude: _longitude,
));
} else { } else {
setState(() { setState(() {
// should placeMarks be empty return latitude and longitude anyway // should placeMarks be empty return latitude and longitude anyway
@ -118,6 +109,19 @@ class LocationSelectorState extends State<LocationSelector> {
} }
} }
void triggerCallback(){
widget.onLocationChanged(MyLocation(
street: _street,
country: _country,
administrativeArea: _administrativeArea,
locality: _city,
subLocality: _subLocality,
postalCode: _postalCode,
latitude: _latitude,
longitude: _longitude,
));
}
/// Resets the location data and calls setState() /// Resets the location data and calls setState()
void _resetLocationData(String errorMessage) { void _resetLocationData(String errorMessage) {
setState(() { setState(() {
@ -187,6 +191,7 @@ class LocationSelectorState extends State<LocationSelector> {
_latitude = position.latitude; _latitude = position.latitude;
_longitude = position.longitude; _longitude = position.longitude;
}); });
triggerCallback();
} }
} catch (e) { } catch (e) {
print('Error getting current location: $e'); print('Error getting current location: $e');