|
- // lib/presentation/screens/media_picker/media_picker_screen.dart
-
- import 'dart:convert';
- import 'dart:io';
- import 'package:flutter/material.dart';
- import 'package:image_picker/image_picker.dart';
-
- // --- IMPORTS NÉCESSAIRES ---
- import '../../../routes/app_routes.dart';
- import '../../../repositories/ai_repository.dart';
- import '../../../services/image_analysis_service.dart';
- import '../ai_enhancement/ai_enhancement_screen.dart';
-
- class MediaPickerScreen extends StatefulWidget {
- const MediaPickerScreen({super.key});
-
- @override
- State<MediaPickerScreen> createState() => _MediaPickerScreenState();
- }
-
- class _MediaPickerScreenState extends State<MediaPickerScreen> {
- final _picker = ImagePicker();
- XFile? _selectedMedia;
- bool _isAnalyzing = false;
-
- // --- DÉCLARATION CORRECTE DES DÉPENDANCES ---
- // On instancie les dépendances. AiRepository n'a pas besoin d'arguments.
- final AiRepository _aiRepository = AiRepository();
- // --- FIN DE LA CORRECTION ---
-
- /// Déclenche la sélection d'image depuis la source choisie (galerie ou caméra).
- Future<void> _pickImage(ImageSource source) async {
- if (_isAnalyzing) return;
-
- try {
- final file = await _picker.pickImage(source: source, imageQuality: 85, maxWidth: 1280);
- if (file != null) {
- setState(() => _selectedMedia = file);
- _analyzeAndNavigate();
- }
- } catch (e) {
- if (!mounted) return;
- ScaffoldMessenger.of(context).showSnackBar(
- SnackBar(content: Text("Erreur lors de la sélection de l'image: $e")),
- );
- }
- }
-
- /// Fonction qui analyse l'image et navigue vers l'écran d'amélioration.
- Future<void> _analyzeAndNavigate() async {
- if (_selectedMedia == null) return;
-
- setState(() => _isAnalyzing = true);
-
- try {
- final imageFile = File(_selectedMedia!.path);
- final imageBytes = await imageFile.readAsBytes();
- final imageBase64 = base64Encode(imageBytes);
-
- // On utilise la méthode d'analyse directement depuis le repository.
- final ImageAnalysisResult analysisResult = await _aiRepository.analyzeImage(imageBase64);
-
- final String prompt = analysisResult.prompt;
- final List<String> filterIds = analysisResult.filterIds;
-
- if (!mounted) return;
-
- final screenArguments = AiEnhancementScreenArguments(
- image: imageFile,
- initialPrompt: prompt,
- suggestedFilterIds: filterIds,
- aiRepository: _aiRepository,
- );
-
- Navigator.pushNamed(
- context,
- AppRoutes.aiEnhancement,
- arguments: screenArguments,
- );
-
- } catch (e) {
- if (!mounted) return;
- ScaffoldMessenger.of(context).showSnackBar(
- SnackBar(content: Text("Erreur lors de l'analyse de l'image : $e")),
- );
- } finally {
- if (mounted) {
- setState(() => _isAnalyzing = false);
- }
- }
- }
-
- @override
- Widget build(BuildContext context) {
- // Le code du Widget build reste inchangé
- return Scaffold(
- appBar: AppBar(title: const Text('1. Choisir une Image')),
- body: Stack(
- children: [
- SafeArea(
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.stretch,
- children: [
- Expanded(
- child: Center(
- child: Padding(
- padding: const EdgeInsets.all(24),
- child: Column(
- mainAxisSize: MainAxisSize.min,
- crossAxisAlignment: CrossAxisAlignment.stretch,
- children: [
- const Icon(Icons.camera_enhance, size: 80, color: Colors.grey),
- const SizedBox(height: 24),
- Text(
- 'Choisissez une image pour commencer',
- style: Theme.of(context).textTheme.headlineSmall,
- textAlign: TextAlign.center,
- ),
- const SizedBox(height: 48),
- FilledButton.icon(
- onPressed: _isAnalyzing ? null : () => _pickImage(ImageSource.gallery),
- icon: const Icon(Icons.photo_library_outlined),
- label: const Text('Importer depuis la galerie'),
- style: FilledButton.styleFrom(padding: const EdgeInsets.symmetric(vertical: 12)),
- ),
- const SizedBox(height: 12),
- OutlinedButton.icon(
- onPressed: _isAnalyzing ? null : () => _pickImage(ImageSource.camera),
- icon: const Icon(Icons.camera_alt_outlined),
- label: const Text('Prendre une photo'),
- style: OutlinedButton.styleFrom(padding: const EdgeInsets.symmetric(vertical: 12)),
- ),
- ],
- ),
- ),
- ),
- ),
- ],
- ),
- ),
- if (_isAnalyzing)
- Container(
- color: Colors.black.withOpacity(0.5),
- child: const Center(
- child: Column(
- mainAxisSize: MainAxisSize.min,
- children: [
- CircularProgressIndicator(),
- SizedBox(height: 16),
- Text("Analyse de l'image...", style: TextStyle(color: Colors.white, fontSize: 16)),
- ],
- ),
- ),
- ),
- ],
- ),
- );
- }
- }
|