import 'dart:io'; import 'package:flutter/material.dart'; import 'package:google_maps_flutter/google_maps_flutter.dart'; import 'dart:async'; import 'package:flutter/services.dart' show PlatformException, rootBundle; import 'package:flutter/cupertino.dart'; import 'package:location/location.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:geolocator/geolocator.dart'; import 'package:teso/Classes/API%20Clasess/TesoBusinessDetail.dart'; import 'package:teso/Classes/TesoShop.dart'; import 'package:teso/Pages/Sub_Pages/BusinessDetails.dart'; import 'package:flutter_polyline_points/flutter_polyline_points.dart'; import 'package:teso/util/consts.dart'; class CouponLocator extends StatefulWidget { final TesoBusinessDetail shop; const CouponLocator({Key key, this.shop}) : super(key: key); @override _CouponLocatorState createState() => _CouponLocatorState( shop: new TesoShop( categoryShop: this.shop.businessCategory, dateEst: this.shop.dateOfEst, email: this.shop.businessEmail, handle: this.shop.handle, latitude: double.parse(this.shop.businessLat), logo: this.shop.businessLogo, longitude: double.parse(this.shop.businessLng), shopAddress: this.shop.businessAddress, shopDescription: this.shop.businessDescription, shopID: this.shop.businessId, shopName: this.shop.businessName, shopPhone: this.shop.businessContact, shopTin: this.shop.businessTin, )); } class _CouponLocatorState extends State { String mapstyle; var _future; static LatLng _initialPosition; Map markers = {}; GoogleMapController mapController; static const double CAMERA_ZOOM = 14.4746; static const double CAMERA_TILT = 80; static const double CAMERA_BEARING = 30; TesoShop shop; _CouponLocatorState({this.shop}); Map polylines = {}; String selectedshop = ""; Location location = Location(); LocationData _location; bool ios = false; Future _determinePosition() async { try { final LocationData _locationResult = await location.getLocation(); setState(() { _location = _locationResult; _initialPosition = LatLng(_location.latitude, _location.longitude); }); await getLocations(); return await navigateToShop(shop); } on PlatformException catch (err) { setState(() { print(err.code); }); return false; } } getLocations() async { MarkerId markerId = MarkerId(widget.shop.businessId); Marker marker = Marker( markerId: markerId, position: LatLng(double.parse(widget.shop.businessLat), double.parse(widget.shop.businessLng)), icon: BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueCyan), infoWindow: InfoWindow( title: widget.shop.businessName, snippet: widget.shop.businessAddress, ), onTap: () => showModalBottomSheet( context: context, shape: RoundedRectangleBorder( borderRadius: BorderRadius.vertical(top: Radius.circular(30.0)), ), builder: (BuildContext bc) { return buildShopDetails(bc, shop, navigateToShop); }, ), ); if (mounted) { setState(() { markers[markerId] = marker; }); } } @override void initState() { super.initState(); ios = Platform.isIOS; SharedPreferences.getInstance().then((prefs) { String currentTheme = prefs.getString("theme"); if (currentTheme == "light") { rootBundle.loadString('assets/styles/light.txt').then((string) { mapstyle = string; }); } else { rootBundle.loadString('assets/styles/dark.txt').then((string) { mapstyle = string; }); } }); _future = _determinePosition(); location.onLocationChanged.listen((LocationData cLoc) { _initialPosition = LatLng(cLoc.latitude, cLoc.longitude); }); } navigateToShop(TesoShop tesoShop) async { MarkerId markerId = MarkerId(tesoShop.shopName + " Location"); Marker marker = Marker( markerId: markerId, position: LatLng(tesoShop.latitude, tesoShop.longitude), icon: BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueGreen), infoWindow: InfoWindow( title: tesoShop.shopName, snippet: tesoShop.shopAddress, ), ); setState(() { markers.clear(); markers[markerId] = marker; selectedshop = tesoShop.shopName; }); Position user = Position( latitude: _initialPosition.latitude, longitude: _initialPosition.longitude, accuracy: 100, altitude: 100, heading: 100, speed: 100, speedAccuracy: 100, timestamp: DateTime.now()); Position shopLoc = Position( latitude: tesoShop.latitude, longitude: tesoShop.longitude, accuracy: 100, altitude: 100, heading: 100, speed: 100, speedAccuracy: 100, timestamp: DateTime.now()); if (await createPolylines(user, shopLoc)) { return true; } else { return false; } } createPolylines(Position start, Position destination) async { try { PolylinePoints polylinePoints; List polylineCoordinates = []; polylinePoints = PolylinePoints(); PolylineResult result = await polylinePoints.getRouteBetweenCoordinates( mapsKey, PointLatLng(start.latitude, start.longitude), PointLatLng(destination.latitude, destination.longitude), travelMode: TravelMode.driving, ); if (result.points.isNotEmpty) { result.points.forEach((PointLatLng point) { polylineCoordinates.add(LatLng(point.latitude, point.longitude)); }); } PolylineId id = PolylineId('poly'); Polyline polyline = Polyline( polylineId: id, color: Theme.of(context).colorScheme.secondary, points: polylineCoordinates, width: 5, ); setState(() { polylines[id] = polyline; }); return true; } catch (e) { print(e); return false; } } @override void dispose() { mapController.dispose(); super.dispose(); } @override Widget build(BuildContext context) { return new Scaffold( body: FutureBuilder( future: _future, builder: (context, snapshot) { if (snapshot.connectionState == ConnectionState.waiting) { return Stack( children: [ ios ? Container( margin: EdgeInsets.only( top: (MediaQuery.of(context).size.height) - (MediaQuery.of(context).size.height * 0.935), left: 10), child: Material( elevation: 5, color: Color.fromRGBO(0, 0, 0, 0.4), borderRadius: BorderRadius.only( bottomLeft: Radius.circular(25), bottomRight: Radius.circular(25), topLeft: Radius.circular(25), topRight: Radius.circular(25), ), child: ClipRRect( borderRadius: BorderRadius.circular(25.0), child: IconButton( icon: Icon( Icons.arrow_back_ios, size: 20, ), color: Colors.white, onPressed: () => Navigator.pop(context), ), ), ), ) : Container(), Container( padding: EdgeInsets.only( top: MediaQuery.of(context).size.width * 0.7), height: MediaQuery.of(context).size.height, width: MediaQuery.of(context).size.width, child: Center( child: Column( children: [ CupertinoActivityIndicator( animating: true, radius: 15, ), Text("Routing to shop....."), ], ), ), ), ], ); } else if (snapshot.data == false && snapshot.connectionState == ConnectionState.done) { Navigator.of(context).pop(); return Container(); } else { return Stack( children: [ GoogleMap( padding: EdgeInsets.only( top: 70.0, ), zoomGesturesEnabled: true, zoomControlsEnabled: false, compassEnabled: true, myLocationButtonEnabled: true, myLocationEnabled: true, markers: Set.of(markers.values), initialCameraPosition: CameraPosition( target: _initialPosition, zoom: CAMERA_ZOOM, tilt: CAMERA_TILT, bearing: CAMERA_BEARING, ), onMapCreated: (GoogleMapController controller) { controller.setMapStyle(mapstyle); mapController = controller; }, onCameraMove: (position) { setState(() { _initialPosition = LatLng( position.target.latitude, position.target.longitude); }); }, polylines: Set.of(polylines.values), ), Container( margin: EdgeInsets.only( top: (MediaQuery.of(context).size.height) - (MediaQuery.of(context).size.height * 0.935), left: 10), child: Material( elevation: 5, color: Color.fromRGBO(0, 0, 0, 0.4), borderRadius: BorderRadius.only( bottomLeft: Radius.circular(25), bottomRight: Radius.circular(25), topLeft: Radius.circular(25), topRight: Radius.circular(25), ), child: ClipRRect( borderRadius: BorderRadius.circular(25.0), child: IconButton( icon: Icon( Icons.arrow_back_ios, size: 20, ), color: Colors.white, onPressed: () => Navigator.pop(context), ), ), ), ), ], ); } }, ), ); } }