|
- // lib/presentation/screens/post_refinement/post_refinement_screen.dart
-
- import 'package:flutter/material.dart';
- // --- CORRECTION 1 : IMPORTER LE REPOSITORY ---
- import '../../../repositories/ai_repository.dart';
- import '../../../routes/app_routes.dart';
-
- import '../../widgets/creation_flow_layout.dart';
- import '../post_preview/post_preview_screen.dart';
- // L'import de 'ollama_service.dart' est supprimé.
-
- // --- CORRECTION 2 : DÉFINIR UNE CLASSE D'ARGUMENTS PROPRE ---
- class PostRefinementScreenArguments {
-
- PostRefinementScreenArguments({
- required this.initialText,
- required this.imageBase64,
- required this.aiRepository,
- });
- final String initialText;
- final String imageBase64;
- final AiRepository aiRepository;
- }
-
- class PostRefinementScreen extends StatefulWidget {
-
- const PostRefinementScreen({
- required this.arguments, super.key,
- });
- // Le constructeur attend maintenant la classe d'arguments.
- final PostRefinementScreenArguments arguments;
-
- @override
- State<PostRefinementScreen> createState() => _PostRefinementScreenState();
- }
-
- class _PostRefinementScreenState extends State<PostRefinementScreen> {
- late final TextEditingController _postTextController;
- final TextEditingController _promptController = TextEditingController();
- bool _isImproving = false;
-
- @override
- void initState() {
- super.initState();
- // On initialise le texte depuis les arguments reçus.
- _postTextController = TextEditingController(text: widget.arguments.initialText);
- }
-
- @override
- void dispose() {
- _postTextController.dispose();
- _promptController.dispose();
- super.dispose();
- }
-
- // --- CORRECTION 3 : UTILISER LE REPOSITORY POUR L'AMÉLIORATION ---
- Future<void> _handleImproveWithAI() async {
- final instruction = _promptController.text;
- if (instruction.isEmpty || _isImproving) return;
-
- if (!mounted) return;
- setState(() => _isImproving = true);
-
- try {
- // On appelle la méthode du Repository, qui se chargera de déléguer au bon service.
- final improvedText = await widget.arguments.aiRepository.improvePostText(
- originalText: _postTextController.text,
- userInstruction: instruction,
- );
-
- if (mounted) {
- setState(() {
- _postTextController.text = improvedText;
- _promptController.clear();
- });
- }
- } catch (e) {
- if (mounted) {
- ScaffoldMessenger.of(context).showSnackBar(SnackBar(
- content: Text("Erreur d'amélioration : ${e.toString()}"),
- backgroundColor: Colors.red));
- }
- } finally {
- if (mounted) {
- setState(() => _isImproving = false);
- }
- }
- }
-
- // --- CORRECTION 4 : NAVIGATION PROPRE VERS L'APERÇU FINAL ---
- // --- CORRECTION APPLIQUÉE ICI ---
- void _navigateToPreview() {
- Navigator.pushNamed(
- context,
- AppRoutes.postPreview,
- // Au lieu d'envoyer une Map, on crée l'objet PostPreviewArguments
- // que l'écran suivant attend.
- arguments: PostPreviewArguments(
- imageBase64: widget.arguments.imageBase64,
- text: _postTextController.text,
- aiRepository: widget.arguments.aiRepository,
- ),
- );
- }
-
- @override
- Widget build(BuildContext context) {
- // Le widget build est INCHANGÉ dans sa structure.
- return CreationFlowLayout( // Ajout du layout de flux
- currentStep: 5, // C'est la 6ème étape
- title: '5. Affinage du texte',
- child: Scaffold(
- appBar: AppBar(
- title: const Text('Affiner le post'),
- actions: [
- FilledButton.tonal(
- onPressed: _navigateToPreview,
- child: const Text('Valider'),
- ),
- const SizedBox(width: 16),
- ],
- ),
- body: SingleChildScrollView(
- padding: const EdgeInsets.all(16),
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.stretch,
- children: [
- TextField(
- controller: _postTextController,
- maxLines: 8,
- decoration: const InputDecoration(
- labelText: 'Texte du post',
- border: OutlineInputBorder(),
- alignLabelWithHint: true,
- ),
- ),
- const SizedBox(height: 24),
- const Row(children: [
- Expanded(child: Divider()),
- Padding(
- padding: EdgeInsets.symmetric(horizontal: 8),
- child: Text("Améliorer avec l'IA"),
- ),
- Expanded(child: Divider()),
- ]),
- const SizedBox(height: 16),
- TextField(
- controller: _promptController,
- decoration: const InputDecoration(
- hintText: 'Ex: ajoute plus de détails, rends-le plus fun...',
- border: OutlineInputBorder(),
- ),
- ),
- const SizedBox(height: 16),
- FilledButton.icon(
- onPressed: _handleImproveWithAI,
- icon: _isImproving
- ? const SizedBox(
- width: 20,
- height: 20,
- child: CircularProgressIndicator(
- color: Colors.white, strokeWidth: 2))
- : const Icon(Icons.auto_awesome),
- label: const Text("Lancer l'amélioration"),
- style: FilledButton.styleFrom(
- padding: const EdgeInsets.symmetric(vertical: 16)),
- ),
- ],
- ),
- ),
- ),
- );
- }
- }
|