Files
petitspas/frontend/lib/widgets/quotidien/couple_selector_bandeau.dart
jmartinandCursor d05a5e22b2 feat(#211): sélecteur couple compact dans le bandeau
Déplace le contexte de garde (parent/AM) à côté du logo et retire le gros bandeau de la colonne Cartes.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-11 10:37:04 +02:00

400 lines
12 KiB
Dart

import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:p_tits_pas/models/couple_garde.dart';
import 'package:p_tits_pas/services/api/api_config.dart';
import 'package:p_tits_pas/widgets/common/auth_network_image.dart';
import 'package:p_tits_pas/widgets/quotidien/carte.dart';
import 'package:p_tits_pas/widgets/quotidien/quotidien_theme.dart';
/// Rôle qui consulte le bandeau : côté parent (enfant | nounou) ou côté AM
/// (enfant | parent(s)). Le composant est le même, seule la lecture change.
/// Ticket #167 (parent) ; #170 (AM) ; #211 (compact dans le bandeau haut).
enum CoupleBandeauMode { parent, assistanteMaternelle }
/// Sélecteur « couple de garde » compact dans le bandeau haut (#211).
///
/// - Plusieurs couples → contrôle unique avec chevron (menu de bascule).
/// - Un seul couple → affichage informatif (pas de chevron, pas de menu).
/// - Aucun couple → état vide discret.
class CoupleSelectorBandeau extends StatelessWidget {
final CoupleBandeauMode mode;
final List<CoupleGarde> couples;
final String? selectedCoupleId;
final ValueChanged<CoupleGarde>? onCoupleSelected;
final bool loading;
final String? errorMessage;
final VoidCallback? onRetry;
static const double height = 40;
static const double avatarSize = 28;
const CoupleSelectorBandeau({
super.key,
this.mode = CoupleBandeauMode.parent,
required this.couples,
this.selectedCoupleId,
this.onCoupleSelected,
this.loading = false,
this.errorMessage,
this.onRetry,
});
CoupleGarde? get _selected {
if (couples.isEmpty) return null;
if (selectedCoupleId != null) {
for (final c in couples) {
if (c.id == selectedCoupleId) return c;
}
}
for (final c in couples) {
if (c.courant) return c;
}
return couples.first;
}
@override
Widget build(BuildContext context) {
return LayoutBuilder(builder: (context, constraints) {
if (loading) return const _CoupleBandeauSkeleton();
if (errorMessage != null) {
return _CoupleBandeauError(message: errorMessage!, onRetry: onRetry);
}
if (couples.isEmpty) return const _CoupleBandeauEmpty();
final selected = _selected!;
final multi = couples.length > 1;
final card = _CoupleCard(
mode: mode,
couple: selected,
showChevron: multi,
colorIndex: couples.indexOf(selected),
);
if (!multi) return card;
return Theme(
data: Theme.of(context).copyWith(
hoverColor: Colors.transparent,
splashColor: Colors.transparent,
highlightColor: Colors.transparent,
focusColor: Colors.transparent,
),
child: PopupMenuButton<String>(
tooltip: 'Changer de garde',
offset: const Offset(0, height + 6),
color: Colors.transparent,
elevation: 0,
constraints: BoxConstraints.tightFor(
width: constraints.maxWidth.clamp(180, 320),
),
padding: EdgeInsets.zero,
onSelected: (id) {
final chosen = couples.firstWhere((c) => c.id == id);
onCoupleSelected?.call(chosen);
},
itemBuilder: (context) => [
for (final c in couples)
if (c.id != selected.id)
PopupMenuItem<String>(
value: c.id,
padding: EdgeInsets.zero,
height: height + 8,
child: Padding(
padding: const EdgeInsets.only(bottom: 6),
child: _CoupleCard(
mode: mode,
couple: c,
showChevron: false,
selected: false,
colorIndex: couples.indexOf(c),
),
),
),
],
child: card,
),
);
});
}
}
/// Carte compacte : enfant | AM/parents (+ chevron si multi).
class _CoupleCard extends StatelessWidget {
final CoupleBandeauMode mode;
final CoupleGarde couple;
final bool showChevron;
final bool selected;
final int colorIndex;
const _CoupleCard({
required this.mode,
required this.couple,
required this.showChevron,
this.selected = true,
required this.colorIndex,
});
static String _parentsLabel(List<CoupleMembre> parents) {
if (parents.isEmpty) return 'Parents';
String prenomOf(CoupleMembre p) {
final prenom = (p.prenom ?? '').trim();
if (prenom.isNotEmpty) return prenom;
return p.displayName(fallback: 'Parent');
}
if (parents.length == 1) return prenomOf(parents.first);
return '${prenomOf(parents[0])} et ${prenomOf(parents[1])}';
}
@override
Widget build(BuildContext context) {
final isParentMode = mode == CoupleBandeauMode.parent;
final Widget right;
if (isParentMode) {
right = _MembreTile(
photoUrl: couple.am?.photoUrl,
name: (couple.am?.prenom != null && couple.am!.prenom!.isNotEmpty)
? couple.am!.prenom!
: (couple.am?.displayName(fallback: 'Nounou') ?? 'Nounou'),
fallbackIcon: Icons.volunteer_activism,
);
} else {
right = Text(
_parentsLabel(couple.parents),
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: GoogleFonts.merienda(
fontSize: 13,
fontWeight: FontWeight.w600,
color: QuotidienTheme.ink,
),
);
}
return Opacity(
opacity: selected ? 1 : 0.92,
child: Carte.bandeau(
assetPath: QuotidienTheme
.bandeauColors[colorIndex % QuotidienTheme.bandeauColors.length],
height: CoupleSelectorBandeau.height,
padding: const EdgeInsets.symmetric(horizontal: 10),
child: Row(
children: [
Expanded(
child: _MembreTile(
photoUrl: couple.enfant.photoUrl,
name: (couple.enfant.prenom != null &&
couple.enfant.prenom!.isNotEmpty)
? couple.enfant.prenom!
: couple.enfant.displayName(fallback: 'Enfant'),
fallbackIcon: Icons.child_care,
),
),
Container(
width: 5,
height: 22,
margin: const EdgeInsets.symmetric(horizontal: 4),
decoration: const BoxDecoration(
image: DecorationImage(
image: AssetImage(QuotidienTheme.pencilLineVerticalAsset),
fit: BoxFit.contain,
),
),
),
Expanded(child: right),
if (showChevron)
const Icon(
Icons.keyboard_arrow_down,
size: 18,
color: QuotidienTheme.ink,
)
else
const SizedBox(width: 4),
],
),
),
);
}
}
/// Photo ronde + prénom (une moitié du couple).
class _MembreTile extends StatelessWidget {
final String? photoUrl;
final String name;
final IconData fallbackIcon;
const _MembreTile({
required this.photoUrl,
required this.name,
required this.fallbackIcon,
});
@override
Widget build(BuildContext context) {
return Row(
children: [
_Avatar(
photoUrl: photoUrl,
fallbackIcon: fallbackIcon,
size: CoupleSelectorBandeau.avatarSize,
),
const SizedBox(width: 6),
Expanded(
child: Text(
name,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: GoogleFonts.merienda(
fontSize: 13,
fontWeight: FontWeight.w600,
color: QuotidienTheme.ink,
),
),
),
],
);
}
}
class _Avatar extends StatelessWidget {
final String? photoUrl;
final IconData fallbackIcon;
final double size;
const _Avatar({
required this.photoUrl,
required this.fallbackIcon,
required this.size,
});
@override
Widget build(BuildContext context) {
final url = ApiConfig.absoluteMediaUrl(photoUrl);
final placeholder = Container(
width: size,
height: size,
color: QuotidienTheme.lavender.withValues(alpha: 0.35),
child: Icon(fallbackIcon, size: size * 0.55, color: QuotidienTheme.ink),
);
return ClipOval(
child: SizedBox(
width: size,
height: size,
child: url.isEmpty
? placeholder
: AuthNetworkImage(
url: url,
width: size,
height: size,
fit: BoxFit.cover,
errorBuilder: (_, __, ___) => placeholder,
),
),
);
}
}
class _CoupleBandeauSkeleton extends StatelessWidget {
const _CoupleBandeauSkeleton();
@override
Widget build(BuildContext context) {
return ColorFiltered(
colorFilter: ColorFilter.mode(
Colors.white.withValues(alpha: 0.5),
BlendMode.lighten,
),
child: Carte.bandeau(
assetPath: QuotidienTheme.bandeauLime,
height: CoupleSelectorBandeau.height,
child: const Center(
child: SizedBox(
width: 18,
height: 18,
child: CircularProgressIndicator(strokeWidth: 2),
),
),
),
);
}
}
class _CoupleBandeauEmpty extends StatelessWidget {
const _CoupleBandeauEmpty();
@override
Widget build(BuildContext context) {
return Carte.bandeau(
assetPath: QuotidienTheme.bandeauLime,
height: CoupleSelectorBandeau.height,
padding: const EdgeInsets.symmetric(horizontal: 10),
child: Row(
children: [
const Icon(Icons.info_outline, size: 18, color: QuotidienTheme.muted),
const SizedBox(width: 8),
Expanded(
child: Text(
'Aucune garde active',
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: GoogleFonts.merriweather(
fontSize: 12,
color: QuotidienTheme.muted,
),
),
),
],
),
);
}
}
class _CoupleBandeauError extends StatelessWidget {
final String message;
final VoidCallback? onRetry;
const _CoupleBandeauError({required this.message, this.onRetry});
@override
Widget build(BuildContext context) {
return ColorFiltered(
colorFilter: ColorFilter.mode(
QuotidienTheme.coral.withValues(alpha: 0.3),
BlendMode.srcATop,
),
child: Carte.bandeau(
assetPath: QuotidienTheme.bandeauLime,
height: CoupleSelectorBandeau.height,
padding: const EdgeInsets.symmetric(horizontal: 8),
child: Row(
children: [
const Icon(Icons.error_outline, size: 18, color: QuotidienTheme.ink),
const SizedBox(width: 6),
Expanded(
child: Text(
message,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: GoogleFonts.merriweather(
fontSize: 11,
color: QuotidienTheme.ink,
),
),
),
if (onRetry != null)
IconButton(
onPressed: onRetry,
icon: const Icon(Icons.refresh, size: 18),
tooltip: 'Réessayer',
padding: EdgeInsets.zero,
constraints: const BoxConstraints(minWidth: 28, minHeight: 28),
),
],
),
),
);
}
}