import 'package:flutter/material.dart'; import 'package:google_fonts/google_fonts.dart'; import 'dart:math' as math; // Pour la rotation du chevron class ParentRegisterStep2Screen extends StatefulWidget { const ParentRegisterStep2Screen({super.key}); @override State createState() => _ParentRegisterStep2ScreenState(); } class _ParentRegisterStep2ScreenState extends State { final _formKey = GlobalKey(); // TODO: Recevoir les infos du parent 1 pour pré-remplir l'adresse // String? _parent1Address; // String? _parent1PostalCode; // String? _parent1City; bool _addParent2 = false; // Par défaut, on n'ajoute pas le parent 2 bool _sameAddressAsParent1 = false; // Contrôleurs pour les champs du parent 2 final _lastNameController = TextEditingController(); final _firstNameController = TextEditingController(); final _phoneController = TextEditingController(); final _emailController = TextEditingController(); final _passwordController = TextEditingController(); final _confirmPasswordController = TextEditingController(); final _addressController = TextEditingController(); final _postalCodeController = TextEditingController(); final _cityController = TextEditingController(); @override void dispose() { _lastNameController.dispose(); _firstNameController.dispose(); _phoneController.dispose(); _emailController.dispose(); _passwordController.dispose(); _confirmPasswordController.dispose(); _addressController.dispose(); _postalCodeController.dispose(); _cityController.dispose(); super.dispose(); } // Helper pour activer/désactiver tous les champs sauf l'adresse bool get _parent2FieldsEnabled => _addParent2; // Helper pour activer/désactiver les champs d'adresse bool get _addressFieldsEnabled => _addParent2 && !_sameAddressAsParent1; @override Widget build(BuildContext context) { final screenSize = MediaQuery.of(context).size; return Scaffold( body: Stack( children: [ // Fond papier Positioned.fill( child: Image.asset( 'assets/images/paper2.png', fit: BoxFit.cover, repeat: ImageRepeat.repeat, ), ), // Contenu centré Center( child: SingleChildScrollView( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ // Indicateur d'étape Text( 'Étape 2/X', // Mettre à jour le numéro d'étape total style: GoogleFonts.merienda(fontSize: 16, color: Colors.black54), ), const SizedBox(height: 10), // Texte d'instruction Text( 'Renseignez les informations du deuxième parent (optionnel) :', style: GoogleFonts.merienda( fontSize: 24, fontWeight: FontWeight.bold, color: Colors.black87, ), textAlign: TextAlign.center, ), const SizedBox(height: 30), // Carte bleue contenant le formulaire Container( width: screenSize.width * 0.6, padding: const EdgeInsets.symmetric(vertical: 40, horizontal: 50), decoration: const BoxDecoration( // Retour à la décoration image: DecorationImage( image: AssetImage('assets/images/card_blue_h.png'), // Utilisation de l'image horizontale fit: BoxFit.fill, ), ), // Suppression du Stack et Transform.rotate child: Form( key: _formKey, child: SingleChildScrollView( // Le SingleChildScrollView redevient l'enfant direct child: Column( mainAxisSize: MainAxisSize.min, children: [ // --- Interrupteurs sur une ligne --- Row( crossAxisAlignment: CrossAxisAlignment.center, children: [ // Option 1: Ajouter Parent 2 Expanded( child: Row( mainAxisAlignment: MainAxisAlignment.center, children: [ const Icon(Icons.person_add_alt_1, size: 20), const SizedBox(width: 8), Flexible( child: Text( 'Ajouter Parent 2 ?', style: GoogleFonts.merienda(fontWeight: FontWeight.bold), overflow: TextOverflow.ellipsis, ), ), _buildCustomSwitch( value: _addParent2, onChanged: (bool? newValue) { final bool actualValue = newValue ?? false; setState(() { _addParent2 = actualValue; if (!_addParent2) { _formKey.currentState?.reset(); _lastNameController.clear(); _firstNameController.clear(); _phoneController.clear(); _emailController.clear(); _passwordController.clear(); _confirmPasswordController.clear(); _addressController.clear(); _postalCodeController.clear(); _cityController.clear(); _sameAddressAsParent1 = false; } }); }, ), ], ), ), const SizedBox(width: 10), // Option 2: Même Adresse Expanded( child: Row( mainAxisAlignment: MainAxisAlignment.center, children: [ Icon(Icons.home_work_outlined, size: 20, color: _addParent2 ? null : Colors.grey), // Griser l'icône si désactivé const SizedBox(width: 8), Flexible( child: Text( 'Même Adresse ?', style: GoogleFonts.merienda(color: _addParent2 ? null : Colors.grey), // Griser le texte si désactivé overflow: TextOverflow.ellipsis, ), ), _buildCustomSwitch( value: _sameAddressAsParent1, onChanged: _addParent2 ? (bool? newValue) { final bool actualValue = newValue ?? false; setState(() { _sameAddressAsParent1 = actualValue; if (_sameAddressAsParent1) { _addressController.clear(); _postalCodeController.clear(); _cityController.clear(); // TODO: Pré-remplir } }); } : null, ), ], ), ), ], ), const SizedBox(height: 25), // Espacement ajusté après les switchs // --- Champs du Parent 2 (conditionnels) --- // Nom & Prénom Row( children: [ Expanded(child: _buildTextField(_lastNameController, 'Nom', hintText: 'Nom du deuxième parent', enabled: _parent2FieldsEnabled)), const SizedBox(width: 20), Expanded(child: _buildTextField(_firstNameController, 'Prénom', hintText: 'Prénom du deuxième parent', enabled: _parent2FieldsEnabled)), ], ), const SizedBox(height: 20), // Téléphone & Email Row( children: [ Expanded(child: _buildTextField(_phoneController, 'Téléphone', keyboardType: TextInputType.phone, hintText: 'Son numéro de téléphone', enabled: _parent2FieldsEnabled)), const SizedBox(width: 20), Expanded(child: _buildTextField(_emailController, 'Email', keyboardType: TextInputType.emailAddress, hintText: 'Son adresse e-mail', enabled: _parent2FieldsEnabled)), ], ), const SizedBox(height: 20), // Mot de passe Row( children: [ Expanded(child: _buildTextField(_passwordController, 'Mot de passe', obscureText: true, hintText: 'Son mot de passe', enabled: _parent2FieldsEnabled)), const SizedBox(width: 20), Expanded(child: _buildTextField(_confirmPasswordController, 'Confirmation', obscureText: true, hintText: 'Confirmer son mot de passe', enabled: _parent2FieldsEnabled)), ], ), const SizedBox(height: 20), // --- Champs Adresse (conditionnels) --- _buildTextField(_addressController, 'Adresse (Rue)', hintText: 'Son numéro et nom de rue', enabled: _addressFieldsEnabled), const SizedBox(height: 20), Row( children: [ Expanded(flex: 2, child: _buildTextField(_postalCodeController, 'Code Postal', keyboardType: TextInputType.number, hintText: 'Son code postal', enabled: _addressFieldsEnabled)), const SizedBox(width: 20), Expanded(flex: 3, child: _buildTextField(_cityController, 'Ville', hintText: 'Sa ville', enabled: _addressFieldsEnabled)), ], ), ], ), ), ), ), ], ), ), ), // Chevron de navigation gauche (Retour) Positioned( top: screenSize.height / 2 - 20, left: 40, child: IconButton( icon: Transform( alignment: Alignment.center, transform: Matrix4.rotationY(math.pi), child: Image.asset('assets/images/chevron_right.png', height: 40), ), onPressed: () => Navigator.pop(context), // Retour à l'étape 1 tooltip: 'Retour', ), ), // Chevron de navigation droit (Suivant) Positioned( top: screenSize.height / 2 - 20, right: 40, child: IconButton( icon: Image.asset('assets/images/chevron_right.png', height: 40), onPressed: () { // Si on n'ajoute pas de parent 2, on passe directement if (!_addParent2) { // Naviguer vers l'étape 3 (enfants) print('Passer à l\'étape 3 (enfants) - Sans Parent 2'); Navigator.pushNamed(context, '/parent-register/step3'); return; } // Si on ajoute un parent 2 // Valider seulement si on n'utilise PAS la même adresse bool isFormValid = true; // TODO: Remettre la validation quand elle sera prête /* if (!_sameAddressAsParent1) { isFormValid = _formKey.currentState?.validate() ?? false; } */ if (isFormValid) { // TODO: Sauvegarder les données du parent 2 print('Passer à l\'étape 3 (enfants) - Avec Parent 2'); Navigator.pushNamed(context, '/parent-register/step3'); } }, tooltip: 'Suivant', ), ), ], ), ); } // --- NOUVEAU WIDGET --- // Widget pour construire un switch personnalisé avec images Widget _buildCustomSwitch({required bool value, required ValueChanged? onChanged}) { // --- DEBUG --- print("Building Custom Switch with value: $value"); // ------------- const double switchHeight = 25.0; const double switchWidth = 40.0; return InkWell( onTap: onChanged != null ? () => onChanged(!value) : null, child: Opacity( // Griser le switch si désactivé opacity: onChanged != null ? 1.0 : 0.5, child: Image.asset( value ? 'assets/images/switch_on.png' : 'assets/images/switch_off.png', height: switchHeight, width: switchWidth, fit: BoxFit.contain, // Ou BoxFit.fill selon le rendu souhaité ), ), ); } // Widget pour construire les champs de texte (identique à l'étape 1) Widget _buildTextField( TextEditingController controller, String label, { TextInputType? keyboardType, bool obscureText = false, String? hintText, bool enabled = true, }) { return Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( '$label :', style: GoogleFonts.merienda(fontSize: 16, fontWeight: FontWeight.w600, color: Colors.black87), ), const SizedBox(height: 5), Container( height: 50, decoration: const BoxDecoration( image: DecorationImage( image: AssetImage('assets/images/input_field_bg.png'), fit: BoxFit.fill, ), ), child: TextFormField( controller: controller, keyboardType: keyboardType, obscureText: obscureText, enabled: enabled, style: GoogleFonts.merienda(fontSize: 16, color: enabled ? Colors.black87 : Colors.grey), decoration: InputDecoration( border: InputBorder.none, contentPadding: const EdgeInsets.symmetric(horizontal: 15, vertical: 14), hintText: hintText ?? label, hintStyle: GoogleFonts.merienda(fontSize: 16, color: Colors.black38), ), validator: (value) { if (!enabled) return null; // Ne pas valider si désactivé // Le reste de la validation (commentée précédemment) return null; /* if (value == null || value.isEmpty) { return 'Ce champ est obligatoire'; } // TODO: Validations spécifiques if (label == 'Confirmation' && value != _passwordController.text) { return 'Les mots de passe ne correspondent pas'; } return null; */ }, ), ), ], ); } }