import 'dart:async';
import 'package:flutter/material.dart';
import 'package:geocoding/geocoding.dart';
import 'package:geolocator/geolocator.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:taxi_customer/core/app_export.dart';
import
'package:taxi_customer/presentation/map_screen/notifier/currentLocationMarker_notif
ier.dart';
import 'notifier/cLocation_notifier.dart';
import 'notifier/currentLocation_notifier.dart';
class MapScreen extends ConsumerStatefulWidget {
const MapScreen({Key? key}) : super(key: key);
@override
ConsumerState<MapScreen> createState() => _MapScreenState();
}
class _MapScreenState extends ConsumerState<MapScreen> {
late GoogleMapController googleMapController;
Map<MarkerId, Marker> markers = {};
//<------ Current Location ---------->
loadData() async {
Position position = await _getUserCurrentLocation();
setState(() {
ref.watch(currentLocationNotifier).currentLatLng = LatLng(position.latitude,
position.longitude);
ref.watch(currentLocationMarkerNotifier).locationMarker = [
Marker(
markerId: MarkerId('Current Location'),
position: ref.read(currentLocationNotifier).currentLatLng!,
infoWindow: InfoWindow(title:
ref.read(cLocationNotifier).currentLocation ?? ''),
),
];
googleMapController.animateCamera(
CameraUpdate.newCameraPosition(
new CameraPosition(
target: ref.read(currentLocationNotifier).currentLatLng!,
zoom: 14.0,
),
),
);
fetchAreaName();
});
}
//<------- Fetch Location ----------->
Future<Position> _getUserCurrentLocation() async {
await Geolocator.requestPermission().catchError((error) {
print(error.toString());
});
return await Geolocator.getCurrentPosition();
}
//<--------- Fetah Address ----------->
Future<void> fetchAreaName() async {
try {
final List<Placemark> placeMarks = await
placemarkFromCoordinates(ref.read(currentLocationNotifier).currentLatLng!.latitude,
ref.read(currentLocationNotifier).currentLatLng!.longitude);
ref.watch(cLocationNotifier).currentLocation =
placeMarks.first.locality.toString();
setState(() {});
} catch (e) {
print('Error fetching area name: $e');
}
}
@override
void initState() {
loadData();
super.initState();
}
@override
void dispose() {
googleMapController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
extendBody: true,
extendBodyBehindAppBar: true,
backgroundColor: theme.colorScheme.onPrimary.withOpacity(1),
body: Stack(
alignment: AlignmentDirectional.bottomCenter,
children: [
GoogleMap(
initialCameraPosition: CameraPosition(
target: ref.read(currentLocationNotifier).currentLatLng!,
zoom: 14,
),
mapType: MapType.hybrid,
myLocationButtonEnabled: true,
myLocationEnabled: true,
markers:
Set<Marker>.of(ref.read(currentLocationMarkerNotifier).locationMarker!),
// polylines: Set<Polyline>.of(ref.read(provider)),
onMapCreated: (GoogleMapController controller) {
setState(() {
googleMapController = controller;
});
},
),
SizedBox(
width: double.maxFinite,
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
_buildNeemuchRDGopalbari(context),
],
),
),
],
),
);
}
Widget _buildNeemuchRDGopalbari(BuildContext context) {
return Container(
padding: EdgeInsets.all(24.h),
height: 200.h,
decoration: AppDecoration.outlineBlack.copyWith(
borderRadius: BorderRadiusStyle.customBorderTL30,
),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"msg_where_do_you_want".tr,
style: theme.textTheme.titleLarge,
),
SizedBox(height: 13.v),
_buildIndicatedContainer(
value: ref.read(cLocationNotifier).currentLocation ?? '',
),
SizedBox(height: 13.v),
_buildIndicatedContainer(value:
ref.read(destinationLocationState)??'Select Destination', isDestination: true),
],
),
);
}
Widget _buildIndicatedContainer({bool isDestination = false, String value = ''})
{
return Consumer(builder: (context, ref, _) {
return GestureDetector(
child: Row(
children: [
Container(
height: 40.0,
width: 28.0,
decoration: BoxDecoration(
color: isDestination ? Color(0xff3422F2) : Colors.white,
borderRadius: BorderRadius.circular(15.0),
),
child: Padding(
padding: isDestination ? EdgeInsets.only(bottom: 5.0) :
EdgeInsets.only(top: 5.0),
child: Align(
alignment: isDestination ? Alignment.bottomCenter :
Alignment.topCenter,
child: Container(
height: 10.0,
width: 10.0,
decoration: BoxDecoration(
color: isDestination ? Colors.white : Colors.black,
shape: BoxShape.circle,
),
),
),
),
),
const SizedBox(
width: 10.0,
),
Expanded(
child: Text(
value,
maxLines: 1,
style: isDestination ? CustomTextStyles.labelLargePoppinsff000000 :
CustomTextStyles.bodySmallGray500_1,
),
),
],
),
onTap: () async{
final check =await
NavigatorService.pushNamed(AppRoutes.selectLocationScreen);
if(check == true)
{
ref.watch(currentLocationMarkerNotifier).locationMarker!.clear();
ref.watch(currentLocationMarkerNotifier).locationMarker = [
Marker(
markerId: MarkerId('Current Location'),
position: ref.read(currentLocationNotifier).currentLatLng!,
infoWindow: InfoWindow(title: 'Current Location'),
),
Marker(
markerId: MarkerId('Destination Location'),
position: ref.read(destinationLatLngState),
infoWindow: InfoWindow(title: 'Destination Location'),
),
];
googleMapController.animateCamera(
CameraUpdate.newCameraPosition(
new CameraPosition(
target: ref.read(destinationLatLngState),
zoom: 14.0,
),
),
);
// setState(() {});
// // ref.watch(locationPolyLineState.notifier).update((state) =>
Polyline(
// // polylineId: PolylineId('polyline'),
// // points: [
// // ref.read(currentLatLngState),
// // ref.read(destinationLatLngState)
// // ],
// // color: Colors.blue,
// // width: 3,
// // ));
}
},
);
});
}
}
// final locationMarkerState = StateProvider<List<Marker>>((ref) {
// return [];
// });
final locationPolyLineState = StateProvider<Polyline>((ref) {
return Polyline(polylineId: PolylineId(""));
});
// final currentLocationState = StateProvider<String>((ref) {
// return '';
// });
final destinationLatLngState = StateProvider<LatLng>((ref) {
return LatLng(24.9291, 67.0450);
});
final destinationLocationState = StateProvider<String>((ref) {
return '';
});