import 'package:flutter/material.dart'; import 'chat_deliveries.dart'; import 'chat_schedule_time.dart'; /// The words each delivery state reads as, beside its icon. String chatDeliveryLabel(ChatDelivery state) => switch (state) { ChatDelivery.sending => 'Sending…', ChatDelivery.sent => 'Sent', ChatDelivery.queued => 'Queued', ChatDelivery.steered => 'Delivered to the running reply', ChatDelivery.failed => 'Not sent', }; /// A small muted line under the operator's own bubble saying whether the /// server has the message. "Not sent" takes the error colour and offers the /// native Retry (same operation id) and Edit (back into the composer) — or /// Discard when the stored intent can no longer be sent as it was. "Queued" /// offers Remove, which takes the message off the server's queue; one the /// server holds until a later [sendAt] reads "Scheduled · sends at 15:01". class ChatDeliveryIndicator extends StatelessWidget { const ChatDeliveryIndicator({ super.key, required this.state, this.onRetry, this.onEdit, this.editLabel = 'Edit', this.onRemove, this.sendAt, }); final ChatDelivery state; final VoidCallback? onRetry; final VoidCallback? onEdit; final String editLabel; /// Takes a "Queued" message back off the server's queue before it is sent. final VoidCallback? onRemove; /// When a queued message is scheduled to send; null for one that waits /// only for the turn in flight. final DateTime? sendAt; @override Widget build(BuildContext context) { final failed = state == ChatDelivery.failed; final color = failed ? Theme.of(context).colorScheme.error : Theme.of(context).colorScheme.onSurfaceVariant; final style = DefaultTextStyle.of( context, ).style.merge(TextStyle(fontSize: 12, color: color)); final scheduled = state == ChatDelivery.queued && sendAt != null; final label = scheduled ? 'Scheduled · sends at ${chatScheduleTime(context, sendAt!)}' : chatDeliveryLabel(state); return Semantics( container: true, label: 'Delivery: $label', child: Wrap( key: Key( scheduled ? 'chat-delivery-scheduled' : 'chat-delivery-${state.name}', ), spacing: 4, crossAxisAlignment: WrapCrossAlignment.center, children: [ ExcludeSemantics( child: Icon( scheduled ? Icons.schedule_send : _icon, size: style.fontSize! + 2, color: color, ), ), ExcludeSemantics(child: Text(label, style: style)), if (failed && onRetry != null) _action('chat-delivery-retry', 'Retry', onRetry!, style), if (failed && onEdit != null) _action('chat-delivery-edit', editLabel, onEdit!, style), if (state == ChatDelivery.queued && onRemove != null) _action('chat-delivery-remove', 'Remove', onRemove!, style), ], ), ); } IconData get _icon => switch (state) { ChatDelivery.sending => Icons.schedule, ChatDelivery.sent => Icons.done, ChatDelivery.queued => Icons.hourglass_empty, ChatDelivery.steered => Icons.done, ChatDelivery.failed => Icons.error_outline, }; Widget _action( String key, String text, VoidCallback onPressed, TextStyle style, ) { return TextButton( key: Key(key), style: TextButton.styleFrom( foregroundColor: style.color, visualDensity: VisualDensity.compact, textStyle: style.copyWith(fontWeight: FontWeight.w600), ), onPressed: onPressed, child: Text(text), ); } }