1
+ import 'dart:async' ;
2
+
1
3
import 'package:flutter/material.dart' ;
2
4
import 'package:flutter_uber_location/navigation_screen.dart' ;
5
+ import 'package:geocoder2/geocoder2.dart' ;
6
+ import 'package:google_maps_flutter/google_maps_flutter.dart' ;
7
+ import 'package:location/location.dart' ;
8
+ import 'package:location/location.dart' as loc;
3
9
4
10
main () {
5
11
runApp (MaterialApp (
@@ -13,57 +19,140 @@ class MyApp extends StatefulWidget {
13
19
}
14
20
15
21
class _MyAppState extends State <MyApp > {
16
- TextEditingController latController = TextEditingController ();
17
- TextEditingController lngController = TextEditingController ();
22
+ LatLng ? destLocation = LatLng (40.7128 , - 74.0060 );
23
+ Location location = Location ();
24
+ loc.LocationData ? _currentPosition;
25
+ final Completer <GoogleMapController ?> _controller = Completer ();
26
+ String ? _address;
27
+
28
+ @override
29
+ void initState () {
30
+ // TODO: implement initState
31
+ super .initState ();
32
+ getCurrentLocation ();
33
+ }
34
+
18
35
@override
19
36
Widget build (BuildContext context) {
20
37
return Scaffold (
21
38
appBar: AppBar (
22
39
title: Text ('Flutter uber' ),
23
40
),
24
- body: Padding (
25
- padding: const EdgeInsets .all (18.0 ),
26
- child: Column (mainAxisAlignment: MainAxisAlignment .center, children: [
27
- Text (
28
- 'Enter your location' ,
29
- style: TextStyle (fontSize: 40 ),
30
- ),
31
- SizedBox (
32
- height: 30 ,
33
- ),
34
- TextField (
35
- controller: latController,
36
- decoration: InputDecoration (
37
- border: OutlineInputBorder (),
38
- labelText: 'latitude' ,
41
+ floatingActionButton: FloatingActionButton (
42
+ child: Icon (Icons .navigate_next),
43
+ onPressed: () {
44
+ Navigator .of (context).pushAndRemoveUntil (
45
+ MaterialPageRoute (
46
+ builder: (context) => NavigationScreen (
47
+ destLocation! .latitude, destLocation! .longitude),
48
+ ),
49
+ (route) => false );
50
+ }),
51
+ body: Stack (
52
+ children: [
53
+ GoogleMap (
54
+
55
+ zoomControlsEnabled: false ,
56
+ initialCameraPosition: CameraPosition (
57
+ target: destLocation! ,
58
+ zoom: 16 ,
39
59
),
60
+ onCameraMove: (CameraPosition ? position) {
61
+ if (destLocation != position! .target) {
62
+ setState (() {
63
+ destLocation = position.target;
64
+ });
65
+ }
66
+ },
67
+ onCameraIdle: () {
68
+ print ('camera idle' );
69
+ getAddressFromLatLng ();
70
+ },
71
+ onTap: (latLng) {
72
+ print (latLng);
73
+ },
74
+ onMapCreated: (GoogleMapController controller) {
75
+ _controller.complete (controller);
76
+ },
40
77
),
41
- SizedBox (
42
- height: 20 ,
43
- ),
44
- TextField (
45
- controller: lngController,
46
- decoration: InputDecoration (
47
- border: OutlineInputBorder (),
48
- labelText: 'longitute' ,
78
+ Align (
79
+ alignment: Alignment .center,
80
+ child: Padding (
81
+ padding: const EdgeInsets .only (bottom: 35.0 ),
82
+ child: Image .asset (
83
+ 'images/pick.png' ,
84
+ height: 45 ,
85
+ width: 45 ,
86
+ ),
49
87
),
50
88
),
51
- SizedBox (
52
- height : 20 ,
53
- ) ,
54
- Container (
55
- width : double .infinity,
56
- child : ElevatedButton (
57
- onPressed : () {
58
- Navigator . of (context). push ( MaterialPageRoute (
59
- builder : (context) => NavigationScreen (
60
- double . parse (latController.text ),
61
- double . parse (lngController.text))));
62
- } ,
63
- child : Text ( 'Get Directions' ) ),
89
+ Positioned (
90
+ top : 40 ,
91
+ right : 20 ,
92
+ left : 20 ,
93
+ child : Container (
94
+ decoration : BoxDecoration (
95
+ border : Border . all (color : Colors .black),
96
+ color : Colors .white,
97
+ ),
98
+ padding : EdgeInsets . all ( 20 ),
99
+ child : Text (_address ?? 'Pick your destination address' ,
100
+ overflow : TextOverflow .visible, softWrap : true ) ,
101
+ ),
64
102
),
65
- ]) ,
103
+ ],
66
104
),
67
105
);
68
106
}
107
+
108
+ getAddressFromLatLng () async {
109
+ try {
110
+ GeoData data = await Geocoder2 .getDataFromCoordinates (
111
+ latitude: destLocation! .latitude,
112
+ longitude: destLocation! .longitude,
113
+ googleMapApiKey: "YOUR_API_KEY" );
114
+ setState (() {
115
+ _address = data.address;
116
+ });
117
+ } catch (e) {
118
+ print (e);
119
+ }
120
+ }
121
+
122
+ getCurrentLocation () async {
123
+ bool _serviceEnabled;
124
+ PermissionStatus _permissionGranted;
125
+
126
+ _serviceEnabled = await location.serviceEnabled ();
127
+ final GoogleMapController ? controller = await _controller.future;
128
+
129
+ if (! _serviceEnabled) {
130
+ _serviceEnabled = await location.requestService ();
131
+ if (! _serviceEnabled) {
132
+ return ;
133
+ }
134
+ }
135
+
136
+ _permissionGranted = await location.hasPermission ();
137
+ if (_permissionGranted == PermissionStatus .denied) {
138
+ _permissionGranted = await location.requestPermission ();
139
+ if (_permissionGranted != PermissionStatus .granted) {
140
+ return ;
141
+ }
142
+ }
143
+ if (_permissionGranted == loc.PermissionStatus .granted) {
144
+ location.changeSettings (accuracy: loc.LocationAccuracy .high);
145
+
146
+ _currentPosition = await location.getLocation ();
147
+ controller? .animateCamera (CameraUpdate .newCameraPosition (CameraPosition (
148
+ target:
149
+ LatLng (_currentPosition! .latitude! , _currentPosition! .longitude! ),
150
+ zoom: 16 ,
151
+ )));
152
+ setState (() {
153
+ destLocation =
154
+ LatLng (_currentPosition! .latitude! , _currentPosition! .longitude! );
155
+ });
156
+ }
157
+ }
69
158
}
0 commit comments