Files
hum-flutter/lib/views/listings/views/new/upload.dart
2025-12-12 14:31:36 -05:00

152 lines
5.7 KiB
Dart

import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:hum/core/constants/app_text.dart';
import 'package:hum/core/constants/app_theme.dart';
import 'package:hum/services/firebase_functions.dart';
import 'package:hum/views/listings/widgets/widget_listings.dart';
import 'package:phosphor_flutter/phosphor_flutter.dart';
import 'package:image_picker/image_picker.dart';
class ViewListingsNew extends StatefulWidget {
const ViewListingsNew({super.key});
@override
State<ViewListingsNew> createState() => _ViewListingsNewState();
}
class _ViewListingsNewState extends State<ViewListingsNew> {
bool _isUploading = false;
Future<String> _pickAndUploadImage(ImageSource source) async {
String downloadURL = '';
final ImagePicker picker = ImagePicker();
try {
final XFile? image = await picker.pickImage(source: source);
if (image == null) return downloadURL;
setState(() => _isUploading = true);
// Create a reference to the location you want to upload to in firebase
final storageRef = FirebaseStorage.instance.ref().child(
'uploads/${DateTime.now().millisecondsSinceEpoch}.jpg',
);
// Upload the file
final uploadTask = storageRef.putFile(File(image.path));
final snapshot = await uploadTask.whenComplete(() => null);
// Get the download URL
downloadURL = await snapshot.ref.getDownloadURL();
} catch (e) {
print('Error uploading image: $e');
} finally {
if (mounted) {
setState(() => _isUploading = false);
}
}
return downloadURL;
}
@override
Widget build(BuildContext context) {
return CupertinoPageScaffold(
navigationBar: CupertinoNavigationBar(
leading: CupertinoButton(
padding: EdgeInsets.all(0),
child: Text(doneCancel),
onPressed: () {
Navigator.of(context).maybePop();
},
),
),
backgroundColor: CupertinoDynamicColor.resolve(colorBarControl, context),
child: _isUploading
? const Center(child: CupertinoActivityIndicator(radius: 20))
: Center(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.only(top: 40.0, bottom: 20),
child: Stack(
alignment: AlignmentDirectional.center,
children: [
PhosphorIcon(
PhosphorIconsDuotone.camera,
// color: Colors.green,
size: 80.0,
),
Opacity(
opacity: .3,
child: PhosphorIcon(
PhosphorIconsLight.cornersOut,
color: CupertinoColors.activeBlue,
size: 130.0,
),
),
],
),
),
Text(
listingAddTitle,
style: TextStyle(fontSize: 36, fontWeight: FontWeight.bold),
),
Padding(
padding: const EdgeInsets.only(top: 16.0, bottom: 80),
child: Opacity(
opacity: .5,
child: Text(
listingAddMessage,
textAlign: TextAlign.center,
style: TextStyle(fontSize: 18, fontWeight: FontWeight.w500),
),
),
),
Expanded(
child: Column(
spacing: 20,
children: [
WDGListingButton(
icon: PhosphorIconsDuotone.camera,
text: listingTakePhoto,
description: listingTakePhotoDescription,
action: () async {
String imageURL = await _pickAndUploadImage(ImageSource.camera);
if (imageURL.isNotEmpty) {
print(imageURL);
print('^^^^^^^^');
await callHumProcessImage(imageURL);
}
},
),
WDGListingButton(
icon: PhosphorIconsDuotone.image,
iconColor: CupertinoColors.activeGreen,
text: listingChoosePhoto,
description: listingChoosePhotoDescription,
action: () async {
String imageURL = await _pickAndUploadImage(ImageSource.gallery);
if (imageURL.isNotEmpty) {
print(imageURL);
print('^^^^^^^^');
await callHumProcessImage(imageURL);
}
},
),
// WDGListingButton(icon: PhosphorIconsDuotone.cornersOut, action: () {}),
],
),
),
],
),
),
),
);
}
}