Aidra Driver 1.3.5+68
Aidra Driver - Your path to green energy
Loading...
Searching...
No Matches
start_location_screen.dart
Go to the documentation of this file.
1import 'package:easy_localization/easy_localization.dart';
2import 'package:flutter/material.dart';
3import 'package:flutter_bloc/flutter_bloc.dart';
4import 'package:google_maps_flutter/google_maps_flutter.dart';
5import 'package:geolocator/geolocator.dart';
6import 'package:geocoding/geocoding.dart';
7import 'package:go_router/go_router.dart';
8
9import '../../../../../core/constants/assets.dart';
10import '../../../../../core/router/routes.dart';
11import '../../../../../core/ui/theme/color_palette.dart';
12import '../../../../../core/ui/widgets/custom_scaffold.dart';
13import '../../bloc/planning_bloc/routing_planning_bloc.dart';
14import 'widgets/map_widget.dart';
15import 'widgets/bottom_content_panel.dart';
16
17class StartLocationScreen extends StatefulWidget {
18 const StartLocationScreen({super.key});
19
20 @override
21 State<StartLocationScreen> createState() => _StartLocationScreenState();
22}
23
24class _StartLocationScreenState extends State<StartLocationScreen> {
25 GoogleMapController? _mapController;
26 LatLng _currentLocation = const LatLng(24.6932, 46.7161);
27 bool _isMapLoaded = false;
28 String _currentAddress = 'Getting your location...';
29 bool _isLoadingLocation = true;
30 Set<Marker> _markers = {};
31
32 final String mapStyle = '''
33 [
34 {
35 "featureType": "poi",
36 "elementType": "labels.icon",
37 "stylers": [
38 {
39 "visibility": "off"
40 }
41 ]
42 },
43 {
44 "featureType": "transit",
45 "stylers": [
46 {
47 "visibility": "off"
48 }
49 ]
50 }
51 ]
52 ''';
53
54 @override
55 void initState() {
56 super.initState();
58 }
59
60 Future<void> _getCurrentLocation() async {
61 try {
62 bool serviceEnabled = await Geolocator.isLocationServiceEnabled();
63 if (!serviceEnabled) {
64 setState(() {
65 _currentAddress = 'Location services are disabled';
66 _isLoadingLocation = false;
67 });
68 return;
69 }
70
71 LocationPermission permission = await Geolocator.checkPermission();
72 if (permission == LocationPermission.denied) {
73 permission = await Geolocator.requestPermission();
74 if (permission == LocationPermission.denied) {
75 setState(() {
76 _currentAddress = 'Location permissions are denied';
77 _isLoadingLocation = false;
78 });
79 return;
80 }
81 }
82
83 if (permission == LocationPermission.deniedForever) {
84 setState(() {
85 _currentAddress = 'Location permissions are permanently denied';
86 _isLoadingLocation = false;
87 });
88 return;
89 }
90
91 final position = await Geolocator.getCurrentPosition(
92 locationSettings: const LocationSettings(
93 accuracy: LocationAccuracy.high,
94 timeLimit: Duration(seconds: 10),
95 ),
96 );
97
98 final location = LatLng(position.latitude, position.longitude);
99 final truckIcon = await BitmapDescriptor.asset(
100 const ImageConfiguration(),
102 width: 40,
103 );
104
105 setState(() {
107 _markers = {
108 Marker(
109 markerId: const MarkerId('start_location'),
110 position: location,
111 icon: truckIcon,
112 anchor: const Offset(0.5, 0.5),
113 ),
114 };
115 });
116
118
119 if (_mapController != null) {
120 await _mapController!.animateCamera(
121 CameraUpdate.newCameraPosition(
122 CameraPosition(
123 target: location,
124 zoom: 16,
125 ),
126 ),
127 );
128 }
129 } catch (e) {
130 setState(() {
131 _currentAddress = 'Unable to get current location';
132 _isLoadingLocation = false;
133 });
134 }
135 }
136
137 Future<void> _getAddressFromLocation(LatLng location) async {
138 try {
139 List<Placemark> placemarks = await placemarkFromCoordinates(
140 location.latitude,
141 location.longitude,
142 );
143
144 if (placemarks.isNotEmpty && mounted) {
145 final placemark = placemarks.first;
146 String address = '';
147
148 if (placemark.street != null && placemark.street!.isNotEmpty) {
149 address += placemark.street!;
150 }
151
152 if (placemark.locality != null && placemark.locality!.isNotEmpty) {
153 if (address.isNotEmpty) address += ', ';
154 address += placemark.locality!;
155 }
156
157 if (placemark.administrativeArea != null &&
158 placemark.administrativeArea!.isNotEmpty) {
159 if (address.isNotEmpty) address += ', ';
160 address += placemark.administrativeArea!;
161 }
162
163 setState(() {
164 _currentAddress = address.isNotEmpty ? address : 'scheduling.current_location'.tr();
165 _isLoadingLocation = false;
166 });
167 }
168 } catch (e) {
169 if (mounted) {
170 setState(() {
171 _currentAddress = 'scheduling.current_location'.tr();
172 _isLoadingLocation = false;
173 });
174 }
175 }
176 }
177
178 void _onContinue() {
179 context.read<RoutingPlanningBloc>().add(
183 ),
184 );
185 context.push(Routes.weeklyCollectionsScreen.route);
186 }
187
188 @override
189 Widget build(BuildContext context) {
190 return CustomScaffold(
191 isLeadingVisible: false,
192 title: 'scheduling.start_location'.tr(),
193 backgroundColor: ColorPalette.antiFlashWhite,
194 body: Column(
195 children: [
196 Expanded(
197 child: MapWidget(
198 currentLocation: _currentLocation,
199 markers: _markers,
200 isMapLoaded: _isMapLoaded,
201 onMapCreated: (GoogleMapController controller) {
202 _mapController = controller;
203 setState(() {
204 _isMapLoaded = true;
205 });
206 },
208 ),
209 ),
210 BottomContentPanel(
211 isLoadingLocation: _isLoadingLocation,
212 currentAddress: _currentAddress,
213 onContinue: _onContinue,
214 ),
215 ],
216 ),
217 );
218 }
219
220 @override
221 void dispose() {
222 _mapController?.dispose();
223 super.dispose();
224 }
225}
override void initState()
override void dispose()
class App extends StatefulWidget build(BuildContext context)
Definition app.dart:31
static const String pinIcon
Definition assets.dart:6
static const antiFlashWhite
const StartLocationScreen({super.key})
override State< StartLocationScreen > createState()
final Widget child
class Partner String
final String address
final String mapStyle
class EndLocationChooserScreen extends StatefulWidget _mapController
Future< void > _getAddressFromLocation(LatLng location) async
class UpdateLocationEvent extends LocationSelectionEvent location
Routes
Definition routes.dart:30
const SetStartLocationEvent({ required this.location, required this.address, })
final String title
void _onContinue()
bool _isLoadingLocation
Set< Marker > _markers
Future< void > _getCurrentLocation() async
LatLng _currentLocation
style Column(crossAxisAlignment:CrossAxisAlignment.end, children:[Container(padding:EdgeInsets.symmetric(horizontal:8.w, vertical:4.h), decoration:BoxDecoration(color:ColorPalette.tiffanyBlue.withValues(alpha:0.1), borderRadius:BorderRadius.circular(12),), child:Text(collection.type ?? '', style:Theme.of(context).textTheme.bodySmall?.copyWith(color:ColorPalette.tiffanyBlue, fontWeight:FontWeight.bold,),),),],)