summaryrefslogtreecommitdiffstats
path: root/lib/homepage.dart
blob: 4933741e593490340620fda566b6ba5cb3a98fc2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// SPDX-License-Identifier: Apache-2.0


import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_navigation/config.dart';
import 'package:flutter_navigation/kuksa/class-provider.dart';
import 'package:flutter_navigation/kuksa/class.dart';
import 'package:flutter_navigation/map/map-response.dart';
import 'package:flutter_navigation/provider.dart';
import 'package:flutter_navigation/search.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:latlong2/latlong.dart';


import 'map/Show-route.dart';

class MyHomePage extends ConsumerStatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);


  final String title;

  @override
  ConsumerState<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends ConsumerState<MyHomePage> {


  late MapController mapController;
  TextEditingController _destinationController = TextEditingController();



  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    mapController = MapController();
  }

  String ConvertToTime(num duration){
    int hour = (duration/3600).toInt();
    int min = (duration%3600).toInt() ;
    min = (min/60).toInt();
    String mini = min.toString();
    String Hour = hour.toString();
    String time = "$Hour hr $mini min";

    return time;
  }

  @override
  Widget build(BuildContext context) {

    VehicleSignal vehicleSignal = ref.watch(vehicleSignalProvider);
    LatLng center = LatLng(vehicleSignal.currentLatitude, vehicleSignal.currentLongitude);

    _destinationController.text = ref.watch(DestinationAdressProvider);
    final config = ref.read(ConfigStateprovider);



    return Scaffold(

      body: Stack(
        children: [

          FlutterMap(
            mapController: mapController,
            options: MapOptions(
              center: center,
              minZoom: 12,
              zoom: 12,

              maxZoom: 25.0,
              keepAlive: true,
            ),
            layers: [

              TileLayerOptions(
                urlTemplate: "https://api.mapbox.com/styles/v1/hritik3961/cl7hxzrrf002t15o2j2yh14lm/tiles/256/{z}/{x}/{y}@2x?access_token=${config.mapboxAccessToken}",
                additionalOptions: {
                  "access_token": config.mapboxAccessToken,

                },
              ),
              MarkerLayerOptions(
                rotate: true,
                markers: [
                  Marker(
                      point: center,
                      width: 70,
                      height: 70,
                      builder: (context) =>
                      const Icon(

                        Icons.location_pin,
                        size: 40,
                        color: Colors.red,
                      )

                  ),
                ],
              ),

            ],
          ),
          Container(
            padding: EdgeInsets.all(8),

            alignment: Alignment.topLeft,
            child: TextFormField(

              controller: _destinationController,
              decoration: InputDecoration(
                filled: true,
                fillColor: Colors.white,
                border: OutlineInputBorder(),
                icon: Icon(Icons.location_pin,color: Colors.black,),
                hintText: "Choose your destination",


              ),

              onTap: (){
                Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (context) => SearchPage(iscurrent: false),
                  ),
                );
              },
            ),
          ),
        ],
      ),
      floatingActionButton: FloatingActionButton.extended(
        onPressed: () async{


          LatLng current = LatLng(vehicleSignal.currentLatitude, vehicleSignal.currentLongitude);
          LatLng destination = LatLng(vehicleSignal.destinationLatitude,vehicleSignal.destinationLongitude);

          if(destination != LatLng(0,0)){

            Map RouteResponse = await getDirectionsAPIResponse(current,destination,ref);

            if(RouteResponse.isNotEmpty){
              List RouteCoordinates = RouteResponse['geometry']['coordinates'];
              List<LatLng> polyline =[];

              for(int i =0; i<RouteCoordinates.length ;i++){
                polyline.add(LatLng((RouteCoordinates[i][1]).toDouble(),(RouteCoordinates[i][0]).toDouble()));

              }
              ref.read(polylineprovider.notifier).update(polyline);
              Map response = await getAdress(current,ref);
              String curradress = response['features'][0]['place_name'];
              num duration = RouteResponse['duration'];
              String time = ConvertToTime(duration);


              double distanc = ((RouteResponse['distance']).toDouble());
              int distance = (distanc/1000).toInt();


              Navigator.push(
                  context,
                  MaterialPageRoute(
                      builder: (_) =>
                          NavigationHome( polyLine: polyline, CurrAddress: curradress,Duration: time,Distance: distance,)));

            }



          }


        },
        label: const Text('Show Route'),
        backgroundColor: Colors.purple,
        icon: const Icon(Icons.drive_eta_rounded),
      ),


    );
  }
}