import 'package:flutter/material.dart'; import 'package:flutter/semantics.dart'; import 'package:flutter/services.dart'; import '../../../ui/theme/app_design_tokens.dart'; import 'chat_stack_disc.dart'; import 'chat_stack_entry.dart'; import 'chat_stack_motion.dart'; import 'chat_stack_pose.dart'; /// The chat stack fanned out: a scrim over the screen and the open chats in /// a column rising from the stack's own slot, each avatar with its name in a /// small pill beside it, and Add chat at the top. /// /// Item 0 sits exactly on the collapsed stack, nearest the thumb. The chats /// keep the open-chat order (the order the server syncs), so a chat keeps /// its place in the column from one visit to the next; the chat on screen /// wears a ring in its colour. Add chat is last, at the top: the reachable /// bottom slots go to the frequent action (switching), and the rarer one /// ends the list where the operator reads "and more". /// /// Nothing here rebuilds per frame: every item moves through /// [ChatStackPoseTransition] and fades through [FadeTransition], both /// repaint-only. class ChatStackColumn extends StatelessWidget { const ChatStackColumn({ super.key, required this.motion, required this.anchor, required this.entries, required this.depths, required this.interactive, required this.onOpen, required this.onAdd, required this.onDismiss, this.onRemove, }); final ChatStackMotion motion; /// The collapsed stack's slot in this overlay's coordinates. final Rect anchor; final List entries; /// Each item's layer in the collapsed pile (null when it is not drawn /// there); the add item last. final List depths; /// False while folding back: taps fall through to nothing. final bool interactive; final ValueChanged onOpen; final VoidCallback onAdd; final VoidCallback onDismiss; /// Long press removes a chat from the open chats; null when it cannot /// (the only open chat). final ValueChanged? onRemove; static const removeLabel = 'Remove from open chats'; static const addLabel = 'Add chat'; @override Widget build(BuildContext context) { final media = MediaQuery.of(context); final scheme = Theme.of(context).colorScheme; final dark = Theme.of(context).brightness == Brightness.dark; final rtl = Directionality.of(context) == TextDirection.rtl; return LayoutBuilder( builder: (context, constraints) { final size = constraints.biggest; final count = motion.count; final height = (count - 1) * kChatStackPitch + kChatStackSlot; final top = media.padding.top + 8; final available = anchor.bottom - top; final fits = height <= available; // The column's own edge is the stack's; the labels take the rest of // the row back to a margin at the other side. final endInset = rtl ? anchor.left : size.width - anchor.right; const margin = 16.0; final width = size.width - endInset - margin; final labelMax = width - kChatStackSlot - kChatStackLabelGap; // Paint the pile's order while it opens: hidden layers first, the // top of the pile last. final order = [for (var i = 0; i < count; i++) i] ..sort((a, b) => (depths[b] ?? 99).compareTo(depths[a] ?? 99)); final items = Stack( clipBehavior: Clip.none, children: [ for (final i in order) ...[ _label(context, i, labelMax), _item(context, i), ], ], ); return CallbackShortcuts( bindings: { const SingleActivator(LogicalKeyboardKey.escape): onDismiss, }, child: FocusScope( autofocus: true, child: IgnorePointer( ignoring: !interactive, child: Stack( children: [ Positioned.fill( child: FadeTransition( opacity: motion.scrim, child: ModalBarrier( color: scheme.scrim.withValues(alpha: dark ? .5 : .32), dismissible: true, onDismiss: onDismiss, semanticsLabel: 'Close chats', ), ), ), PositionedDirectional( end: endInset, bottom: size.height - anchor.bottom, top: top, width: width, child: Align( alignment: AlignmentDirectional.bottomEnd, child: SingleChildScrollView( reverse: true, clipBehavior: fits ? Clip.none : Clip.hardEdge, child: SizedBox( width: width, height: height, child: Semantics( container: true, explicitChildNodes: true, label: 'Open chats', child: items, ), ), ), ), ), ], ), ), ), ); }, ); } Widget _item(BuildContext context, int i) { final entry = i < entries.length ? entries[i] : null; return PositionedDirectional( key: ValueKey('chat-stack-item-${entry?.paneId ?? 'add'}'), end: 0, bottom: 0, // The transform is outermost: the boxes under it 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: FadeTransition( opacity: motion.fade[i], child: KeyedSubtree( key: ValueKey('chat-stack-button-${entry?.paneId ?? 'add'}'), child: entry == null ? _addButton(context) : _chatButton(entry), ), ), ), ); } Widget _label(BuildContext context, int i, double maxWidth) { final entry = i < entries.length ? entries[i] : null; 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], child: ExcludeSemantics( child: FadeTransition( opacity: motion.label[i], child: Align( alignment: AlignmentDirectional.centerEnd, widthFactor: 1, child: ConstrainedBox( constraints: BoxConstraints(maxWidth: maxWidth), child: GestureDetector( onTap: entry == null ? onAdd : () => onOpen(entry), onLongPress: entry == null || onRemove == null ? null : () => onRemove!(entry), child: ChatStackLabel( text: entry?.title ?? addLabel, color: entry?.color(context), emphasized: entry?.current ?? false, ), ), ), ), ), ), ), ); } Widget _chatButton(ChatStackEntry entry) => Builder( builder: (context) { final remove = onRemove; final ring = entry.current ? entry.color(context) ?? Theme.of(context).colorScheme.primary : null; return Semantics( container: true, button: true, selected: entry.current, label: entry.title, hint: entry.current ? 'Open now' : null, onTap: () => onOpen(entry), customSemanticsActions: remove == null ? null : { const CustomSemanticsAction(label: removeLabel): () => remove(entry), }, child: _Tappable( autofocus: entry.current, onTap: () => onOpen(entry), onLongPress: remove == null ? null : () => remove(entry), child: ChatStackDisc(ring: ring, 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: addLabel, onTap: onAdd, child: _Tappable( onTap: onAdd, child: ChatStackDisc( child: DecoratedBox( decoration: ShapeDecoration( color: tokens.flat ? null : scheme.primaryContainer, shape: chatStackShape(tokens).copyWith( // A design with a contrast floor frames it in the full // accent at its own stroke. side: !tokens.flat ? BorderSide.none : tokens.minContrast != null ? BorderSide(color: scheme.primary, width: tokens.stroke) : BorderSide(color: scheme.primary.withValues(alpha: .55)), ), ), child: Icon( Icons.add, color: tokens.flat ? scheme.primary : scheme.onPrimaryContainer, ), ), ), ), ); } } /// A disc's ink and focus: the splash is drawn over the disc (the disc is /// opaque, so ink under it would never show), in the design's shape. class _Tappable extends StatelessWidget { const _Tappable({ required this.child, required this.onTap, this.onLongPress, this.autofocus = false, }); final Widget child; final VoidCallback onTap; final VoidCallback? onLongPress; final bool autofocus; @override Widget build(BuildContext context) { final shape = chatStackShape(AppDesignTokens.of(context)); return Stack( children: [ child, Positioned.fill( child: Padding( padding: const EdgeInsets.all( (kChatStackSlot - kChatStackDisc) / 2, ), child: Material( type: MaterialType.transparency, shape: shape, clipBehavior: Clip.antiAlias, child: InkWell( autofocus: autofocus, excludeFromSemantics: true, customBorder: shape, onTap: onTap, onLongPress: onLongPress, ), ), ), ), ], ); } }