Files
afterwork/lib/presentation/widgets/image_preview_picker.dart
2024-11-17 23:00:18 +00:00

119 lines
4.3 KiB
Dart

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
/// `ImagePreviewPicker` est un widget permettant à l'utilisateur de choisir une image depuis la galerie ou de prendre une photo.
/// Ce widget affiche un aperçu de l'image sélectionnée et gère l'interaction pour choisir une nouvelle image.
///
/// Arguments :
/// - `onImagePicked`: Un callback qui renvoie le fichier image sélectionné (ou null si aucune image n'est choisie).
class ImagePreviewPicker extends StatefulWidget {
final void Function(File?) onImagePicked;
const ImagePreviewPicker({Key? key, required this.onImagePicked}) : super(key: key);
@override
_ImagePreviewPickerState createState() => _ImagePreviewPickerState();
}
class _ImagePreviewPickerState extends State<ImagePreviewPicker> {
File? _selectedImageFile;
/// Méthode pour ouvrir le modal de sélection d'image avec une animation.
Future<void> _pickImage() async {
// Log : Ouverture du modal de sélection d'image
debugPrint('Ouverture du modal de sélection d\'image');
final ImagePicker picker = ImagePicker();
// Affichage du modal de sélection d'image
final XFile? pickedFile = await showModalBottomSheet<XFile?>(
context: context,
builder: (BuildContext context) {
return SafeArea(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
ListTile(
leading: const Icon(Icons.camera_alt),
title: const Text('Prendre une photo'),
onTap: () async {
Navigator.pop(context, await picker.pickImage(source: ImageSource.camera));
},
),
ListTile(
leading: const Icon(Icons.photo_library),
title: const Text('Choisir depuis la galerie'),
onTap: () async {
Navigator.pop(context, await picker.pickImage(source: ImageSource.gallery));
},
),
],
),
);
},
);
// Si un fichier est sélectionné, mettez à jour l'état avec l'image choisie
if (pickedFile != null) {
setState(() {
_selectedImageFile = File(pickedFile.path);
widget.onImagePicked(_selectedImageFile); // Passez l'image au parent
// Log : Image sélectionnée
debugPrint('Image sélectionnée : ${_selectedImageFile?.path}');
});
}
}
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: _pickImage, // Ouvre le modal lors du clic
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'Aperçu de l\'image (16:9)',
style: TextStyle(color: Colors.blueGrey),
),
const SizedBox(height: 8),
AnimatedContainer(
duration: const Duration(milliseconds: 300), // Animation douce lors du changement d'image
padding: const EdgeInsets.all(8.0),
decoration: BoxDecoration(
color: Colors.blueGrey.withOpacity(0.1), // Fond légèrement opaque
borderRadius: BorderRadius.circular(12.0), // Bordures arrondies
border: Border.all(
color: _selectedImageFile != null ? Colors.blue : Colors.blueGrey,
width: 2.0, // Bordure visible autour de l'image
),
),
child: AspectRatio(
aspectRatio: 16 / 9, // Maintient l'aspect ratio de l'image
child: _selectedImageFile != null
? ClipRRect(
borderRadius: BorderRadius.circular(10.0),
child: Image.file(
_selectedImageFile!,
fit: BoxFit.cover,
errorBuilder: (context, error, stackTrace) {
return const Center(
child: Icon(Icons.error, color: Colors.red),
);
},
),
)
: const Center(
child: Text(
'Cliquez pour ajouter une image',
style: TextStyle(color: Colors.blueGrey),
),
),
),
),
],
),
);
}
}