Aidra Driver 1.3.5+68
Aidra Driver - Your path to green energy
Loading...
Searching...
No Matches
location_selection_bloc.dart
Go to the documentation of this file.
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';
6
7part 'location_selection_event.dart';
8part 'location_selection_state.dart';
9
10class LocationSelectionBloc extends Bloc<LocationSelectionEvent, LocationSelectionState> {
11 LocationSelectionBloc() : super(LocationSelectionInitial()) {
12 on<GetCurrentLocationEvent>(_onGetCurrentLocation);
13 on<UpdateLocationEvent>(_onUpdateLocation);
14 on<GetAddressFromLocationEvent>(_onGetAddressFromLocation);
15 on<SelectSavedLocationEvent>(_onSelectSavedLocation);
16 on<ConfirmLocationEvent>(_onConfirmLocation);
17 on<ResetLocationSelectionEvent>(_onResetLocationSelection);
18 }
19
22 Emitter<LocationSelectionState> emit,
23 ) async {
24 emit(LocationSelectionLoading());
25
26 try {
27 bool serviceEnabled = await Geolocator.isLocationServiceEnabled();
28 if (!serviceEnabled) {
29 emit(const LocationSelectionError(
30 message: 'Location services are disabled',
31 ));
32 return;
33 }
34
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',
41 ));
42 return;
43 }
44 }
45
46 if (permission == LocationPermission.deniedForever) {
47 emit(const LocationSelectionError(
48 message: 'Location permissions are permanently denied',
49 ));
50 return;
51 }
52
53 final position = await Geolocator.getCurrentPosition(
54 locationSettings: const LocationSettings(
55 accuracy: LocationAccuracy.high,
56 timeLimit: Duration(seconds: 10),
57 ),
58 );
59
60 final location = LatLng(position.latitude, position.longitude);
61
64 address: 'Getting your location...',
65 isAddressLoading: true,
66 ));
67
68 // Get address for the location
70 } catch (e) {
71 emit(const LocationSelectionError(
72 message: 'Unable to get current location',
73 ));
74 }
75 }
76
77 Future<void> _onUpdateLocation(
78 UpdateLocationEvent event,
79 Emitter<LocationSelectionState> emit,
80 ) async {
82 location: event.location,
83 address: 'Getting address...',
84 isAddressLoading: true,
85 ));
86
87 // Get address for the new location
88 add(GetAddressFromLocationEvent(location: event.location));
89 }
90
93 Emitter<LocationSelectionState> emit,
94 ) async {
95 try {
96 List<Placemark> placemarks = await placemarkFromCoordinates(
97 event.location.latitude,
98 event.location.longitude,
99 );
100
101 if (placemarks.isNotEmpty) {
102 final placemark = placemarks.first;
103 String address = '';
104
105 if (placemark.street != null && placemark.street!.isNotEmpty) {
106 address += placemark.street!;
107 }
108
109 if (placemark.locality != null && placemark.locality!.isNotEmpty) {
110 if (address.isNotEmpty) address += ', ';
111 address += placemark.locality!;
112 }
113
114 if (placemark.administrativeArea != null &&
115 placemark.administrativeArea!.isNotEmpty) {
116 if (address.isNotEmpty) address += ', ';
117 address += placemark.administrativeArea!;
118 }
119
120 if (placemark.country != null && placemark.country!.isNotEmpty) {
121 if (address.isNotEmpty) address += ', ';
122 address += placemark.country!;
123 }
124
126 location: event.location,
127 address: address.isNotEmpty ? address : 'Selected Location',
128 isAddressLoading: false,
129 ));
130 } else {
132 location: event.location,
133 address: 'Selected Location',
134 isAddressLoading: false,
135 ));
136 }
137 } catch (e) {
139 location: event.location,
140 address: 'Selected Location',
141 isAddressLoading: false,
142 ));
143 }
144 }
145
148 Emitter<LocationSelectionState> emit,
149 ) async {
151 location: event.location,
152 address: event.address,
153 isAddressLoading: false,
154 ));
155 }
156
157 Future<void> _onConfirmLocation(
159 Emitter<LocationSelectionState> emit,
160 ) async {
161 final currentState = state;
162 if (currentState is LocationSelectionSuccess) {
164 location: currentState.location,
165 address: currentState.address.isNotEmpty
166 ? currentState.address
167 : 'Location (${currentState.location.latitude.toStringAsFixed(4)}, ${currentState.location.longitude.toStringAsFixed(4)})',
168 ));
169 }
170 }
171
173 ResetLocationSelectionEvent event,
174 Emitter<LocationSelectionState> emit,
175 ) async {
176 emit(LocationSelectionInitial());
177 }
178}
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 Partner String
final String address
final String message
Definition failures.dart:0
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, })