import 'dart:async'; import 'package:flutter/widgets.dart'; import 'package:geolocator/geolocator.dart'; import 'package:google_maps_flutter/google_maps_flutter.dart'; import 'package:google_maps_webservice/geocoding.dart'; import 'package:google_maps_webservice/places.dart'; import 'package:http/http.dart'; import 'package:provider/provider.dart'; import '../src/models/pick_result.dart'; import '../src/place_picker.dart'; class PlaceProvider extends ChangeNotifier { PlaceProvider( String apiKey, String? proxyBaseUrl, Client? httpClient, Map apiHeaders, ) { places = GoogleMapsPlaces( apiKey: apiKey, baseUrl: proxyBaseUrl, httpClient: httpClient, apiHeaders: apiHeaders as Map?, ); geocoding = GoogleMapsGeocoding( apiKey: apiKey, baseUrl: proxyBaseUrl, httpClient: httpClient, apiHeaders: apiHeaders as Map?, ); } static PlaceProvider of(BuildContext context, {bool listen = true}) => Provider.of(context, listen: listen); late GoogleMapsPlaces places; late GoogleMapsGeocoding geocoding; String? sessionToken; bool isOnUpdateLocationCooldown = false; LocationAccuracy? desiredAccuracy; bool isAutoCompleteSearching = false; Future updateCurrentLocation(bool forceAndroidLocationManager) async { bool serviceEnabled; LocationPermission permission; // Test if location services are enabled. serviceEnabled = await Geolocator.isLocationServiceEnabled(); if (!serviceEnabled) { // Location services are not enabled don't continue // accessing the position and request users of the // App to enable the location services. return Future.error('Location services are disabled.'); } permission = await Geolocator.checkPermission(); if (permission == LocationPermission.denied) { permission = await Geolocator.requestPermission(); if (permission == LocationPermission.denied) { // Permissions are denied, next time you could try // requesting permissions again (this is also where // Android's shouldShowRequestPermissionRationale // returned true. According to Android guidelines // your App should show an explanatory UI now. return Future.error('Location permissions are denied'); } } if (permission == LocationPermission.deniedForever) { // Permissions are denied forever, handle appropriately. return Future.error( 'Location permissions are permanently denied, we cannot request permissions.'); } Position position = await Geolocator.getCurrentPosition( desiredAccuracy: LocationAccuracy.best, ); currentPosition = position; // notifyListeners(); } Position? _currentPosition; Position? get currentPosition => _currentPosition; set currentPosition(Position? newPosition) { _currentPosition = newPosition; notifyListeners(); } Timer? _debounceTimer; Timer? get debounceTimer => _debounceTimer; set debounceTimer(Timer? timer) { _debounceTimer = timer; notifyListeners(); } CameraPosition? _previousCameraPosition; CameraPosition? get prevCameraPosition => _previousCameraPosition; setPrevCameraPosition(CameraPosition? prePosition) { _previousCameraPosition = prePosition; } CameraPosition? _currentCameraPosition; CameraPosition? get cameraPosition => _currentCameraPosition; setCameraPosition(CameraPosition? newPosition) { _currentCameraPosition = newPosition; } PickResult? _selectedPlace; PickResult? get selectedPlace => _selectedPlace; set selectedPlace(PickResult? result) { _selectedPlace = result; notifyListeners(); } SearchingState _placeSearchingState = SearchingState.Idle; SearchingState get placeSearchingState => _placeSearchingState; set placeSearchingState(SearchingState newState) { _placeSearchingState = newState; notifyListeners(); } GoogleMapController? _mapController; GoogleMapController? get mapController => _mapController; set mapController(GoogleMapController? controller) { _mapController = controller; notifyListeners(); } PinState _pinState = PinState.Preparing; PinState get pinState => _pinState; set pinState(PinState newState) { _pinState = newState; notifyListeners(); } bool _isSeachBarFocused = false; bool get isSearchBarFocused => _isSeachBarFocused; set isSearchBarFocused(bool focused) { _isSeachBarFocused = focused; notifyListeners(); } MapType _mapType = MapType.normal; MapType get mapType => _mapType; setMapType(MapType mapType, {bool notify = false}) { _mapType = mapType; if (notify) notifyListeners(); } switchMapType() { _mapType = MapType.values[(_mapType.index + 1) % MapType.values.length]; if (_mapType == MapType.none) _mapType = MapType.normal; notifyListeners(); } }