Skip to content

Commit

Permalink
#06 Location & Maps
Browse files Browse the repository at this point in the history
  • Loading branch information
omarahmedx14 committed Sep 18, 2021
1 parent 2682f63 commit 286c9da
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 25 deletions.
2 changes: 1 addition & 1 deletion android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ android {
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.example.flutter_maps"
minSdkVersion 16
minSdkVersion 20
targetSdkVersion 30
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
Expand Down
9 changes: 9 additions & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.flutter_maps">

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<application
android:label="flutter_maps"
android:icon="@mipmap/ic_launcher">

<meta-data android:name="com.google.android.geo.API_KEY"
android:value="AIzaSyCOI-DC-BMvtLC87CA_W5VqpiJqCpTEgcM"/>


<activity
android:name=".MainActivity"
android:launchMode="singleTop"
Expand Down
1 change: 1 addition & 0 deletions lib/constnats/strings.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const loginScreen = '/login-screen';
const otpScreen = '/otp-screen';
const mapScreen = '/map-screen';
const googleAPIKey = 'AIzaSyCOI-DC-BMvtLC87CA_W5VqpiJqCpTEgcM';
15 changes: 15 additions & 0 deletions lib/helpers/location_helper.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'package:geolocator/geolocator.dart';

class LocationHelper {

static Future<Position> getCurrentLocation() async {
bool isServiceEnabled = await Geolocator.isLocationServiceEnabled();
if (!isServiceEnabled) {
await Geolocator.requestPermission();
}

return await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.high,
);
}
}
89 changes: 66 additions & 23 deletions lib/presentation/screens/map_screen.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_maps/business_logic/cubit/phone_auth/phone_auth_cubit.dart';
import 'package:flutter_maps/constnats/my_colors.dart';
import 'package:flutter_maps/constnats/strings.dart';
import 'package:flutter_maps/helpers/location_helper.dart';
import 'package:geolocator/geolocator.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';

class MapScreen extends StatefulWidget {
const MapScreen({Key? key}) : super(key: key);
Expand All @@ -13,32 +19,69 @@ class MapScreen extends StatefulWidget {
class _MapScreenState extends State<MapScreen> {
PhoneAuthCubit phoneAuthCubit = PhoneAuthCubit();

static Position? position;
Completer<GoogleMapController> _mapController = Completer();

static final CameraPosition _myCurrentLocationCameraPosition = CameraPosition(
bearing: 0.0,
target: LatLng(position!.latitude, position!.longitude),
tilt: 0.0,
zoom: 17,
);

Future<void> getMyCurrentLocation() async {
position = await LocationHelper.getCurrentLocation().whenComplete(() {
setState(() {});
});
}

@override
initState() {
super.initState();
getMyCurrentLocation();
}

Widget buildMap() {
return GoogleMap(
mapType: MapType.normal,
myLocationEnabled: true,
zoomControlsEnabled: false,
myLocationButtonEnabled: false,
initialCameraPosition: _myCurrentLocationCameraPosition,
onMapCreated: (GoogleMapController controller) {
_mapController.complete(controller);
},
);
}

Future<void> _goToMyCurrentLocation() async {
final GoogleMapController controller = await _mapController.future;
controller.animateCamera(
CameraUpdate.newCameraPosition(_myCurrentLocationCameraPosition));
}

@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Container(
padding: EdgeInsets.all(20),
child: BlocProvider<PhoneAuthCubit>(
create: (context) => phoneAuthCubit,
child: ElevatedButton(
onPressed: () async {
await phoneAuthCubit.logOut();
Navigator.of(context).pushReplacementNamed(loginScreen);
},
child: Text(
'Logout',
style: TextStyle(color: Colors.white, fontSize: 16),
),
style: ElevatedButton.styleFrom(
minimumSize: Size(110, 50),
primary: Colors.black,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(6),
),
),
),
),
body: Stack(
children: [
position != null
? buildMap()
: Center(
child: Container(
child: CircularProgressIndicator(
color: MyColors.blue,
),
),
)
],
),
floatingActionButton: Container(
margin: EdgeInsets.fromLTRB(0, 0, 8, 30),
child: FloatingActionButton(
backgroundColor: MyColors.blue,
onPressed: _goToMyCurrentLocation,
child: Icon(Icons.place, color: Colors.white),
),
),
);
Expand Down
65 changes: 64 additions & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "7.2.0"
flutter_plugin_android_lifecycle:
dependency: transitive
description:
name: flutter_plugin_android_lifecycle
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.3"
flutter_test:
dependency: "direct dev"
description: flutter
Expand All @@ -128,6 +135,55 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
geolocator:
dependency: "direct main"
description:
name: geolocator
url: "https://pub.dartlang.org"
source: hosted
version: "7.6.2"
geolocator_android:
dependency: transitive
description:
name: geolocator_android
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.1"
geolocator_apple:
dependency: transitive
description:
name: geolocator_apple
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
geolocator_platform_interface:
dependency: transitive
description:
name: geolocator_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "2.3.4"
geolocator_web:
dependency: transitive
description:
name: geolocator_web
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.6"
google_maps_flutter:
dependency: "direct main"
description:
name: google_maps_flutter
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.9"
google_maps_flutter_platform_interface:
dependency: transitive
description:
name: google_maps_flutter_platform_interface
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.1"
http_parser:
dependency: transitive
description:
Expand Down Expand Up @@ -224,6 +280,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
stream_transform:
dependency: transitive
description:
name: stream_transform
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
string_scanner:
dependency: transitive
description:
Expand Down Expand Up @@ -261,4 +324,4 @@ packages:
version: "2.1.0"
sdks:
dart: ">=2.12.0 <3.0.0"
flutter: ">=1.16.0"
flutter: ">=2.0.0"
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ dependencies:
flutter:
sdk: flutter
flutter_bloc: ^7.2.0
geolocator: ^7.6.2
google_maps_flutter: ^2.0.9
pin_code_fields: ^7.3.0

dev_dependencies:
Expand Down

0 comments on commit 286c9da

Please sign in to comment.