Untitled
import 'dart:developer'; import 'package:auto_route/auto_route.dart'; import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:garibook/app/modules/map/providers/map_data_provider.dart'; import 'package:garibook/app/modules/map/views/components/draggable_bottom_sheet.dart'; import 'package:garibook/app/utilities/context.dart'; import 'package:google_maps_flutter/google_maps_flutter.dart'; import '../../../utilities/theme_styles.dart'; import '../../../widgets/garibook_text_widgets.dart'; @RoutePage() class MapViewScreen extends ConsumerStatefulWidget { final bool fromSearchPage; const MapViewScreen({super.key, required this.fromSearchPage}); @override ConsumerState<MapViewScreen> createState() => _MapViewScreenState(); } class _MapViewScreenState extends ConsumerState<MapViewScreen> { @override void initState() { super.initState(); Future(() { ref.read(mapDataProvider).focusHasChanged = false; ref.read(mapDataProvider).mapPinInitialDataArrived = false; ref.read(mapDataProvider).isPickupLocationCurrent = true; ref.read(mapDataProvider).isDropoffLocationCurrent = false; // ref // .read(mapDataProvider) // .getCurrentLocationForRideSharing("map initstate"); }); } @override void dispose() { // Clean up all providers' states when the app closes ref.read(mapDataProvider).clearAll(); super.dispose(); } @override Widget build(BuildContext context) { final asyncMarker = ref.watch(markerProvider(context)); final driverLocProvider = ref.watch(mapDataProvider).currentLocation; final controller = ref.watch(mapDataProvider); return Scaffold( appBar: widget.fromSearchPage ? null : AppBar( leading: IconButton( onPressed: () { context.router.maybePop(); }, icon: const Icon(Icons.arrow_back_rounded, size: 20, color: ThemeStyles.whiteColor), ), backgroundColor: ThemeStyles.primaryColor, centerTitle: true, title: const GaribookTextWidget( text: "Driver's Location", fontSize: 20, color: Colors.white, ), elevation: 0, ), body: Stack( children: [ widget.fromSearchPage ? Column( children: [ Expanded( flex: 2, child: SizedBox( height: context.height * 0.6, width: context.width, child: Stack( children: [ GoogleMap( polylines: controller.polylines.isEmpty ? {} : controller.polylines.toSet(), onCameraIdle: () async { if (ref .read(mapDataProvider) .mapPinInitialDataArrived) { if (!ref.read(mapDataProvider).showCars) { await ref .read(mapDataProvider.notifier) .fetchLocationFromMapPin( LatLng( ref .watch(mapDataProvider) .currentLocation! .latitude, (ref .watch(mapDataProvider) .currentLocation! .longitude)), 'map view onCameraIdle', ref); } } }, onCameraMove: (position) async { ref .watch(mapDataProvider.notifier) .currentLocation = LatLng(position.target.latitude, position.target.longitude); }, onMapCreated: (GoogleMapController controllers) { controller.mapcontroller = controllers; _setToCurrentLoc( controllers, ref .watch(mapDataProvider.notifier) .currentLocation!); }, initialCameraPosition: CameraPosition( target: ref .watch(mapDataProvider.notifier) .currentLocation!, zoom: 20, ), ), if (!ref.read(mapDataProvider).showCars) Align( alignment: Alignment.center, child: Image.asset( fit: BoxFit.contain, 'assets/images/logo/GB-Loader.png', height: 40, width: 40, ), ), ], ), ), ), const Expanded(flex: 2, child: SizedBox()) ], ) : asyncMarker.when( data: (marker) => asyncMarker != null ? GoogleMap( onMapCreated: (GoogleMapController controllers) { controller.setMapCamera(ref); }, markers: { marker, Marker( position: LatLng(driverLocProvider!.latitude, driverLocProvider.longitude), markerId: const MarkerId('driver_location'), icon: BitmapDescriptor.defaultMarker, ), }, initialCameraPosition: CameraPosition( target: LatLng( ref .watch(mapDataProvider) .currentLocation! .latitude, ref .watch(mapDataProvider) .currentLocation! .longitude), zoom: 16, ), ) : const Center( child: Text("Driver location not available")), loading: () => const Center(child: CircularProgressIndicator()), error: (error, stackTrace) => const Center(child: Text("No Map Data Found")), ), if (widget.fromSearchPage) DraggableExpenseBottomSheetWidget(), ], )); } void _setToCurrentLoc( GoogleMapController controller, LatLng currentLocation) async { await controller.animateCamera( CameraUpdate.newLatLngZoom(currentLocation, 15), // 15 is the zoom level ); Future.delayed(const Duration(seconds: 5)).then((v) { ref.read(mapDataProvider).mapPinInitialDataArrived = true; }); } }
Leave a Comment