import 'package:flutter/material.dart'; import 'package:flutter/semantics.dart'; import '../../../ui/theme/app_design_tokens.dart'; import 'chat_stack_column.dart'; import 'chat_stack_disc.dart'; import 'chat_stack_drag.dart'; import 'chat_stack_drag_shift.dart'; import 'chat_stack_entry.dart'; import 'chat_stack_handles.dart'; import 'chat_stack_motion.dart'; import 'chat_stack_pose.dart'; /// The open column's avatars and name pills, each placed at the stack's /// slot and moved by the open motion ([ChatStackPoseTransition]) and by a /// drag ([ChatStackDragShift]), painted in [order]. class ChatStackColumnItems extends StatelessWidget { const ChatStackColumnItems({ super.key, required this.motion, required this.entries, required this.order, required this.labelMax, required this.drag, required this.draggable, required this.onOpen, required this.onAdd, required this.onDragStart, required this.onDragMove, required this.onDragEnd, this.onRemove, this.onReorder, }); final ChatStackMotion motion; final List entries; /// Item indices in paint order, the last on top. final List order; final double labelMax; final ChatStackDrag drag; /// Whether a hold lifts a chat. final bool draggable; final ValueChanged onOpen; final VoidCallback onAdd; final ValueChanged onDragStart; final ValueChanged onDragMove; final ValueChanged onDragEnd; final ValueChanged? onRemove; final void Function(ChatStackEntry entry, int index)? onReorder; @override Widget build(BuildContext context) => Stack( clipBehavior: Clip.none, children: [ for (final i in order) ...[_label(context, i), _item(context, i)], ], ); ChatStackEntry? _entry(int i) => i < entries.length ? entries[i] : null; /// [child] lifted by a hold when [entry] can be dragged. Widget _handle(ChatStackEntry? entry, Widget child) => ChatStackDragHandle( onStart: entry == null || !draggable ? null : () => onDragStart(entry), onMove: onDragMove, onEnd: entry == null ? null : () => onDragEnd(entry), onCancel: drag.cancel, child: child, ); /// [child] where the drag paints [entry]'s item; Add chat never moves. Widget _shift(ChatStackEntry? entry, Widget child, {bool scales = true}) => entry == null ? child : ChatStackDragShift( item: drag.item(entry.paneId), scales: scales, child: child, ); Animation? _lift(ChatStackEntry? entry) => entry != null && entry.paneId == drag.lifted ? drag.lift : null; Widget _item(BuildContext context, int i) { final entry = _entry(i); return PositionedDirectional( key: ValueKey('chat-stack-item-${entry?.paneId ?? 'add'}'), end: 0, bottom: 0, // The transforms are outermost: the boxes under them sit at the // stack's slot, and only a transform passes hits on to where it paints // them. child: ChatStackPoseTransition( progress: motion.move[i], from: motion.from[i], to: motion.target[i], child: _shift( entry, FadeTransition( opacity: motion.fade[i], child: KeyedSubtree( key: ValueKey('chat-stack-button-${entry?.paneId ?? 'add'}'), child: entry == null ? _addButton(context) : _chatButton(context, entry, i), ), ), ), ), ); } Widget _label(BuildContext context, int i) { final entry = _entry(i); return PositionedDirectional( key: ValueKey('chat-stack-label-${entry?.paneId ?? 'add'}'), end: kChatStackSlot + kChatStackLabelGap, bottom: 0, height: kChatStackSlot, child: ChatStackPoseTransition( progress: motion.label[i], from: motion.labelFrom[i], to: motion.target[i], // The pill moves with its avatar but keeps its size, so the gap // between them stays even. child: _shift( entry, scales: false, ExcludeSemantics( child: FadeTransition( opacity: motion.label[i], child: Align( alignment: AlignmentDirectional.centerEnd, widthFactor: 1, child: ConstrainedBox( constraints: BoxConstraints(maxWidth: labelMax), child: _handle( entry, GestureDetector( onTap: entry == null ? onAdd : () => onOpen(entry), child: ChatStackLabel( text: entry?.title ?? ChatStackColumn.addLabel, color: entry?.color(context), emphasized: entry?.current ?? false, lift: _lift(entry), ), ), ), ), ), ), ), ), ), ); } Widget _chatButton(BuildContext context, ChatStackEntry entry, int index) { final remove = onRemove; final reorder = onReorder; final last = entries.length - 1; final ring = entry.current ? entry.color(context) ?? Theme.of(context).colorScheme.primary : null; // Up is toward the top of the column: a later place in the order. final actions = { if (reorder != null && index < last) const CustomSemanticsAction(label: ChatStackColumn.moveUpLabel): () => reorder(entry, index + 1), if (reorder != null && index > 0) const CustomSemanticsAction(label: ChatStackColumn.moveDownLabel): () => reorder(entry, index - 1), if (remove != null) const CustomSemanticsAction(label: ChatStackColumn.removeLabel): () => remove(entry), }; return Semantics( container: true, button: true, selected: entry.current, label: entry.title, hint: entry.current ? 'Open now' : null, onTap: () => onOpen(entry), customSemanticsActions: actions.isEmpty ? null : actions, child: _handle( entry, ChatStackTappable( autofocus: entry.current, onTap: () => onOpen(entry), child: ChatStackDisc( ring: ring, lift: _lift(entry), child: chatStackAvatar(entry), ), ), ), ); } Widget _addButton(BuildContext context) { final scheme = Theme.of(context).colorScheme; final tokens = AppDesignTokens.of(context); return Semantics( container: true, button: true, label: ChatStackColumn.addLabel, onTap: onAdd, child: ChatStackTappable( onTap: onAdd, child: ChatStackDisc( child: DecoratedBox( decoration: ShapeDecoration( color: tokens.flat ? null : scheme.primaryContainer, shape: chatStackShape(tokens).copyWith( side: tokens.flat ? BorderSide(color: scheme.primary.withValues(alpha: .55)) : BorderSide.none, ), ), child: Icon( Icons.add, color: tokens.flat ? scheme.primary : scheme.onPrimaryContainer, ), ), ), ), ); } }