Skip to content

Commit

Permalink
Added NGO's on Map
Browse files Browse the repository at this point in the history
  • Loading branch information
shuence committed Feb 25, 2024
1 parent 3ccec93 commit 9cf6528
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 49 deletions.
91 changes: 89 additions & 2 deletions lib/screens/ngo/ngo_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import 'package:spoonshare/models/users/user.dart';
import 'package:spoonshare/screens/ngo/ngo_home.dart';
import 'package:spoonshare/widgets/bottom_navbar.dart';
import 'package:spoonshare/widgets/custom_text_field.dart';
import 'package:spoonshare/widgets/auto_complete.dart';
import 'package:spoonshare/widgets/snackbar.dart';
import 'package:uuid/uuid.dart';

class NGOFormScreen extends StatefulWidget {
const NGOFormScreen({Key? key}) : super(key: key);
Expand All @@ -35,8 +37,8 @@ class NGOFormScreenState extends State<NGOFormScreen> {
@override
void initState() {
super.initState();
// Fetch user profile data
fetchUserProfileData();
_addressController.addListener(onModify);
}

Future<void> fetchUserProfileData() async {
Expand All @@ -58,7 +60,56 @@ class NGOFormScreenState extends State<NGOFormScreen> {
File? _imageFile;
late double lat;
late double lng;
// final bool _addressSelected = false;
bool _addressSelected = false;

String tokenForSession = "12345";
List<Map<String, dynamic>> listForPlaces = [];
var uuid = const Uuid();

Future<void> makeSuggestions(String input) async {
try {
var suggestions = await PlaceApi.getSuggestions(input);
setState(() {
listForPlaces = suggestions;
});
} catch (e) {
print("Error: $e");
}
}

@override
void dispose() {
_addressController.dispose();
super.dispose();
}

void onModify() {
if (tokenForSession.isEmpty) {
setState(() {
tokenForSession = uuid.v4();
});
}
makeSuggestions(_addressController.text);
}

Future<void> handleListItemTap(int index) async {
String placeId = listForPlaces[index]['place_id'];
var placeDetails = await PlaceApi.getPlaceDetails(placeId);
double selectedLat = placeDetails['geometry']['location']['lat'];
double selectedLng = placeDetails['geometry']['location']['lng'];
String selectedAddress = listForPlaces[index]['description'];
print(selectedAddress);
print(selectedLat);
print(selectedLng);

setState(() {
_addressController.text = selectedAddress;
lat =
selectedLat; // Update the class-level variables with the selected values
lng = selectedLng;
_addressSelected = true; // Set _addressSelected to true
});
}

bool _validateFields() {
return _ngoNameController.text.isNotEmpty &&
Expand All @@ -73,6 +124,8 @@ class NGOFormScreenState extends State<NGOFormScreen> {

@override
Widget build(BuildContext context) {
bool showExpandedList =
_addressController.text.isNotEmpty && !_addressSelected;
return Scaffold(
appBar: AppBar(
title: const Text('NGO Form'),
Expand Down Expand Up @@ -204,6 +257,36 @@ class NGOFormScreenState extends State<NGOFormScreen> {
label: 'Address*',
controller: _addressController,
),
if (showExpandedList)
Container(
height: 200, // Set the height of the suggestions container
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(8),
boxShadow: [
BoxShadow(
color: Colors.grey.withOpacity(0.5),
spreadRadius: 2,
blurRadius: 5,
offset: const Offset(0, 3),
),
],
),
child: ListView.builder(
itemCount: listForPlaces.length,
itemBuilder: (context, index) {
return ListTile(
onTap: () async {
_addressSelected = true;
await handleListItemTap(index);
},
title: Text(
listForPlaces[index]['description'],
),
);
},
),
),
const SizedBox(height: 16),
CustomTextField(
label: 'LinkedIn/Instagram Profile Links*',
Expand Down Expand Up @@ -406,6 +489,9 @@ class NGOFormScreenState extends State<NGOFormScreen> {
_imageFile,
_ngoNameController.text,
);

GeoPoint location = GeoPoint(lat, lng);

await FirebaseFirestore.instance.collection('ngos').add({
'ngoName': _ngoNameController.text,
'mobileNo': _mobileNoController.text,
Expand All @@ -415,6 +501,7 @@ class NGOFormScreenState extends State<NGOFormScreen> {
'incorporationDay': _selectedIncorporationDay,
'description': _decripationController.text,
'address': _addressController.text,
'location': location,
'linkedin': _linkedinController.text,
'verified': false,
});
Expand Down
2 changes: 0 additions & 2 deletions lib/screens/ngo/ngo_information.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ import 'dart:io';

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_storage/firebase_storage.dart' as firebase_storage;
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:spoonshare/models/users/user.dart';
import 'package:spoonshare/screens/ngo/ngo_home.dart';
import 'package:spoonshare/widgets/bottom_navbar.dart';
Expand Down
15 changes: 9 additions & 6 deletions lib/widgets/foodcards/nearby_daily_cards.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,15 @@ class NearbyDailyFoodCard extends StatelessWidget {
children: [
const Icon(Icons.location_on, size: 16),
const SizedBox(width: 4),
Text(
data['venue'],
style: const TextStyle(
fontSize: 16,
fontFamily: 'DM Sans',
fontWeight: FontWeight.w700,
Flexible(
child: Text(
data['venue'],
style: const TextStyle(
fontSize: 16,
fontFamily: 'DM Sans',
fontWeight: FontWeight.w700,
),
textAlign: TextAlign.center,
),
),
const SizedBox(width: 8),
Expand Down
54 changes: 28 additions & 26 deletions lib/widgets/foodcards/nearby_food_cards.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,16 @@ class NearbyFoodCard extends StatelessWidget {
Row(
children: [
const Icon(Icons.location_on, size: 16),
const SizedBox(width: 4),
Text(
data['venue'],
style: const TextStyle(
fontSize: 16,
fontFamily: 'DM Sans',
fontWeight: FontWeight.w700,
const SizedBox(height: 6),
Flexible(
child: Text(
data['venue'],
style: const TextStyle(
fontSize: 16,
fontFamily: 'DM Sans',
fontWeight: FontWeight.w700,
),
textAlign: TextAlign.center,
),
),
const SizedBox(width: 8), // Add some spacing here
Expand Down Expand Up @@ -239,27 +242,26 @@ class NearbyFoodCard extends StatelessWidget {
}

// Function to check if the food item is past its expiration date
bool isPast(Map<String, dynamic> data) {
String toDateString = data['toDate']?.trim() ?? '';
String toTimeString = data['toTime']?.trim() ?? '';
bool isPast(Map<String, dynamic> data) {
String toDateString = data['toDate']?.trim() ?? '';
String toTimeString = data['toTime']?.trim() ?? '';

// Parse to date and time
DateTime toDate = DateFormat('yyyy-MM-dd').parse(toDateString);
DateTime toTime = DateFormat('hh:mm a').parse(toTimeString);
// Parse to date and time
DateTime toDate = DateFormat('yyyy-MM-dd').parse(toDateString);
DateTime toTime = DateFormat('hh:mm a').parse(toTimeString);

// Combine date and time into a single DateTime object
DateTime combinedDateTime = DateTime(
toDate.year,
toDate.month,
toDate.day,
toTime.hour,
toTime.minute,
);

// Format the current date and time
DateTime currentDateTime = DateTime.now();
// Combine date and time into a single DateTime object
DateTime combinedDateTime = DateTime(
toDate.year,
toDate.month,
toDate.day,
toTime.hour,
toTime.minute,
);

return combinedDateTime.isBefore(currentDateTime);
}
// Format the current date and time
DateTime currentDateTime = DateTime.now();

return combinedDateTime.isBefore(currentDateTime);
}
}
16 changes: 9 additions & 7 deletions lib/widgets/foodcards/past_food_cards.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,15 @@ class PastFoodCard extends StatelessWidget {
children: [
const Icon(Icons.location_on, size: 16),
const SizedBox(width: 4),
Text(
data['venue'],
style: const TextStyle(
fontSize: 16,
fontFamily: 'DM Sans',
color: Colors.white,
fontWeight: FontWeight.w700,
Flexible(
child: Text(
data['venue'],
style: const TextStyle(
fontSize: 16,
fontFamily: 'DM Sans',
fontWeight: FontWeight.w700,
),
textAlign: TextAlign.center,
),
),
const SizedBox(width: 8), // Add some spacing here
Expand Down
53 changes: 48 additions & 5 deletions lib/widgets/maps_widget.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import 'dart:async';
import 'dart:ui' as Codec;
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:intl/intl.dart';
import 'package:location/location.dart' as location;
import 'package:permission_handler/permission_handler.dart';
import 'package:spoonshare/screens/admin/all_ngos.dart';
import 'package:spoonshare/screens/fooddetails/food_details.dart';
import 'package:spoonshare/widgets/bottom_navbar.dart';

Expand All @@ -20,6 +20,7 @@ class _MapsWidgetState extends State<MapsWidget> {
late GoogleMapController mapController;
location.LocationData? currentLocation;
late BitmapDescriptor customMarkerIcon;
late BitmapDescriptor customNgoMarkerIcon;

@override
void initState() {
Expand Down Expand Up @@ -58,27 +59,41 @@ class _MapsWidgetState extends State<MapsWidget> {
);
}

void _navigateToNgoDetails(BuildContext context, QueryDocumentSnapshot data) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ngoDetailsPage(ngoDoc: data),
),
);
}

Future<void> _loadCustomMarker() async {
Uint8List markerIcon =
await getBytesFromAsset('assets/images/marker_icon.png', 100);
customMarkerIcon = BitmapDescriptor.fromBytes(markerIcon);

// Load a different marker for NGOs
Uint8List ngoMarkerIcon =
await getBytesFromAsset('assets/images/ngo.png', 100);
customNgoMarkerIcon = BitmapDescriptor.fromBytes(ngoMarkerIcon);
}

Future<List<Marker>> _initializeMap() async {
List<Marker> markers = [];

QuerySnapshot querySnapshot = await FirebaseFirestore.instance
// Add markers for shared food
QuerySnapshot sharedFoodSnapshot = await FirebaseFirestore.instance
.collection('food')
.doc('sharedfood')
.collection('foodData')
.get();

for (var doc in querySnapshot.docs) {
for (var doc in sharedFoodSnapshot.docs) {
double lat = doc['location'].latitude;
double lng = doc['location'].longitude;
String venue = doc['venue'];
bool dailyActive = doc['dailyActive'] ?? true;

bool isVerified = doc['verified'] ?? true;

if (!dailyActive) {
Expand Down Expand Up @@ -115,12 +130,40 @@ class _MapsWidgetState extends State<MapsWidget> {
infoWindow: infoWindow,
icon: customMarkerIcon,
);

markers.add(marker);
}
}
}

// Add markers for NGOs
QuerySnapshot ngoSnapshot = await FirebaseFirestore.instance
.collection('ngos')
.where('verified', isEqualTo: true)
.get();

for (var doc in ngoSnapshot.docs) {
double lat = doc['location'].latitude;
double lng = doc['location'].longitude;
String name = doc['name'];

InfoWindow infoWindow = InfoWindow(
title: name,
snippet: "NGO location",
onTap: () {
_navigateToNgoDetails(context, doc);
},
);

Marker marker = Marker(
markerId: MarkerId('$lat,$lng'),
position: LatLng(lat, lng),
infoWindow: infoWindow,
icon: customNgoMarkerIcon,
);

markers.add(marker);
}

return markers;
}

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: spoonshare
description: "Meals of Grace."
publish_to: 'none'
version: 1.1.0
version: 1.2.0

environment:
sdk: '>=3.2.4 <4.0.0'
Expand Down

0 comments on commit 9cf6528

Please sign in to comment.