Advertisement
mrblab24

Travel Hour Guide Screen Bug Solution

Dec 7th, 2020 (edited)
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 12.26 KB | None | 0 0
  1. import 'dart:async';
  2. import 'dart:typed_data';
  3. import 'package:cloud_firestore/cloud_firestore.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter_polyline_points/flutter_polyline_points.dart';
  6. import 'package:geo/geo.dart' as geo;
  7. import 'package:google_maps_flutter/google_maps_flutter.dart';
  8. import 'package:sliding_up_panel/sliding_up_panel.dart';
  9. import 'package:travel_hour/blocs/ads_bloc.dart';
  10. import 'package:travel_hour/models/colors.dart';
  11. import 'package:travel_hour/config/config.dart';
  12. import 'package:travel_hour/models/place.dart';
  13. import 'package:easy_localization/easy_localization.dart';
  14. import 'package:travel_hour/utils/convert_map_icon.dart';
  15. import 'package:travel_hour/utils/map_util.dart';
  16. import 'package:provider/provider.dart';
  17.  
  18.  
  19.  
  20.  
  21.  
  22. class GuidePage extends StatefulWidget {
  23.  
  24.   final Place d;
  25.   GuidePage({Key key,@required this.d}) : super(key: key);
  26.  
  27.   _GuidePageState createState() => _GuidePageState();
  28. }
  29.  
  30. class _GuidePageState extends State<GuidePage> {
  31.  
  32.   GoogleMapController mapController;
  33.  
  34.  
  35.   List<Marker> _markers = [];
  36.   Map data = {};
  37.   String distance  = 'O km';
  38.  
  39.   Map<PolylineId, Polyline> polylines = {};
  40.   List<LatLng> polylineCoordinates = [];
  41.   PolylinePoints polylinePoints = PolylinePoints();
  42.  
  43.   Uint8List _sourceIcon;
  44.   Uint8List _destinationIcon;
  45.  
  46.  
  47.  
  48.   Future getData() async {
  49.    await FirebaseFirestore.instance.collection('places')
  50.    .doc(widget.d.timestamp)
  51.    .collection('travel guide')
  52.    .doc(widget.d.timestamp)
  53.    .get().then((DocumentSnapshot snap) {
  54.       setState(() {
  55.         data = snap.data();
  56.       });
  57.      
  58.     });
  59.   }
  60.  
  61.  
  62.  
  63.   Future _setMarkerIcons () async{
  64.     _sourceIcon = await getBytesFromAsset(Config().drivingMarkerIcon, 110);
  65.     _destinationIcon = await getBytesFromAsset(Config().destinationMarkerIcon, 110);
  66.   }
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.   Future addMarker() async {
  75.     List m = [
  76.       Marker(
  77.       markerId: MarkerId(data['startpoint name']),
  78.       position: LatLng(data['startpoint lat'], data['startpoint lng']),
  79.       infoWindow: InfoWindow(title: data['startpoint name']),
  80.       icon: BitmapDescriptor.fromBytes(_sourceIcon)
  81.      
  82.       ),
  83.       Marker(
  84.       markerId: MarkerId(data['endpoint name']),
  85.       position: LatLng(data['endpoint lat'], data['endpoint lng']),
  86.       infoWindow: InfoWindow(title: data['endpoint name']),
  87.       icon: BitmapDescriptor.fromBytes(_destinationIcon)
  88.       )
  89.     ];
  90.     setState(() {
  91.       m.forEach((element) {
  92.         _markers.add(element);
  93.       });
  94.     });
  95.  
  96.    
  97.   }
  98.  
  99.  
  100.  
  101.  
  102.  Future computeDistance() async{
  103.   var p1 = geo.LatLng(data['startpoint lat'], data['startpoint lng']);
  104.   var p2 = geo.LatLng(data['endpoint lat'], data['endpoint lng']);
  105.   double _distance = geo.computeDistanceBetween(p1, p2)/1000;
  106.   setState(() {
  107.     distance = '${_distance.toStringAsFixed(2)} km';
  108.   });
  109.  
  110. }
  111.  
  112.  
  113.  
  114.   Future _getPolyline() async {
  115.     PolylineResult result = await polylinePoints.getRouteBetweenCoordinates(
  116.         Config().mapAPIKey,
  117.         PointLatLng(data['startpoint lat'], data['startpoint lng']),
  118.         PointLatLng(data['endpoint lat'], data['endpoint lng']),
  119.         travelMode: TravelMode.driving,
  120.     );
  121.     if (result.points.isNotEmpty) {
  122.       result.points.forEach((PointLatLng point) {
  123.         polylineCoordinates.add(LatLng(point.latitude, point.longitude));
  124.       });
  125.     }
  126.     _addPolyLine();
  127.   }
  128.  
  129.  
  130.  
  131.   _addPolyLine() {
  132.     PolylineId id = PolylineId("poly");
  133.     Polyline polyline = Polyline(
  134.       polylineId: id,
  135.       color: Color.fromARGB(255, 40, 122, 198),
  136.       points: polylineCoordinates);
  137.     polylines[id] = polyline;
  138.     setState(() {});
  139.   }
  140.  
  141.  
  142.  
  143.  
  144.  
  145.   void animateCamera() {
  146.     mapController.animateCamera(CameraUpdate.newCameraPosition(CameraPosition(
  147.         target: LatLng(data['startpoint lat'], data['startpoint lng']),
  148.         zoom: 8,
  149.         bearing: 120
  150.        
  151.       )));
  152.   }
  153.  
  154.  
  155.  
  156.   void onMapCreated(controller) {
  157.     controller.setMapStyle(MapUtils.mapStyles);
  158.     setState(() {
  159.       mapController = controller;
  160.     });
  161.   }
  162.  
  163.  
  164.  
  165.  
  166.   @override
  167.   void initState(){
  168.     super.initState();
  169.     Future.delayed(Duration(milliseconds: 0))
  170.     .then((value) async{
  171.       context.read<AdsBloc>().initiateAds();
  172.     });
  173.     _setMarkerIcons()
  174.     .then((value) => getData())
  175.     .then((value) => addMarker()).then((value){
  176.       if(data.isNotEmpty) {animateCamera();}
  177.       _getPolyline();
  178.       computeDistance();
  179.     });
  180.    
  181.   }
  182.  
  183.  
  184.  
  185.   Widget panelUI() {
  186.     return Column(
  187.       children: <Widget>[
  188.         Row(
  189.           mainAxisAlignment: MainAxisAlignment.center,
  190.           children: <Widget>[
  191.             Container(
  192.               width: 30,
  193.               height: 5,
  194.               decoration: BoxDecoration(
  195.                   color: Colors.grey[300],
  196.                   borderRadius: BorderRadius.all(Radius.circular(12.0))),
  197.             ),
  198.           ],
  199.         ),
  200.         SizedBox(
  201.           height: 10.0,
  202.         ),
  203.         Row(
  204.           mainAxisAlignment: MainAxisAlignment.center,
  205.           children: <Widget>[
  206.             Text(
  207.               "travel guide",
  208.               style: TextStyle(
  209.                 fontWeight: FontWeight.w600,
  210.                 fontSize: 20,
  211.               ),
  212.             ).tr(),
  213.           ],
  214.         ),
  215.         RichText(
  216.           text: TextSpan(
  217.             style: TextStyle(color: Colors.grey[800], fontSize: 15, fontWeight: FontWeight.normal),
  218.             text: 'estimated cost = '.tr(),
  219.             children: <TextSpan>[
  220.               TextSpan(
  221.                 style: TextStyle(color: Colors.grey[800], fontSize: 18, fontWeight: FontWeight.bold),
  222.                 text: data['price']
  223.               )
  224.             ]
  225.           )),
  226.         RichText(
  227.           text: TextSpan(
  228.             style: TextStyle(color: Colors.grey[800], fontSize: 15, fontWeight: FontWeight.normal),
  229.             text: 'distance = '.tr(),
  230.             children: <TextSpan>[
  231.               TextSpan(
  232.                 style: TextStyle(color: Colors.grey[800], fontSize: 18, fontWeight: FontWeight.bold),
  233.                 text: distance
  234.               )
  235.             ]
  236.           )),
  237.        
  238.        
  239.         Container(
  240.                     margin: EdgeInsets.only(top: 8, bottom: 8),
  241.                     height: 3,
  242.                     width: 170,
  243.                     decoration: BoxDecoration(
  244.                       color: Colors.blueAccent,
  245.                       borderRadius: BorderRadius.circular(40)),
  246.                   ),
  247.  
  248.         Container(
  249.           padding: EdgeInsets.all(15),
  250.           alignment: Alignment.centerLeft,
  251.           child: Column(
  252.             crossAxisAlignment: CrossAxisAlignment.start,
  253.             children: <Widget>[
  254.             Text('steps', style: TextStyle(fontWeight: FontWeight.w600, fontSize: 18),).tr(),
  255.             Container(
  256.                     margin: EdgeInsets.only(top: 8, bottom: 8),
  257.                     height: 3,
  258.                     width: 70,
  259.                     decoration: BoxDecoration(
  260.                       color: Colors.blueAccent,
  261.                       borderRadius: BorderRadius.circular(40)),
  262.                   ),
  263.           ],)
  264.         ),
  265.         Expanded(
  266.           child: data.isEmpty ? Center(child: CircularProgressIndicator(),) :
  267.           ListView.separated(
  268.             padding: EdgeInsets.only(bottom: 10),
  269.             itemCount: data['paths'].length,
  270.  
  271.             itemBuilder: (BuildContext context, int index) {
  272.  
  273.               return Padding(
  274.                 padding: const EdgeInsets.only(left: 15, right: 15),
  275.                 child: Row(
  276.                   children: <Widget>[
  277.                     Column(
  278.                       children: <Widget>[
  279.                         CircleAvatar(radius: 15, child: Text('${index + 1}', style: TextStyle(color: Colors.white),),backgroundColor: ColorList().guideColors[index]),
  280.                         Container(
  281.                           height: 90,
  282.                           width: 2,
  283.                           color: Colors.black12,
  284.                         )
  285.  
  286.                       ],
  287.                     ),
  288.                     SizedBox(width: 15,),
  289.                     Container(
  290.                       child: Expanded(
  291.                     child: Text(
  292.                     data['paths'][index],
  293.                     maxLines: 3,
  294.                     overflow: TextOverflow.ellipsis,
  295.                     style: TextStyle(fontSize: 16, fontWeight: FontWeight.w500,),
  296.                   ),
  297.                       ),
  298.                     )
  299.                   ],
  300.                 ),
  301.               );
  302.             },
  303.             separatorBuilder: (BuildContext context, int index) {
  304.               return SizedBox();
  305.             },
  306.           ),
  307.         ),
  308.       ],
  309.     );
  310.   }
  311.  
  312.  
  313.  
  314.   Widget panelBodyUI(h, w) {
  315.     return Container(
  316.       width: w,
  317.       child: GoogleMap(
  318.        
  319.         initialCameraPosition: Config().initialCameraPosition,
  320.         mapType: MapType.normal,
  321.         onMapCreated: (controller) => onMapCreated(controller),
  322.         markers: Set.from(_markers),
  323.         polylines: Set<Polyline>.of(polylines.values),
  324.         compassEnabled: false,
  325.         myLocationEnabled: false,
  326.         zoomGesturesEnabled: true,
  327.       ),
  328.     );
  329.   }
  330.  
  331.  
  332.  
  333.  
  334.  
  335.   @override
  336.   Widget build(BuildContext context) {
  337.     double w = MediaQuery.of(context).size.width;
  338.     double h = MediaQuery.of(context).size.height;
  339.     return new Scaffold(
  340.         body: SafeArea(
  341.       child: Stack(children: <Widget>[
  342.       SlidingUpPanel(
  343.             minHeight: 125,
  344.             maxHeight: MediaQuery.of(context).size.height * 0.80,
  345.              backdropEnabled: true,
  346.              backdropOpacity: 0.2,
  347.              backdropTapClosesPanel: true,
  348.              isDraggable: true,
  349.              
  350.             borderRadius: BorderRadius.only(
  351.                 topLeft: Radius.circular(15), topRight: Radius.circular(15)),
  352.             boxShadow: <BoxShadow>[
  353.               BoxShadow(
  354.                   color: Colors.grey[400], blurRadius: 4, offset: Offset(1, 0))
  355.             ],
  356.            
  357.             padding: EdgeInsets.only(top: 15, left: 10, bottom: 0, right: 10),
  358.             panel: panelUI(),
  359.             body: panelBodyUI(h, w)),
  360.      
  361.      
  362.      
  363.      
  364.       Positioned(
  365.           top: 15,
  366.           left: 10,
  367.           child: Container(
  368.            
  369.             child: Row(
  370.               children: <Widget>[
  371.                 InkWell(
  372.                   child: Container(
  373.                     height: 45,
  374.                     width: 45,
  375.                     decoration: BoxDecoration(
  376.                         color: Colors.white,
  377.                         shape: BoxShape.circle,
  378.                         boxShadow: <BoxShadow>[
  379.                           BoxShadow(
  380.                               color: Colors.grey[300],
  381.                               blurRadius: 10,
  382.                               offset: Offset(3, 3))
  383.                         ]),
  384.                     child: Icon(Icons.keyboard_backspace),
  385.                   ),
  386.                   onTap: () {
  387.                     Navigator.pop(context);
  388.                   },
  389.                 ),
  390.                 SizedBox(
  391.                   width: 5,
  392.                 ),
  393.                 data.isEmpty
  394.                 ? Container()
  395.                 : Container(
  396.                   width: MediaQuery.of(context).size.width * 0.80,
  397.                  
  398.                   decoration: BoxDecoration(
  399.                       color: Colors.white,
  400.                       borderRadius: BorderRadius.circular(20),
  401.                       border: Border.all(color: Colors.grey, width: 0.5)),
  402.                   child: Padding(
  403.                     padding: const EdgeInsets.only(
  404.                         left: 15, top: 10, bottom: 10, right: 15),
  405.                     child: Text(
  406.                         '${data['startpoint name']} - ${data['endpoint name']}',
  407.                         style: TextStyle(fontSize: 16, fontWeight: FontWeight.w600),
  408.                       ),
  409.                    
  410.                   ),
  411.                 ),
  412.               ],
  413.             ),
  414.           ),
  415.       ),
  416.  
  417.       data.isEmpty && polylines.isEmpty
  418.           ? Align(
  419.             alignment: Alignment.center,
  420.             child: CircularProgressIndicator(),)
  421.           : Container()
  422.     ]),
  423.         ));
  424.   }
  425. }
  426.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement