import 'package:flutter/material.dart'; import '../../../../ui/adaptive/app_popups.dart'; import 'chat_turn_activity_view.dart'; /// The quiet chip under an agent turn (its tools, its permissions) and the /// native sheet it opens. /// /// Its leading icon starts at the reply text's edge: the chip sits at the /// row's edge with the same inset as the turn's activity line. Its chevron is /// the transcript's expand affordance and turns down while the sheet is open. class ChatTurnChip extends StatefulWidget { const ChatTurnChip({ super.key, required this.chipKey, required this.leading, required this.label, required this.color, required this.semanticsLabel, this.sheet, this.open, this.ellipsizeLabel = false, this.labelSpans, this.detail, }) : assert(sheet != null || open != null); final Key chipKey; final Widget leading; final String label; final Color color; final String semanticsLabel; /// The detail content the chip opens, unless [open] presents it instead. final Widget? sheet; /// A custom presentation (a full-screen route on a narrow window). final Future Function(BuildContext context)? open; /// Shrink a long label to one ellipsized line. Only for a chip whose parent /// bounds its width (a turn footer's Wrap, an aligned row). final bool ellipsizeLabel; /// Styled pieces of [label] (e.g. coloured counts); [label] stays the /// plain text. Pieces without their own style inherit the chip's style. final List Function(BuildContext context)? labelSpans; /// A second line under the label, shown in full (a question's answer). final Widget? detail; @override State createState() => _ChatTurnChipState(); } class _ChatTurnChipState extends State { bool _open = false; @override Widget build(BuildContext context) { final labelRow = Row( mainAxisSize: MainAxisSize.min, children: [ if (widget.ellipsizeLabel || widget.detail != null) Flexible(child: _label(maxLines: 1, overflow: TextOverflow.ellipsis)) else _label(), const SizedBox(width: 2), AnimatedRotation( turns: _open ? 0.25 : 0, duration: const Duration(milliseconds: 150), child: Icon(Icons.chevron_right, size: 18, color: widget.color), ), ], ); final content = Row( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.start, children: [ Padding( padding: const EdgeInsetsDirectional.only(top: 2), child: widget.leading, ), const SizedBox(width: 6), Flexible( child: widget.detail == null ? labelRow : Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.start, children: [labelRow, widget.detail!], ), ), ], ); return Semantics( button: true, expanded: _open, // The child's own action is excluded with its label, so the node // carries the activation for assistive technology. onTap: _show, label: widget.semanticsLabel, excludeSemantics: true, child: Padding( padding: const EdgeInsets.only(top: 2), child: InkWell( key: widget.chipKey, borderRadius: BorderRadius.circular(8), onTap: _show, child: Padding( padding: const EdgeInsets.symmetric( horizontal: chatAgentContentInset, vertical: 6, ), child: content, ), ), ), ); } Widget _label({int? maxLines, TextOverflow? overflow}) { final style = chatTurnChipTextStyle(context, widget.color); final spans = widget.labelSpans; if (spans == null) { return Text( widget.label, maxLines: maxLines, overflow: overflow, style: style, ); } return Builder( builder: (context) => Text.rich( TextSpan(children: spans(context)), maxLines: maxLines, overflow: overflow, style: style, ), ); } Future _show() async { if (_open) return; setState(() => _open = true); try { if (widget.open case final open?) { await open(context); } else { await showChatTurnSheet(context, widget.sheet!); } } finally { if (mounted) setState(() => _open = false); } } } /// The secondary text of a turn's meta rows (chips, the activity line): /// smaller and quieter than the reply's prose. TextStyle? chatTurnChipTextStyle(BuildContext context, Color color) => Theme.of( context, ).textTheme.bodyMedium?.copyWith(color: color, fontWeight: FontWeight.w400); /// The detail surface a turn chip opens: a dialog on desktop and a bottom /// sheet on a phone. Future showChatTurnSheet(BuildContext context, Widget sheet) { return showAppDetails(context, builder: (_) => sheet); }