|
|
|
|
@ -33,9 +33,12 @@ class _AddRecipeScreenState extends State<AddRecipeScreen> {
|
|
|
|
|
bool _isFavourite = false;
|
|
|
|
|
List<File> _selectedImages = [];
|
|
|
|
|
List<String> _uploadedImageUrls = [];
|
|
|
|
|
List<File> _selectedVideos = [];
|
|
|
|
|
List<String> _uploadedVideoUrls = [];
|
|
|
|
|
bool _isUploading = false;
|
|
|
|
|
bool _isSaving = false;
|
|
|
|
|
String? _errorMessage;
|
|
|
|
|
RecipeModel? _currentRecipe; // Track current recipe state for immediate UI updates
|
|
|
|
|
|
|
|
|
|
final ImagePicker _imagePicker = ImagePicker();
|
|
|
|
|
RecipeService? _recipeService;
|
|
|
|
|
@ -75,14 +78,19 @@ class _AddRecipeScreenState extends State<AddRecipeScreen> {
|
|
|
|
|
if (widget.recipe == null) return;
|
|
|
|
|
|
|
|
|
|
final recipe = widget.recipe!;
|
|
|
|
|
_currentRecipe = recipe; // Store current recipe state
|
|
|
|
|
_titleController.text = recipe.title;
|
|
|
|
|
_descriptionController.text = recipe.description ?? '';
|
|
|
|
|
_tagsController.text = recipe.tags.join(', ');
|
|
|
|
|
_rating = recipe.rating;
|
|
|
|
|
_isFavourite = recipe.isFavourite;
|
|
|
|
|
_uploadedImageUrls = List.from(recipe.imageUrls);
|
|
|
|
|
_uploadedVideoUrls = List.from(recipe.videoUrls);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Gets the current recipe (either from widget or _currentRecipe state)
|
|
|
|
|
RecipeModel? get _recipe => _currentRecipe ?? widget.recipe;
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void dispose() {
|
|
|
|
|
_titleController.dispose();
|
|
|
|
|
@ -160,8 +168,82 @@ class _AddRecipeScreenState extends State<AddRecipeScreen> {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> _pickVideos() async {
|
|
|
|
|
// Show dialog to choose between camera and gallery
|
|
|
|
|
final ImageSource? source = await showDialog<ImageSource>(
|
|
|
|
|
context: context,
|
|
|
|
|
builder: (BuildContext context) {
|
|
|
|
|
return AlertDialog(
|
|
|
|
|
title: const Text('Select Video Source'),
|
|
|
|
|
content: Column(
|
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
|
children: [
|
|
|
|
|
ListTile(
|
|
|
|
|
leading: const Icon(Icons.videocam),
|
|
|
|
|
title: const Text('Record Video'),
|
|
|
|
|
onTap: () => Navigator.of(context).pop(ImageSource.camera),
|
|
|
|
|
),
|
|
|
|
|
ListTile(
|
|
|
|
|
leading: const Icon(Icons.video_library),
|
|
|
|
|
title: const Text('Choose from Gallery'),
|
|
|
|
|
onTap: () => Navigator.of(context).pop(ImageSource.gallery),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (source == null) return;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
List<XFile> pickedFiles = [];
|
|
|
|
|
|
|
|
|
|
if (source == ImageSource.camera) {
|
|
|
|
|
// Record a single video
|
|
|
|
|
final XFile? pickedFile = await _imagePicker.pickVideo(source: source);
|
|
|
|
|
if (pickedFile != null) {
|
|
|
|
|
pickedFiles = [pickedFile];
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Pick videos from gallery - pickMedia returns a single XFile or null
|
|
|
|
|
// For multiple videos, we'll need to call it multiple times or use a different approach
|
|
|
|
|
// For now, let's use pickVideo with gallery source which should work
|
|
|
|
|
final XFile? pickedFile = await _imagePicker.pickVideo(source: ImageSource.gallery);
|
|
|
|
|
if (pickedFile != null) {
|
|
|
|
|
pickedFiles = [pickedFile];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pickedFiles.isEmpty) return;
|
|
|
|
|
|
|
|
|
|
final newVideos = pickedFiles.map((file) => File(file.path)).toList();
|
|
|
|
|
|
|
|
|
|
setState(() {
|
|
|
|
|
_selectedVideos.addAll(newVideos);
|
|
|
|
|
_isUploading = true;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Auto-upload videos immediately
|
|
|
|
|
await _uploadImages();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
Logger.error('Failed to pick videos', e);
|
|
|
|
|
if (mounted) {
|
|
|
|
|
setState(() {
|
|
|
|
|
_isUploading = false;
|
|
|
|
|
});
|
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
|
SnackBar(
|
|
|
|
|
content: Text('Failed to pick videos: $e'),
|
|
|
|
|
backgroundColor: Colors.red,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> _uploadImages() async {
|
|
|
|
|
if (_selectedImages.isEmpty) return;
|
|
|
|
|
if (_selectedImages.isEmpty && _selectedVideos.isEmpty) return;
|
|
|
|
|
|
|
|
|
|
final mediaService = ServiceLocator.instance.mediaService;
|
|
|
|
|
if (mediaService == null) {
|
|
|
|
|
@ -182,15 +264,18 @@ class _AddRecipeScreenState extends State<AddRecipeScreen> {
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
final List<String> uploadedUrls = [];
|
|
|
|
|
final List<String> uploadedImageUrls = [];
|
|
|
|
|
final List<String> uploadedVideoUrls = [];
|
|
|
|
|
final List<File> failedImages = [];
|
|
|
|
|
final List<File> failedVideos = [];
|
|
|
|
|
|
|
|
|
|
// Upload images
|
|
|
|
|
for (final imageFile in _selectedImages) {
|
|
|
|
|
try {
|
|
|
|
|
final uploadResult = await mediaService.uploadImage(imageFile);
|
|
|
|
|
// uploadResult contains 'id' or 'hash' and 'url'
|
|
|
|
|
final imageUrl = uploadResult['url'] as String? ?? mediaService.getImageUrl(uploadResult['id'] as String? ?? uploadResult['hash'] as String? ?? '');
|
|
|
|
|
uploadedUrls.add(imageUrl);
|
|
|
|
|
uploadedImageUrls.add(imageUrl);
|
|
|
|
|
Logger.info('Image uploaded: $imageUrl');
|
|
|
|
|
} catch (e) {
|
|
|
|
|
Logger.warning('Failed to upload image ${imageFile.path}: $e');
|
|
|
|
|
@ -210,26 +295,56 @@ class _AddRecipeScreenState extends State<AddRecipeScreen> {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Upload videos
|
|
|
|
|
for (final videoFile in _selectedVideos) {
|
|
|
|
|
try {
|
|
|
|
|
final uploadResult = await mediaService.uploadVideo(videoFile);
|
|
|
|
|
// uploadResult contains 'id' or 'hash' and 'url'
|
|
|
|
|
final videoUrl = uploadResult['url'] as String? ?? mediaService.getImageUrl(uploadResult['id'] as String? ?? uploadResult['hash'] as String? ?? '');
|
|
|
|
|
uploadedVideoUrls.add(videoUrl);
|
|
|
|
|
Logger.info('Video uploaded: $videoUrl');
|
|
|
|
|
} catch (e) {
|
|
|
|
|
Logger.warning('Failed to upload video ${videoFile.path}: $e');
|
|
|
|
|
failedVideos.add(videoFile);
|
|
|
|
|
|
|
|
|
|
// Show user-friendly error message
|
|
|
|
|
final errorMessage = _getUploadErrorMessage(e);
|
|
|
|
|
if (mounted) {
|
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
|
SnackBar(
|
|
|
|
|
content: Text('Failed to upload video: $errorMessage'),
|
|
|
|
|
backgroundColor: Colors.red,
|
|
|
|
|
duration: const Duration(seconds: 3),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setState(() {
|
|
|
|
|
_uploadedImageUrls.addAll(uploadedUrls);
|
|
|
|
|
// Keep only failed images in the selected list for retry
|
|
|
|
|
_uploadedImageUrls.addAll(uploadedImageUrls);
|
|
|
|
|
_uploadedVideoUrls.addAll(uploadedVideoUrls);
|
|
|
|
|
// Keep only failed files in the selected list for retry
|
|
|
|
|
_selectedImages = failedImages;
|
|
|
|
|
_selectedVideos = failedVideos;
|
|
|
|
|
_isUploading = false;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (mounted) {
|
|
|
|
|
if (uploadedUrls.isNotEmpty && failedImages.isEmpty) {
|
|
|
|
|
final totalUploaded = uploadedImageUrls.length + uploadedVideoUrls.length;
|
|
|
|
|
final totalFailed = failedImages.length + failedVideos.length;
|
|
|
|
|
if (totalUploaded > 0 && totalFailed == 0) {
|
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
|
SnackBar(
|
|
|
|
|
content: Text('${uploadedUrls.length} image(s) uploaded successfully'),
|
|
|
|
|
content: Text('$totalUploaded media file(s) uploaded successfully'),
|
|
|
|
|
backgroundColor: Colors.green,
|
|
|
|
|
duration: const Duration(seconds: 1),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
} else if (uploadedUrls.isNotEmpty && failedImages.isNotEmpty) {
|
|
|
|
|
} else if (totalUploaded > 0 && totalFailed > 0) {
|
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
|
SnackBar(
|
|
|
|
|
content: Text('${uploadedUrls.length} image(s) uploaded, ${failedImages.length} failed'),
|
|
|
|
|
content: Text('$totalUploaded file(s) uploaded, $totalFailed failed'),
|
|
|
|
|
backgroundColor: Colors.orange,
|
|
|
|
|
duration: const Duration(seconds: 2),
|
|
|
|
|
),
|
|
|
|
|
@ -304,8 +419,8 @@ class _AddRecipeScreenState extends State<AddRecipeScreen> {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Upload any pending images first and wait for completion
|
|
|
|
|
if (_selectedImages.isNotEmpty) {
|
|
|
|
|
// Upload any pending images and videos first and wait for completion
|
|
|
|
|
if (_selectedImages.isNotEmpty || _selectedVideos.isNotEmpty) {
|
|
|
|
|
setState(() {
|
|
|
|
|
_isSaving = true;
|
|
|
|
|
_errorMessage = null;
|
|
|
|
|
@ -314,10 +429,10 @@ class _AddRecipeScreenState extends State<AddRecipeScreen> {
|
|
|
|
|
await _uploadImages();
|
|
|
|
|
|
|
|
|
|
// Check if upload failed
|
|
|
|
|
if (_selectedImages.isNotEmpty) {
|
|
|
|
|
if (_selectedImages.isNotEmpty || _selectedVideos.isNotEmpty) {
|
|
|
|
|
setState(() {
|
|
|
|
|
_isSaving = false;
|
|
|
|
|
_errorMessage = 'Some images failed to upload. Please try again.';
|
|
|
|
|
_errorMessage = 'Some media files failed to upload. Please try again.';
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
@ -351,6 +466,7 @@ class _AddRecipeScreenState extends State<AddRecipeScreen> {
|
|
|
|
|
rating: _rating,
|
|
|
|
|
isFavourite: _isFavourite,
|
|
|
|
|
imageUrls: _uploadedImageUrls,
|
|
|
|
|
videoUrls: _uploadedVideoUrls,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (widget.recipe != null) {
|
|
|
|
|
@ -392,6 +508,12 @@ class _AddRecipeScreenState extends State<AddRecipeScreen> {
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void _removeVideo(int index) {
|
|
|
|
|
setState(() {
|
|
|
|
|
_uploadedVideoUrls.removeAt(index);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> _toggleFavourite() async {
|
|
|
|
|
if (_recipeService == null || widget.recipe == null) return;
|
|
|
|
|
|
|
|
|
|
@ -428,15 +550,16 @@ class _AddRecipeScreenState extends State<AddRecipeScreen> {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> _removeTag(String tag) async {
|
|
|
|
|
if (_recipeService == null || widget.recipe == null) return;
|
|
|
|
|
if (_recipeService == null || _recipe == null) return;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
final updatedTags = List<String>.from(widget.recipe!.tags)..remove(tag);
|
|
|
|
|
final updatedRecipe = widget.recipe!.copyWith(tags: updatedTags);
|
|
|
|
|
final updatedTags = List<String>.from(_recipe!.tags)..remove(tag);
|
|
|
|
|
final updatedRecipe = _recipe!.copyWith(tags: updatedTags);
|
|
|
|
|
await _recipeService!.updateRecipe(updatedRecipe);
|
|
|
|
|
|
|
|
|
|
if (mounted) {
|
|
|
|
|
setState(() {
|
|
|
|
|
_currentRecipe = updatedRecipe; // Update current recipe state immediately
|
|
|
|
|
_tagsController.text = updatedTags.join(', ');
|
|
|
|
|
});
|
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
|
@ -510,12 +633,12 @@ class _AddRecipeScreenState extends State<AddRecipeScreen> {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> _addTag(String tag) async {
|
|
|
|
|
if (_recipeService == null || widget.recipe == null) return;
|
|
|
|
|
if (_recipeService == null || _recipe == null) return;
|
|
|
|
|
|
|
|
|
|
final trimmedTag = tag.trim();
|
|
|
|
|
if (trimmedTag.isEmpty) return;
|
|
|
|
|
|
|
|
|
|
if (widget.recipe!.tags.contains(trimmedTag)) {
|
|
|
|
|
if (_recipe!.tags.contains(trimmedTag)) {
|
|
|
|
|
if (mounted) {
|
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
|
const SnackBar(
|
|
|
|
|
@ -528,12 +651,13 @@ class _AddRecipeScreenState extends State<AddRecipeScreen> {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
final updatedTags = List<String>.from(widget.recipe!.tags)..add(trimmedTag);
|
|
|
|
|
final updatedRecipe = widget.recipe!.copyWith(tags: updatedTags);
|
|
|
|
|
final updatedTags = List<String>.from(_recipe!.tags)..add(trimmedTag);
|
|
|
|
|
final updatedRecipe = _recipe!.copyWith(tags: updatedTags);
|
|
|
|
|
await _recipeService!.updateRecipe(updatedRecipe);
|
|
|
|
|
|
|
|
|
|
if (mounted) {
|
|
|
|
|
setState(() {
|
|
|
|
|
_currentRecipe = updatedRecipe; // Update current recipe state immediately
|
|
|
|
|
_tagsController.text = updatedTags.join(', ');
|
|
|
|
|
});
|
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
|
@ -760,7 +884,7 @@ class _AddRecipeScreenState extends State<AddRecipeScreen> {
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(height: 8),
|
|
|
|
|
if (_isUploading)
|
|
|
|
|
if (_isUploading && _selectedImages.isNotEmpty)
|
|
|
|
|
const Padding(
|
|
|
|
|
padding: EdgeInsets.all(8),
|
|
|
|
|
child: Row(
|
|
|
|
|
@ -816,6 +940,123 @@ class _AddRecipeScreenState extends State<AddRecipeScreen> {
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(height: 16),
|
|
|
|
|
|
|
|
|
|
// Videos section
|
|
|
|
|
Card(
|
|
|
|
|
child: Padding(
|
|
|
|
|
padding: const EdgeInsets.all(16),
|
|
|
|
|
child: Column(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
|
children: [
|
|
|
|
|
Row(
|
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
|
|
|
children: [
|
|
|
|
|
const Text(
|
|
|
|
|
'Videos',
|
|
|
|
|
style: TextStyle(
|
|
|
|
|
fontSize: 16,
|
|
|
|
|
fontWeight: FontWeight.bold,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
if (!widget.viewMode)
|
|
|
|
|
ElevatedButton.icon(
|
|
|
|
|
onPressed: _isUploading ? null : _pickVideos,
|
|
|
|
|
icon: const Icon(Icons.videocam),
|
|
|
|
|
label: const Text('Add Videos'),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(height: 8),
|
|
|
|
|
if (_isUploading && _selectedVideos.isNotEmpty)
|
|
|
|
|
const Padding(
|
|
|
|
|
padding: EdgeInsets.all(8),
|
|
|
|
|
child: Row(
|
|
|
|
|
children: [
|
|
|
|
|
SizedBox(
|
|
|
|
|
width: 20,
|
|
|
|
|
height: 20,
|
|
|
|
|
child: CircularProgressIndicator(strokeWidth: 2),
|
|
|
|
|
),
|
|
|
|
|
SizedBox(width: 8),
|
|
|
|
|
Text('Uploading videos...'),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
if (_uploadedVideoUrls.isNotEmpty)
|
|
|
|
|
Wrap(
|
|
|
|
|
spacing: 8,
|
|
|
|
|
runSpacing: 8,
|
|
|
|
|
children: _uploadedVideoUrls.asMap().entries.map((entry) {
|
|
|
|
|
final index = entry.key;
|
|
|
|
|
// Calculate the actual index in the gallery (images first, then videos)
|
|
|
|
|
final galleryIndex = _uploadedImageUrls.length + index;
|
|
|
|
|
return GestureDetector(
|
|
|
|
|
onTap: () {
|
|
|
|
|
Navigator.push(
|
|
|
|
|
context,
|
|
|
|
|
MaterialPageRoute(
|
|
|
|
|
builder: (context) => PhotoGalleryScreen(
|
|
|
|
|
imageUrls: _uploadedImageUrls,
|
|
|
|
|
videoUrls: _uploadedVideoUrls,
|
|
|
|
|
initialIndex: galleryIndex,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
child: Stack(
|
|
|
|
|
children: [
|
|
|
|
|
Container(
|
|
|
|
|
width: 100,
|
|
|
|
|
height: 100,
|
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
|
border: Border.all(color: Colors.grey),
|
|
|
|
|
borderRadius: BorderRadius.circular(8),
|
|
|
|
|
color: Colors.black87,
|
|
|
|
|
),
|
|
|
|
|
child: ClipRRect(
|
|
|
|
|
borderRadius: BorderRadius.circular(8),
|
|
|
|
|
child: Stack(
|
|
|
|
|
alignment: Alignment.center,
|
|
|
|
|
children: [
|
|
|
|
|
const Icon(Icons.play_circle_filled, size: 40, color: Colors.white70),
|
|
|
|
|
Positioned(
|
|
|
|
|
bottom: 4,
|
|
|
|
|
left: 4,
|
|
|
|
|
right: 4,
|
|
|
|
|
child: Text(
|
|
|
|
|
'MP4',
|
|
|
|
|
style: TextStyle(
|
|
|
|
|
color: Colors.white,
|
|
|
|
|
fontSize: 10,
|
|
|
|
|
fontWeight: FontWeight.bold,
|
|
|
|
|
),
|
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
if (!widget.viewMode)
|
|
|
|
|
Positioned(
|
|
|
|
|
top: 4,
|
|
|
|
|
right: 4,
|
|
|
|
|
child: IconButton(
|
|
|
|
|
icon: const Icon(Icons.close, size: 20),
|
|
|
|
|
color: Colors.red,
|
|
|
|
|
onPressed: () => _removeVideo(index),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}).toList(),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(height: 16),
|
|
|
|
|
|
|
|
|
|
// Error message
|
|
|
|
|
if (_errorMessage != null)
|
|
|
|
|
Container(
|
|
|
|
|
@ -952,12 +1193,23 @@ class _AddRecipeScreenState extends State<AddRecipeScreen> {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Widget _buildViewMode(BuildContext context) {
|
|
|
|
|
final imagesToShow = _uploadedImageUrls.take(3).toList();
|
|
|
|
|
// Combine images and videos, show up to 3 media items
|
|
|
|
|
final allMedia = <({String url, bool isVideo})>[];
|
|
|
|
|
for (final url in _uploadedImageUrls.take(3)) {
|
|
|
|
|
allMedia.add((url: url, isVideo: false));
|
|
|
|
|
}
|
|
|
|
|
final remainingSlots = 3 - allMedia.length;
|
|
|
|
|
if (remainingSlots > 0) {
|
|
|
|
|
for (final url in _uploadedVideoUrls.take(remainingSlots)) {
|
|
|
|
|
allMedia.add((url: url, isVideo: true));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
final mediaToShow = allMedia.take(3).toList();
|
|
|
|
|
|
|
|
|
|
return Scaffold(
|
|
|
|
|
body: CustomScrollView(
|
|
|
|
|
slivers: [
|
|
|
|
|
// App bar with images
|
|
|
|
|
// App bar with media
|
|
|
|
|
SliverAppBar(
|
|
|
|
|
expandedHeight: 300,
|
|
|
|
|
pinned: true,
|
|
|
|
|
@ -981,7 +1233,7 @@ class _AddRecipeScreenState extends State<AddRecipeScreen> {
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
flexibleSpace: FlexibleSpaceBar(
|
|
|
|
|
background: imagesToShow.isEmpty
|
|
|
|
|
background: mediaToShow.isEmpty
|
|
|
|
|
? Container(
|
|
|
|
|
color: Colors.grey[200],
|
|
|
|
|
child: const Center(
|
|
|
|
|
@ -992,7 +1244,7 @@ class _AddRecipeScreenState extends State<AddRecipeScreen> {
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
: _buildTiledPhotoLayout(imagesToShow),
|
|
|
|
|
: _buildTiledPhotoLayout(mediaToShow),
|
|
|
|
|
),
|
|
|
|
|
actions: [],
|
|
|
|
|
),
|
|
|
|
|
@ -1043,7 +1295,7 @@ class _AddRecipeScreenState extends State<AddRecipeScreen> {
|
|
|
|
|
spacing: 8,
|
|
|
|
|
runSpacing: 8,
|
|
|
|
|
children: [
|
|
|
|
|
...widget.recipe!.tags.map((tag) {
|
|
|
|
|
...(_recipe?.tags ?? []).map((tag) {
|
|
|
|
|
return Chip(
|
|
|
|
|
label: Text(
|
|
|
|
|
tag,
|
|
|
|
|
@ -1157,51 +1409,81 @@ class _AddRecipeScreenState extends State<AddRecipeScreen> {
|
|
|
|
|
const SizedBox(height: 24),
|
|
|
|
|
],
|
|
|
|
|
|
|
|
|
|
// Remaining photos (smaller)
|
|
|
|
|
if (_uploadedImageUrls.length > 3) ...[
|
|
|
|
|
Text(
|
|
|
|
|
'More Photos (${_uploadedImageUrls.length - 3})',
|
|
|
|
|
style: Theme.of(context).textTheme.titleMedium?.copyWith(
|
|
|
|
|
fontWeight: FontWeight.bold,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(height: 12),
|
|
|
|
|
SizedBox(
|
|
|
|
|
height: 120,
|
|
|
|
|
child: ListView.builder(
|
|
|
|
|
scrollDirection: Axis.horizontal,
|
|
|
|
|
itemCount: _uploadedImageUrls.length - 3,
|
|
|
|
|
itemBuilder: (context, index) {
|
|
|
|
|
final actualIndex = index + 3;
|
|
|
|
|
final imageUrl = _uploadedImageUrls[actualIndex];
|
|
|
|
|
return GestureDetector(
|
|
|
|
|
onTap: () {
|
|
|
|
|
Navigator.push(
|
|
|
|
|
context,
|
|
|
|
|
MaterialPageRoute(
|
|
|
|
|
builder: (context) => PhotoGalleryScreen(
|
|
|
|
|
imageUrls: _uploadedImageUrls,
|
|
|
|
|
initialIndex: actualIndex,
|
|
|
|
|
// Remaining media (smaller) - images and videos beyond the first 3
|
|
|
|
|
Builder(
|
|
|
|
|
builder: (context) {
|
|
|
|
|
final remainingImages = _uploadedImageUrls.length > 3
|
|
|
|
|
? _uploadedImageUrls.skip(3).toList()
|
|
|
|
|
: <String>[];
|
|
|
|
|
final remainingVideos = _uploadedImageUrls.length >= 3
|
|
|
|
|
? _uploadedVideoUrls
|
|
|
|
|
: (_uploadedImageUrls.length + _uploadedVideoUrls.length > 3
|
|
|
|
|
? _uploadedVideoUrls.skip(3 - _uploadedImageUrls.length).toList()
|
|
|
|
|
: <String>[]);
|
|
|
|
|
final totalRemaining = remainingImages.length + remainingVideos.length;
|
|
|
|
|
|
|
|
|
|
if (totalRemaining == 0) {
|
|
|
|
|
return const SizedBox.shrink();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Column(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
|
children: [
|
|
|
|
|
Text(
|
|
|
|
|
'More Media ($totalRemaining)',
|
|
|
|
|
style: Theme.of(context).textTheme.titleMedium?.copyWith(
|
|
|
|
|
fontWeight: FontWeight.bold,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(height: 12),
|
|
|
|
|
SizedBox(
|
|
|
|
|
height: 120,
|
|
|
|
|
child: ListView.builder(
|
|
|
|
|
scrollDirection: Axis.horizontal,
|
|
|
|
|
itemCount: totalRemaining,
|
|
|
|
|
itemBuilder: (context, index) {
|
|
|
|
|
final isVideo = index >= remainingImages.length;
|
|
|
|
|
final actualIndex = isVideo
|
|
|
|
|
? _uploadedImageUrls.length + (index - remainingImages.length)
|
|
|
|
|
: 3 + index;
|
|
|
|
|
final mediaUrl = isVideo
|
|
|
|
|
? remainingVideos[index - remainingImages.length]
|
|
|
|
|
: remainingImages[index];
|
|
|
|
|
|
|
|
|
|
return GestureDetector(
|
|
|
|
|
onTap: () {
|
|
|
|
|
Navigator.push(
|
|
|
|
|
context,
|
|
|
|
|
MaterialPageRoute(
|
|
|
|
|
builder: (context) => PhotoGalleryScreen(
|
|
|
|
|
imageUrls: _uploadedImageUrls,
|
|
|
|
|
videoUrls: _uploadedVideoUrls,
|
|
|
|
|
initialIndex: actualIndex,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
child: Container(
|
|
|
|
|
width: 120,
|
|
|
|
|
margin: EdgeInsets.only(
|
|
|
|
|
right: index < totalRemaining - 1 ? 12 : 0,
|
|
|
|
|
),
|
|
|
|
|
child: ClipRRect(
|
|
|
|
|
borderRadius: BorderRadius.circular(12),
|
|
|
|
|
child: isVideo
|
|
|
|
|
? _buildVideoThumbnailForTile(mediaUrl)
|
|
|
|
|
: _buildImagePreview(mediaUrl),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
child: Container(
|
|
|
|
|
width: 120,
|
|
|
|
|
margin: EdgeInsets.only(
|
|
|
|
|
right: index < _uploadedImageUrls.length - 4 ? 12 : 0,
|
|
|
|
|
),
|
|
|
|
|
child: ClipRRect(
|
|
|
|
|
borderRadius: BorderRadius.circular(12),
|
|
|
|
|
child: _buildImagePreview(imageUrl),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(height: 24),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(height: 24),
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
|
|
|
|
|
// Created date
|
|
|
|
|
if (widget.recipe != null)
|
|
|
|
|
@ -1222,8 +1504,8 @@ class _AddRecipeScreenState extends State<AddRecipeScreen> {
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Widget _buildTiledPhotoLayout(List<String> imagesToShow) {
|
|
|
|
|
final imageCount = imagesToShow.length;
|
|
|
|
|
Widget _buildTiledPhotoLayout(List<({String url, bool isVideo})> mediaToShow) {
|
|
|
|
|
final mediaCount = mediaToShow.length;
|
|
|
|
|
|
|
|
|
|
return LayoutBuilder(
|
|
|
|
|
builder: (context, constraints) {
|
|
|
|
|
@ -1232,17 +1514,17 @@ class _AddRecipeScreenState extends State<AddRecipeScreen> {
|
|
|
|
|
height: constraints.maxHeight,
|
|
|
|
|
child: Row(
|
|
|
|
|
children: [
|
|
|
|
|
if (imageCount == 1)
|
|
|
|
|
// Single image: full width
|
|
|
|
|
if (mediaCount == 1)
|
|
|
|
|
// Single media: full width
|
|
|
|
|
Expanded(
|
|
|
|
|
child: _buildImageTile(imagesToShow[0], 0, showBorder: false),
|
|
|
|
|
child: _buildMediaTile(mediaToShow[0], 0, showBorder: false),
|
|
|
|
|
)
|
|
|
|
|
else if (imageCount == 2)
|
|
|
|
|
// Two images: split 50/50
|
|
|
|
|
...imagesToShow.asMap().entries.map((entry) {
|
|
|
|
|
else if (mediaCount == 2)
|
|
|
|
|
// Two media: split 50/50
|
|
|
|
|
...mediaToShow.asMap().entries.map((entry) {
|
|
|
|
|
final index = entry.key;
|
|
|
|
|
return Expanded(
|
|
|
|
|
child: _buildImageTile(
|
|
|
|
|
child: _buildMediaTile(
|
|
|
|
|
entry.value,
|
|
|
|
|
index,
|
|
|
|
|
showBorder: index == 0,
|
|
|
|
|
@ -1250,31 +1532,31 @@ class _AddRecipeScreenState extends State<AddRecipeScreen> {
|
|
|
|
|
);
|
|
|
|
|
})
|
|
|
|
|
else
|
|
|
|
|
// Three images: one large on left, two stacked on right
|
|
|
|
|
// Three media: one large on left, two stacked on right
|
|
|
|
|
Expanded(
|
|
|
|
|
flex: 2,
|
|
|
|
|
child: _buildImageTile(
|
|
|
|
|
imagesToShow[0],
|
|
|
|
|
child: _buildMediaTile(
|
|
|
|
|
mediaToShow[0],
|
|
|
|
|
0,
|
|
|
|
|
showBorder: false,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
if (imageCount == 3) ...[
|
|
|
|
|
if (mediaCount == 3) ...[
|
|
|
|
|
const SizedBox(width: 2),
|
|
|
|
|
Expanded(
|
|
|
|
|
child: Column(
|
|
|
|
|
children: [
|
|
|
|
|
Expanded(
|
|
|
|
|
child: _buildImageTile(
|
|
|
|
|
imagesToShow[1],
|
|
|
|
|
child: _buildMediaTile(
|
|
|
|
|
mediaToShow[1],
|
|
|
|
|
1,
|
|
|
|
|
showBorder: true,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
const SizedBox(height: 2),
|
|
|
|
|
Expanded(
|
|
|
|
|
child: _buildImageTile(
|
|
|
|
|
imagesToShow[2],
|
|
|
|
|
child: _buildMediaTile(
|
|
|
|
|
mediaToShow[2],
|
|
|
|
|
2,
|
|
|
|
|
showBorder: false,
|
|
|
|
|
),
|
|
|
|
|
@ -1290,7 +1572,12 @@ class _AddRecipeScreenState extends State<AddRecipeScreen> {
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Widget _buildImageTile(String imageUrl, int index, {required bool showBorder}) {
|
|
|
|
|
Widget _buildMediaTile(({String url, bool isVideo}) media, int index, {required bool showBorder}) {
|
|
|
|
|
// Calculate the actual index in the gallery (accounting for images first, then videos)
|
|
|
|
|
final actualIndex = media.isVideo
|
|
|
|
|
? _uploadedImageUrls.length + _uploadedVideoUrls.indexOf(media.url)
|
|
|
|
|
: _uploadedImageUrls.indexOf(media.url);
|
|
|
|
|
|
|
|
|
|
return GestureDetector(
|
|
|
|
|
onTap: () {
|
|
|
|
|
Navigator.push(
|
|
|
|
|
@ -1298,7 +1585,8 @@ class _AddRecipeScreenState extends State<AddRecipeScreen> {
|
|
|
|
|
MaterialPageRoute(
|
|
|
|
|
builder: (context) => PhotoGalleryScreen(
|
|
|
|
|
imageUrls: _uploadedImageUrls,
|
|
|
|
|
initialIndex: index,
|
|
|
|
|
videoUrls: _uploadedVideoUrls,
|
|
|
|
|
initialIndex: actualIndex,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
@ -1314,11 +1602,52 @@ class _AddRecipeScreenState extends State<AddRecipeScreen> {
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
: null,
|
|
|
|
|
child: _buildImagePreviewForTile(imageUrl),
|
|
|
|
|
child: media.isVideo
|
|
|
|
|
? _buildVideoThumbnailForTile(media.url)
|
|
|
|
|
: _buildImagePreviewForTile(media.url),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Widget _buildVideoThumbnailForTile(String videoUrl) {
|
|
|
|
|
return Stack(
|
|
|
|
|
fit: StackFit.expand,
|
|
|
|
|
children: [
|
|
|
|
|
Container(
|
|
|
|
|
color: Colors.black87,
|
|
|
|
|
child: const Center(
|
|
|
|
|
child: Icon(
|
|
|
|
|
Icons.play_circle_filled,
|
|
|
|
|
size: 60,
|
|
|
|
|
color: Colors.white70,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
Positioned(
|
|
|
|
|
bottom: 8,
|
|
|
|
|
left: 8,
|
|
|
|
|
right: 8,
|
|
|
|
|
child: Container(
|
|
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 3),
|
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
|
color: Colors.black.withValues(alpha: 0.7),
|
|
|
|
|
borderRadius: BorderRadius.circular(4),
|
|
|
|
|
),
|
|
|
|
|
child: const Text(
|
|
|
|
|
'MP4',
|
|
|
|
|
style: TextStyle(
|
|
|
|
|
color: Colors.white,
|
|
|
|
|
fontSize: 12,
|
|
|
|
|
fontWeight: FontWeight.bold,
|
|
|
|
|
),
|
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Builds an image preview specifically for tiled layouts (ensures proper fit).
|
|
|
|
|
Widget _buildImagePreviewForTile(String imageUrl) {
|
|
|
|
|
final mediaService = ServiceLocator.instance.mediaService;
|
|
|
|
|
|