Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Directions functionality, with coloured water sources, and some restyling #28

Merged
merged 16 commits into from
Jun 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Binary file added Assets/images/marker-icon-green.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion lib/Components/Permissions.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'dart:core';
import 'package:flutter/material.dart';
import 'package:location/location.dart';

Location location = Location();
Expand Down
102 changes: 87 additions & 15 deletions lib/Components/bottom_sheet_info.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:flow/constants.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:flutter_svg/svg.dart';
import 'package:flow/Components/flow_shared_preferences.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'dart:convert';
import 'package:location/location.dart';
import 'directions/directions_model.dart';
import 'directions/directions_repository.dart';
import 'flow_maps.dart';
import 'flow_location.dart';

class BottomSheetInfo extends StatefulWidget {
final String bottomSheetID;
final String bottomSheetDescription;
final bool bottomSheetIsTypeTap;
final bool bottomSheetIsFlowing;
final String distance;
final GeoPoint tapLocation;

BottomSheetInfo({
Key key,
Expand All @@ -20,16 +28,24 @@ class BottomSheetInfo extends StatefulWidget {
this.bottomSheetIsTypeTap,
this.bottomSheetIsFlowing,
this.distance,
this.tapLocation,
}) : super(key: key);

@override
_BottomSheetInfoState createState() => _BottomSheetInfoState();
BottomSheetInfoState createState() => BottomSheetInfoState();
}

class _BottomSheetInfoState extends State<BottomSheetInfo> {
class BottomSheetInfoState extends State<BottomSheetInfo> {
String ifIsTypeTap;
String ifIsFlowing;
bool isSaved;
//Directions _info;
LatLng markerLocation;
LatLng currentLocation;
Directions dirInfo;
Widget circIndicator = SizedBox(
width: 2,
);

// ignore: deprecated_member_use
List<FlowSaved> flowList = List<FlowSaved>();
Expand All @@ -40,8 +56,10 @@ class _BottomSheetInfoState extends State<BottomSheetInfo> {

@override
void initState() {
initFlowSharedPreferences();
super.initState();
initFlowSharedPreferences();
getCurrentLocation();
// ifTapIsSaved();
}

///initialising Shared Preferences
Expand All @@ -50,6 +68,15 @@ class _BottomSheetInfoState extends State<BottomSheetInfo> {
loadSPData();
}

getCurrentLocation() async {
final LocationData currentLocData = await getLocation();
currentLocation = LatLng(currentLocData.latitude, currentLocData.longitude);

print('current location data is $currentLocation');

return currentLocation;
}

@override
Widget build(BuildContext context) {
if (widget.bottomSheetIsTypeTap == true) {
Expand All @@ -64,6 +91,11 @@ class _BottomSheetInfoState extends State<BottomSheetInfo> {
ifIsFlowing = 'Not Flowing';
}

markerLocation = LatLng(
widget.tapLocation.latitude.toDouble(),
widget.tapLocation.longitude.toDouble(),
);

return Container(
padding: EdgeInsets.all(20),
// height: MediaQuery.of(context).size.height * .4,
Expand Down Expand Up @@ -130,7 +162,7 @@ class _BottomSheetInfoState extends State<BottomSheetInfo> {
children: [
BodyText(title: 'Approx. Distance:'),
Text(
'N/A m',
' km',
style: TextStyle(
fontWeight: FontWeight.bold,
color: primarycolor,
Expand All @@ -154,10 +186,39 @@ class _BottomSheetInfoState extends State<BottomSheetInfo> {
elevation: 0,
label: Text('Get Directions'),
backgroundColor: primarycolor,
onPressed: () {},
onPressed: () async {
dirInfo = await FlowMaps()
.getDirections(currentLocation, markerLocation);
print(
'marker position is ${widget.tapLocation.latitude.toDouble()} and ${widget.tapLocation.longitude.toDouble()} ');
print(currentLocation);
print(dirInfo.polylinePoints);
setState(() {
circIndicator = CircularProgressIndicator(
backgroundColor: Colors.transparent,
);
});

// Future.delayed(Duration(seconds: 1), () {
// Navigator.pop(context);
// setState(() {});
//
// // Navigator.push(
// // context,
// // MaterialPageRoute(
// // builder: (context) => FlowMaps(
// // directionInfo: dirInfo,
// // ),
// });
Future.delayed(Duration(seconds: 1), () async {
Navigator.pop(context, dirInfo);
});
},
),
SizedBox(width: 40),
checkIfIsSavedFAB(),
SizedBox(width: 10),
circIndicator,
SizedBox(width: 20),
ifTapIsSaved(),
],
),

Expand All @@ -167,20 +228,32 @@ class _BottomSheetInfoState extends State<BottomSheetInfo> {
);
}

// ///Get Directions func
// Future<Directions> getDirections(LatLng origin, LatLng destination) async {
// final directions = await DirectionsRepository()
// .getDirections(origin: origin, destination: destination);
//
// // print(directionInfo);
// // print(directions);
// // setState(() {});
// //return directionInfo;
// return directions;
// }

/// method to check if is saved and return appropriate icon

Widget checkIfIsSavedFAB() {
ifTapIsSaved() {
Widget checkIfSaved;

///loop to go through the entire list and check if this source has been saved or not
if (flowList.isEmpty) {
checkIfSaved = FABToAddTosave();
checkIfSaved = addToSavedFAB();
} else {
for (int i = 0; i < flowList.length; i++) {
if (flowList[i].savedID == widget.bottomSheetID) {
checkIfSaved = FABToRemoveFromSaved();
checkIfSaved = removeFromSavedFAB();
} else {
checkIfSaved = FABToAddTosave();
checkIfSaved = addToSavedFAB();
}
}
}
Expand All @@ -189,7 +262,7 @@ class _BottomSheetInfoState extends State<BottomSheetInfo> {

/// custom FABs to add and remove from saved
// ignore: non_constant_identifier_names
Widget FABToAddTosave() {
Widget addToSavedFAB() {
return FloatingActionButton(
elevation: 0,
backgroundColor: primarycolor.withOpacity(.2),
Expand All @@ -215,7 +288,7 @@ class _BottomSheetInfoState extends State<BottomSheetInfo> {
}

// ignore: non_constant_identifier_names
Widget FABToRemoveFromSaved() {
Widget removeFromSavedFAB() {
return FloatingActionButton(
elevation: 0,
backgroundColor: secondarycolor.withOpacity(.2),
Expand Down Expand Up @@ -268,10 +341,9 @@ class _BottomSheetInfoState extends State<BottomSheetInfo> {
}

void loadSPData() {
List<String> spList = flowSharedPreferences.getStringList('list');
List<String> spList = (flowSharedPreferences.getStringList('list'));
flowList = spList
.map((savedItem) => FlowSaved.fromMap(json.decode(savedItem)))
.toList();
setState(() {});
}
}
54 changes: 54 additions & 0 deletions lib/Components/directions/directions_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import 'package:flutter/foundation.dart';
import 'package:flutter_polyline_points/flutter_polyline_points.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';

class Directions {
final LatLngBounds bounds;
final List<PointLatLng> polylinePoints;
final String totalDistance;
final String totalDuration;

const Directions({
@required this.bounds,
@required this.polylinePoints,
@required this.totalDistance,
@required this.totalDuration,
});
factory Directions.fromMap(Map<String, dynamic> map) {
//check if route is not available
if ((map['routes'] as List).isEmpty) {
print('routes list is empty');
return null;
}

// get route information
final data = Map<String, dynamic>.from(map['routes'][0]);

//bounds
final northeast = data['bounds']['northeast'];
final southwest = data['bounds']['southwest'];
final bounds = LatLngBounds(
southwest: LatLng(southwest['lat'], southwest['lng']),
northeast: LatLng(northeast['lat'], northeast['lng']),
);

///Distance and Duration
String distance = '';
String duration = '';
if ((data['legs'] as List).isNotEmpty) {
final leg = data['legs'][0];
distance = leg['distance']['text'];
duration = leg['duration']['text'];
}

print('distance from direction model $distance');
print('duration from direction model $duration');
return Directions(
bounds: bounds,
polylinePoints:
PolylinePoints().decodePolyline(data['overview_polyline']['points']),
totalDistance: distance,
totalDuration: duration,
);
}
}
59 changes: 59 additions & 0 deletions lib/Components/directions/directions_repository.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import 'package:dio/dio.dart';
import 'package:flow/Components/bottom_sheet_info.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import '.env.dart';
import 'directions_model.dart';

class DirectionsRepository {
static const String _baseUrl =
'https://maps.googleapis.com/maps/api/directions/json?';

final Dio _dio;
Directions directionData;

DirectionsRepository({Dio dio}) : _dio = dio ?? Dio();
Future<Directions> getDirections({
@required LatLng origin,
@required LatLng destination,
}) async {
final response = await _dio.get(
_baseUrl,
queryParameters: {
'origin': '${origin.latitude},${origin.longitude}',
'destination': '${destination.latitude},${destination.longitude}',
'key': googleAPIKey,
},
);
// check if response is successful
if (response.statusCode == 200) {
print('response is successful');
// print('response data from model is ${response.data}');
return Directions.fromMap(response.data);
}
print('an error has occured:');
return response.data;

// print('response from directon api${response.data}');
// return response.data;

// print('response is not successful');
// return null;
}
}

// getCurrentLocation() async {
// /// getting current position
// currentPosition = await location.getLocation();
//
// ///listening for location changes
// location.onLocationChanged.listen(
// (LocationData currentLocation) {
// setState(() {
// currentPosition = currentLocation;
// return currentLocation;
// });
// },
// );
// }
15 changes: 8 additions & 7 deletions lib/Components/flow_bottom_nav_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,24 @@ class _FlowBottomNavBarState extends State<FlowBottomNavBar> {
bottomNavigationBar: BottomNavigationBar(
backgroundColor: Colors.white,
type: BottomNavigationBarType.fixed,
elevation: 15,
elevation: 10,
selectedItemColor: primarycolor,
unselectedItemColor: textcolor.withOpacity(.1),
currentIndex: currentMenuIndex,
onTap: (value) {
currentMenuIndex = value;
setState(() {});
},
showUnselectedLabels: true,
unselectedItemColor: textcolor,
showUnselectedLabels: false,
showSelectedLabels: false,
selectedFontSize: 12,
unselectedFontSize: 12,
items: [
BottomNavigationBarItem(
label: ('Home'),
icon: SvgPicture.asset(
'Assets/icons/svgs/fi-rr-home.svg',
color: textcolor,
color: textcolor.withOpacity(.9),
),
activeIcon: SvgPicture.asset(
'Assets/icons/svgs/fi-rr-home.svg',
Expand All @@ -61,7 +62,7 @@ class _FlowBottomNavBarState extends State<FlowBottomNavBar> {
label: 'Find',
icon: SvgPicture.asset(
'Assets/icons/svgs/fi-rr-search.svg',
color: textcolor,
color: textcolor.withOpacity(.9),
),
activeIcon: SvgPicture.asset(
'Assets/icons/svgs/fi-rr-search.svg',
Expand All @@ -72,7 +73,7 @@ class _FlowBottomNavBarState extends State<FlowBottomNavBar> {
label: 'Saved',
icon: SvgPicture.asset(
'Assets/icons/svgs/fi-rr-heart.svg',
color: textcolor,
color: textcolor.withOpacity(.9),
),
activeIcon: SvgPicture.asset(
'Assets/icons/svgs/fi-rr-heart.svg',
Expand All @@ -83,7 +84,7 @@ class _FlowBottomNavBarState extends State<FlowBottomNavBar> {
label: 'About',
icon: SvgPicture.asset(
'Assets/icons/svgs/fi-rr-user.svg',
color: textcolor,
color: textcolor.withOpacity(.9),
),
activeIcon: SvgPicture.asset(
'Assets/icons/svgs/fi-rr-user.svg',
Expand Down