import 'package:flutter/material.dart'; import 'package:intl/intl.dart'; import '../../core/utils/date_formatter.dart'; /// Champ de sélection de date avec design moderne et support du thème. /// /// Ce widget fournit un champ de sélection de date cohérent avec le design system, /// utilisant automatiquement les couleurs du thème actif. /// /// **Usage:** /// ```dart /// DatePickerField( /// label: 'Date de début', /// selectedDate: selectedDate, /// onDatePicked: (date) { /// setState(() => selectedDate = date); /// }, /// ) /// ``` class DatePickerField extends StatelessWidget { /// Crée un nouveau [DatePickerField]. /// /// [onDatePicked] La fonction appelée lorsqu'une date est sélectionnée /// [selectedDate] La date actuellement sélectionnée (optionnel) /// [label] Le texte du label (par défaut: 'Sélectionnez une date') /// [firstDate] La première date sélectionnable (par défaut: aujourd'hui) /// [lastDate] La dernière date sélectionnable (par défaut: 2101) /// [initialDate] La date initiale affichée (par défaut: aujourd'hui ou selectedDate) /// [helpText] Texte d'aide affiché dans le picker (optionnel) const DatePickerField({ required this.onDatePicked, super.key, this.selectedDate, this.label = 'Sélectionnez une date', this.firstDate, this.lastDate, this.initialDate, this.helpText, }); /// La date actuellement sélectionnée final DateTime? selectedDate; /// La fonction appelée lorsqu'une date est sélectionnée final Function(DateTime) onDatePicked; /// Le texte du label final String label; /// La première date sélectionnable final DateTime? firstDate; /// La dernière date sélectionnable final DateTime? lastDate; /// La date initiale affichée dans le picker final DateTime? initialDate; /// Texte d'aide affiché dans le picker final String? helpText; @override Widget build(BuildContext context) { final theme = Theme.of(context); final hasDate = selectedDate != null; return InkWell( onTap: () => _showDatePicker(context), borderRadius: BorderRadius.circular(16), child: Container( padding: const EdgeInsets.symmetric(vertical: 16, horizontal: 20), decoration: BoxDecoration( color: theme.colorScheme.surface, borderRadius: BorderRadius.circular(16), border: Border.all( color: hasDate ? theme.colorScheme.primary : theme.colorScheme.outline.withOpacity(0.5), width: hasDate ? 2 : 1, ), ), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ if (hasDate) Text( 'Date sélectionnée', style: theme.textTheme.bodySmall?.copyWith( color: theme.colorScheme.onSurface.withOpacity(0.6), ), ), const SizedBox(height: 4), Text( hasDate ? DateFormatter.formatDateShort(selectedDate!) : label, style: theme.textTheme.bodyLarge?.copyWith( color: hasDate ? theme.colorScheme.onSurface : theme.colorScheme.onSurface.withOpacity(0.6), fontWeight: hasDate ? FontWeight.w600 : FontWeight.normal, ), ), ], ), ), const SizedBox(width: 12), Icon( Icons.calendar_today, color: hasDate ? theme.colorScheme.primary : theme.colorScheme.onSurface.withOpacity(0.6), size: 24, ), ], ), ), ); } /// Affiche le sélecteur de date. Future _showDatePicker(BuildContext context) async { final theme = Theme.of(context); final now = DateTime.now(); final picked = await showDatePicker( context: context, initialDate: initialDate ?? selectedDate ?? now, firstDate: firstDate ?? now, lastDate: lastDate ?? DateTime(2101), helpText: helpText, builder: (context, child) { return Theme( data: theme.copyWith( colorScheme: theme.colorScheme.copyWith( primary: theme.colorScheme.primary, ), ), child: child!, ); }, ); if (picked != null) { onDatePicked(picked); } } }