Skip to content

Commit

Permalink
space uploader image first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
niiks3 committed Jul 24, 2024
1 parent 0568bb2 commit 784110c
Show file tree
Hide file tree
Showing 11 changed files with 421 additions and 23 deletions.
6 changes: 3 additions & 3 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:get/get.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:google_fonts/google_fonts.dart';
Expand All @@ -15,7 +15,7 @@ import 'views/event_space_search_page.dart';
import 'views/event_space_details_screen.dart';
import 'views/events screen/event_detail_screen.dart';
import 'views/event_space_bid_management.dart';
import 'spaceuploader_events_screens/add_event_screen.dart';
import 'spaceuploader_events_screens/add_space_screen.dart'; // Changed to add_space_screen.dart
import 'spaceuploader_events_screens//manage_spaces_screen.dart';
import 'spaceuploader_events_screens//space_uploader_event_analytics.dart';

Expand Down Expand Up @@ -55,7 +55,7 @@ class MyApp extends StatelessWidget {
}),
GetPage(name: '/manage-bids', page: () => const EventSpaceBidManagementScreen()),
GetPage(name: '/space-uploader-login-signup', page: () => const SpaceUploaderLoginSignupView()),
GetPage(name: '/add-event', page: () => AddEventScreen()),
GetPage(name: '/add-space', page: () => AddSpaceScreen()), // Changed to AddSpaceScreen
GetPage(name: '/manage-spaces', page: () => const ManageSpacesScreen()),
GetPage(name: '/space-uploader-event-analytics', page: () => const SpaceUploaderEventAnalyticsScreen()),
],
Expand Down
179 changes: 179 additions & 0 deletions lib/spaceuploader_events_screens/add_space_screen.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:file_picker/file_picker.dart';
import 'dart:html' as html;
import 'dart:typed_data';
import 'dart:io' as io;
import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:get/get.dart';
import 'package:project/views/login_signup_view.dart';

class AddSpaceScreen extends StatefulWidget {
const AddSpaceScreen({super.key});

@override
_AddSpaceScreenState createState() => _AddSpaceScreenState();
}

class _AddSpaceScreenState extends State<AddSpaceScreen> {
final TextEditingController _titleController = TextEditingController();
final TextEditingController _descriptionController = TextEditingController();
final TextEditingController _startingBidController = TextEditingController();
html.File? _imageFile;
String? _imageFileName;
io.File? _nativeImageFile;

Future<void> _pickImage() async {
print('Picking image...');
if (kIsWeb) {
html.FileUploadInputElement uploadInput = html.FileUploadInputElement();
uploadInput.accept = 'image/*';
uploadInput.click();
uploadInput.onChange.listen((e) {
final files = uploadInput.files;
if (files != null && files.isNotEmpty) {
final file = files.first;
final reader = html.FileReader();
reader.readAsArrayBuffer(file);
reader.onLoadEnd.listen((e) {
setState(() {
_imageFile = file;
_imageFileName = file.name;
print('Image selected: $_imageFileName');
});
});
} else {
print('No image selected.');
}
});
} else {
FilePickerResult? result = await FilePicker.platform.pickFiles(
type: FileType.image,
allowMultiple: false,
);

if (result != null) {
setState(() {
_imageFileName = result.files.first.name;
_nativeImageFile = io.File(result.files.first.path!);
print('Image selected: $_imageFileName');
});
} else {
print('No image selected.');
}
}
}

Future<void> _uploadSpace() async {
final user = FirebaseAuth.instance.currentUser;
if (user == null) {
print('No user is signed in.');
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text('Please sign in to upload a space'),
));
Get.to(() => const LoginSignupView());
return;
}

if (_imageFile == null && _nativeImageFile == null) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('Please select an image')));
return;
}

final imageName = DateTime.now().millisecondsSinceEpoch.toString();
final storageRef = FirebaseStorage.instance.ref().child('spaces/$imageName');

try {
String imageUrl;
if (kIsWeb) {
final reader = html.FileReader();
reader.readAsArrayBuffer(_imageFile!);
await reader.onLoadEnd.first;
final data = reader.result as Uint8List;
print('Uploading image data (Web): ${data.length} bytes');
final uploadTask = await storageRef.putData(data);
imageUrl = await uploadTask.ref.getDownloadURL();
print('Image uploaded (Web): $imageUrl');
await _saveSpaceToFirestore(imageUrl);
} else {
print('Uploading image file (Mobile/Desktop)...');
final uploadTask = await storageRef.putFile(_nativeImageFile!);
imageUrl = await uploadTask.ref.getDownloadURL();
print('Image uploaded (Mobile/Desktop): $imageUrl');
await _saveSpaceToFirestore(imageUrl);
}
} catch (e) {
print('Upload failed: $e');
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('Upload failed: $e')));
}
}

Future<void> _saveSpaceToFirestore(String imageUrl) async {
final user = FirebaseAuth.instance.currentUser;
try {
print('Saving space details to Firestore...');
await FirebaseFirestore.instance.collection('spaces').add({
'title': _titleController.text,
'description': _descriptionController.text,
'imageUrl': imageUrl,
'startTime': Timestamp.now(),
'endTime': Timestamp.fromDate(DateTime.now().add(Duration(days: 7))),
'startingBid': double.parse(_startingBidController.text),
'highestBid': {
'bidderId': null,
'amount': 0.0,
},
'createdBy': user!.uid,
});
print('Space details saved to Firestore');
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('Space uploaded successfully')));
Navigator.pop(context);
} catch (e) {
print('Failed to save space details: $e');
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('Failed to save space details: $e')));
}
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Add New Space')),
body: Padding(
padding: EdgeInsets.all(16.0),
child: SingleChildScrollView(
child: Column(
children: [
TextField(
controller: _titleController,
decoration: InputDecoration(labelText: 'Title'),
),
TextField(
controller: _descriptionController,
decoration: InputDecoration(labelText: 'Description'),
),
TextField(
controller: _startingBidController,
decoration: InputDecoration(labelText: 'Starting Bid'),
keyboardType: TextInputType.number,
),
SizedBox(height: 10),
_imageFileName == null
? Text('No image selected.')
: Text(_imageFileName!),
ElevatedButton(
onPressed: _pickImage,
child: Text('Pick Image'),
),
ElevatedButton(
onPressed: _uploadSpace,
child: Text('Upload Space'),
),
],
),
),
),
);
}
}
26 changes: 15 additions & 11 deletions lib/spaceuploader_events_screens/space_uploader_events_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:project/views/event_space_bid_management.dart';
import 'package:project/views/profile_screen.dart';
import 'package:project/spaceuploader_events_screens/add_event_screen.dart';
import 'package:project/spaceuploader_events_screens/space_uploader_event_analytics.dart';
import 'package:project/spaceuploader_events_screens/manage_spaces_screen.dart';
import 'add_space_screen.dart';
import 'manage_spaces_screen.dart';
import 'space_uploader_event_analytics.dart';

class SpaceUploaderEventsScreen extends StatelessWidget {
const SpaceUploaderEventsScreen({super.key});
Expand Down Expand Up @@ -36,7 +36,7 @@ class SpaceUploaderEventsScreen extends StatelessWidget {
icon: const Icon(Icons.arrow_back, color: Colors.white),
onPressed: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) => const ProfileScreen(email: AutofillHints.email)),
MaterialPageRoute(builder: (context) => const ProfileScreen(email: AutofillHints.email)),
);
},
),
Expand All @@ -54,7 +54,10 @@ class SpaceUploaderEventsScreen extends StatelessWidget {
Navigator.push(context,
MaterialPageRoute(builder: (context) => const EventSpaceBidManagementScreen()),
);

// Handle category selection
},

child: Container(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
decoration: BoxDecoration(
Expand All @@ -81,14 +84,14 @@ class SpaceUploaderEventsScreen extends StatelessWidget {
padding: const EdgeInsets.symmetric(horizontal: 20),
children: <Widget>[
EventCard(
date: 'Add Spaces',
title: 'Add a new event',
date: 'Add Space',
title: 'Add a new space',
location: 'Tap to add',
imageUrl: 'https://cdn.pixabay.com/photo/2017/11/24/10/43/ticket-2974645_1280.jpg',
imageUrl: 'https://cdn.pixabay.com/photo/2017/11/24/10/43/ticket-2974645_1280.jpg', // Replace with actual image URL
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => AddEventScreen()),
MaterialPageRoute(builder: (context) => AddSpaceScreen()),
);
},
),
Expand All @@ -97,6 +100,7 @@ class SpaceUploaderEventsScreen extends StatelessWidget {
title: 'Manage your spaces',
location: 'Tap to manage',
imageUrl: 'https://cdn.pixabay.com/photo/2018/05/31/11/54/celebration-3443779_960_720.jpg',
// Replace with actual image URL
onTap: () {
Navigator.push(
context,
Expand All @@ -105,10 +109,10 @@ class SpaceUploaderEventsScreen extends StatelessWidget {
},
),
EventCard(
date: 'Event Analytics',
title: 'View event analytics',
date: 'Space Analytics',
title: 'View space analytics',
location: 'Tap to view analytics',
imageUrl: 'https://cdn.pixabay.com/photo/2023/11/21/17/28/market-analytics-8403845_960_720.png',
imageUrl: 'https://cdn.pixabay.com/photo/2023/11/21/17/28/market-analytics-8403845_960_720.png', // Replace with actual image URL
onTap: () {
Navigator.push(
context,
Expand Down
2 changes: 1 addition & 1 deletion lib/views/space_uploader_profile_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class _SpaceUploaderProfileScreenState extends State<SpaceUploaderProfileScreen>
label: 'Profile',
),
BottomNavigationBarItem(icon: Icon(Icons.event),
label: 'Events',
label: 'My Spaces',
),
BottomNavigationBarItem(
icon: Icon(Icons.attach_money),
Expand Down
4 changes: 4 additions & 0 deletions linux/flutter/generated_plugin_registrant.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@

#include "generated_plugin_registrant.h"

#include <file_selector_linux/file_selector_plugin.h>
#include <url_launcher_linux/url_launcher_plugin.h>

void fl_register_plugins(FlPluginRegistry* registry) {
g_autoptr(FlPluginRegistrar) file_selector_linux_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "FileSelectorPlugin");
file_selector_plugin_register_with_registrar(file_selector_linux_registrar);
g_autoptr(FlPluginRegistrar) url_launcher_linux_registrar =
fl_plugin_registry_get_registrar_for_plugin(registry, "UrlLauncherPlugin");
url_launcher_plugin_register_with_registrar(url_launcher_linux_registrar);
Expand Down
1 change: 1 addition & 0 deletions linux/flutter/generated_plugins.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#

list(APPEND FLUTTER_PLUGIN_LIST
file_selector_linux
url_launcher_linux
)

Expand Down
4 changes: 4 additions & 0 deletions macos/Flutter/GeneratedPluginRegistrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@ import FlutterMacOS
import Foundation

import cloud_firestore
import file_selector_macos
import firebase_auth
import firebase_core
import firebase_storage
import google_sign_in_ios
import path_provider_foundation
import url_launcher_macos

func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
FLTFirebaseFirestorePlugin.register(with: registry.registrar(forPlugin: "FLTFirebaseFirestorePlugin"))
FileSelectorPlugin.register(with: registry.registrar(forPlugin: "FileSelectorPlugin"))
FLTFirebaseAuthPlugin.register(with: registry.registrar(forPlugin: "FLTFirebaseAuthPlugin"))
FLTFirebaseCorePlugin.register(with: registry.registrar(forPlugin: "FLTFirebaseCorePlugin"))
FLTFirebaseStoragePlugin.register(with: registry.registrar(forPlugin: "FLTFirebaseStoragePlugin"))
FLTGoogleSignInPlugin.register(with: registry.registrar(forPlugin: "FLTGoogleSignInPlugin"))
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
UrlLauncherPlugin.register(with: registry.registrar(forPlugin: "UrlLauncherPlugin"))
Expand Down
Loading

0 comments on commit 784110c

Please sign in to comment.