1import 'package:bloc/bloc.dart';
2import 'package:equatable/equatable.dart';
3import 'package:google_maps_flutter/google_maps_flutter.dart';
4import 'package:geolocator/geolocator.dart';
5import 'package:geocoding/geocoding.dart';
7part
'location_selection_event.dart';
8part
'location_selection_state.dart';
22 Emitter<LocationSelectionState> emit,
24 emit(LocationSelectionLoading());
27 bool serviceEnabled = await Geolocator.isLocationServiceEnabled();
28 if (!serviceEnabled) {
29 emit(
const LocationSelectionError(
30 message:
'Location services are disabled',
35 LocationPermission permission = await Geolocator.checkPermission();
36 if (permission == LocationPermission.denied) {
37 permission = await Geolocator.requestPermission();
38 if (permission == LocationPermission.denied) {
39 emit(
const LocationSelectionError(
40 message:
'Location permissions are denied',
46 if (permission == LocationPermission.deniedForever) {
47 emit(
const LocationSelectionError(
48 message:
'Location permissions are permanently denied',
53 final position = await Geolocator.getCurrentPosition(
54 locationSettings:
const LocationSettings(
55 accuracy: LocationAccuracy.high,
56 timeLimit: Duration(seconds: 10),
60 final location = LatLng(position.latitude, position.longitude);
64 address:
'Getting your location...',
71 emit(
const LocationSelectionError(
72 message:
'Unable to get current location',
78 UpdateLocationEvent event,
79 Emitter<LocationSelectionState> emit,
93 Emitter<LocationSelectionState> emit,
96 List<Placemark> placemarks = await placemarkFromCoordinates(
97 event.location.latitude,
98 event.location.longitude,
101 if (placemarks.isNotEmpty) {
102 final placemark = placemarks.first;
105 if (placemark.street != null && placemark.street!.isNotEmpty) {
109 if (placemark.locality != null && placemark.locality!.isNotEmpty) {
111 address += placemark.locality!;
114 if (placemark.administrativeArea != null &&
115 placemark.administrativeArea!.isNotEmpty) {
117 address += placemark.administrativeArea!;
120 if (placemark.country != null && placemark.country!.isNotEmpty) {
148 Emitter<LocationSelectionState> emit,
159 Emitter<LocationSelectionState> emit,
161 final currentState = state;
165 address: currentState.address.isNotEmpty
166 ? currentState.address
167 :
'Location (${currentState.location.latitude.toStringAsFixed(4)}, ${currentState.location.longitude.toStringAsFixed(4)})',
173 ResetLocationSelectionEvent event,
174 Emitter<LocationSelectionState> emit,
176 emit(LocationSelectionInitial());
Future< void > _onConfirmLocation(ConfirmLocationEvent event, Emitter< LocationSelectionState > emit,) async
Future< void > _onSelectSavedLocation(SelectSavedLocationEvent event, Emitter< LocationSelectionState > emit,) async
Future< void > _onGetCurrentLocation(GetCurrentLocationEvent event, Emitter< LocationSelectionState > emit,) async
Future< void > _onUpdateLocation(UpdateLocationEvent event, Emitter< LocationSelectionState > emit,) async
Future< void > _onGetAddressFromLocation(GetAddressFromLocationEvent event, Emitter< LocationSelectionState > emit,) async
Future< void > _onResetLocationSelection(ResetLocationSelectionEvent event, Emitter< LocationSelectionState > emit,) async
class UpdateLocationEvent extends LocationSelectionEvent location
sealed class LocationSelectionEvent extends Equatable GetCurrentLocationEvent()
const GetAddressFromLocationEvent({required this.location})
const SelectSavedLocationEvent({ required this.location, required this.address, })
class SelectSavedLocationEvent extends LocationSelectionEvent ConfirmLocationEvent()
final bool isAddressLoading
const LocationConfirmed({ required this.location, required this.address, })
const LocationSelectionSuccess({ required this.location, required this.address, this.isAddressLoading=false, })