import 'dart:io'; import 'package:flutter/material.dart'; import 'package:image_picker/image_picker.dart'; 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 { File? _selectedImageFile; Future _pickImage() async { final ImagePicker picker = ImagePicker(); final XFile? pickedFile = await showModalBottomSheet( context: context, builder: (BuildContext context) { return SafeArea( child: Column( mainAxisSize: MainAxisSize.min, children: [ ListTile( leading: const Icon(Icons.camera_alt), title: const Text('Take a Photo'), onTap: () async { Navigator.pop(context, await picker.pickImage(source: ImageSource.camera)); }, ), ListTile( leading: const Icon(Icons.photo_library), title: const Text('Choose from Gallery'), onTap: () async { Navigator.pop(context, await picker.pickImage(source: ImageSource.gallery)); }, ), ], ), ); }, ); if (pickedFile != null) { setState(() { _selectedImageFile = File(pickedFile.path); widget.onImagePicked(_selectedImageFile); // Pass the picked image to the parent }); } } @override Widget build(BuildContext context) { return GestureDetector( onTap: _pickImage, child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ const Text( 'Aperçu de l\'image (16:9)', style: TextStyle(color: Colors.white70), ), const SizedBox(height: 8), AspectRatio( aspectRatio: 16 / 9, child: Container( decoration: BoxDecoration( color: Colors.black26, borderRadius: BorderRadius.circular(10.0), border: Border.all(color: Colors.white70, width: 1), ), 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.white54), ), ), ), ), ], ), ); } }