feat(chat): segmented chat header with back, title and model pills

The open conversation draws its own bar of separate rounded pieces: the
platform Back in a circle, a title pill with the folder that opens the
chat information, and a pill with the model and mode controls. Detail
pages drop only their app bar for it, keeping the body mounted.
This commit is contained in:
2026-10-30 23:43:00 +00:00
parent 498c6f17a6
commit 4a85a12cf7
8 changed files with 385 additions and 168 deletions
@@ -0,0 +1,242 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../ui/sessions/chat_adaptive.dart';
/// The open conversation's bar: separate rounded pieces on the page's own
/// background above the transcript, instead of one solid app bar.
///
/// From the leading edge: a round Back (only where the page can go back), a
/// pill with the conversation's title and folder that opens the chat's
/// information, and a pill holding [controls] (model and mode).
class ChatThreadHeader extends StatelessWidget {
const ChatThreadHeader({
super.key,
required this.title,
required this.folder,
required this.onInfo,
this.showBack = false,
this.onBack,
this.controls = const [],
});
final String title;
/// The working folder's name, shown beneath the title; empty for none.
final String folder;
final VoidCallback onInfo;
/// Whether the bar shows Back at all.
final bool showBack;
/// What Back does; null keeps the platform's stock action (pop the route).
final VoidCallback? onBack;
final List<Widget> controls;
@override
Widget build(BuildContext context) {
final cupertino =
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
final background = cupertino
? CupertinoTheme.of(context).scaffoldBackgroundColor
: Theme.of(context).colorScheme.surface;
return ColoredBox(
color: background,
child: SafeArea(
bottom: false,
child: Padding(
padding: const EdgeInsetsDirectional.fromSTEB(12, 8, 12, 16),
child: LayoutBuilder(
builder: (context, constraints) => Row(
children: [
if (showBack) ...[
_ChatHeaderPill(
circle: true,
child: _BackControl(
cupertino: cupertino,
onPressed: onBack,
),
),
const SizedBox(width: 8),
],
Expanded(
child: Align(
alignment: AlignmentDirectional.centerStart,
child: _TitlePill(
title: title,
folder: folder,
cupertino: cupertino,
onPressed: onInfo,
),
),
),
if (controls.isNotEmpty) ...[
const SizedBox(width: 8),
// The controls keep to the trailing edge and at most about
// half the bar, so the title always has room; each control
// ellipsises its own label, the model's taking the larger share.
ConstrainedBox(
constraints: BoxConstraints(
maxWidth: constraints.maxWidth * 0.45,
),
child: _ChatHeaderPill(
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Flexible(flex: 3, child: controls.first),
for (final control in controls.skip(1))
Flexible(flex: 2, child: control),
],
),
),
),
],
],
),
),
),
),
);
}
}
/// One rounded, filled piece of the bar in the host's raised surface role.
class _ChatHeaderPill extends StatelessWidget {
const _ChatHeaderPill({required this.child, this.circle = false});
final Widget child;
final bool circle;
@override
Widget build(BuildContext context) {
final cupertino =
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
final fill = cupertino
? CupertinoColors.systemGrey6.resolveFrom(context)
: Theme.of(context).colorScheme.surfaceContainerHigh;
final shape = circle
? const CircleBorder()
: const StadiumBorder() as ShapeBorder;
if (cupertino) {
return ClipPath(
clipper: ShapeBorderClipper(shape: shape),
child: ColoredBox(color: fill, child: child),
);
}
return Material(
color: fill,
shape: shape,
clipBehavior: Clip.antiAlias,
child: child,
);
}
}
/// The platform's own Back control, so it reads and behaves as every other
/// page's Back; its key and the conversation's destination wording stay.
class _BackControl extends StatelessWidget {
const _BackControl({required this.cupertino, required this.onPressed});
final bool cupertino;
final VoidCallback? onPressed;
@override
Widget build(BuildContext context) => KeyedSubtree(
key: const Key('chat-back'),
child: cupertino
? CupertinoNavigationBarBackButton(onPressed: onPressed)
: BackButton(onPressed: onPressed),
);
}
/// The title and its folder in one pill; pressing it opens the chat's
/// information.
class _TitlePill extends StatelessWidget {
const _TitlePill({
required this.title,
required this.folder,
required this.cupertino,
required this.onPressed,
});
final String title;
final String folder;
final bool cupertino;
final VoidCallback onPressed;
@override
Widget build(BuildContext context) {
final TextStyle? titleStyle;
final TextStyle? folderStyle;
if (cupertino) {
final text = CupertinoTheme.of(context).textTheme;
titleStyle = text.textStyle.copyWith(fontWeight: FontWeight.w600);
folderStyle = text.tabLabelTextStyle.copyWith(
color: CupertinoColors.secondaryLabel.resolveFrom(context),
);
} else {
final theme = Theme.of(context);
titleStyle = theme.textTheme.titleSmall;
folderStyle = theme.textTheme.labelSmall?.copyWith(
color: theme.colorScheme.onSurfaceVariant,
);
}
final content = Padding(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 6),
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
title.isEmpty ? 'Chat' : title,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: titleStyle,
),
if (folder.isNotEmpty)
Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(
cupertino ? CupertinoIcons.folder : Icons.folder_outlined,
size: folderStyle?.fontSize,
color: folderStyle?.color,
),
const SizedBox(width: 4),
Flexible(
child: Text(
folder,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: folderStyle,
),
),
],
),
],
),
);
return _ChatHeaderPill(
child: Semantics(
button: true,
label: 'Chat information',
child: cupertino
? CupertinoButton(
key: const Key('chat-info'),
padding: EdgeInsets.zero,
minimumSize: Size.zero,
onPressed: onPressed,
child: DefaultTextStyle.merge(
style: titleStyle,
child: content,
),
)
: InkWell(
key: const Key('chat-info'),
onTap: onPressed,
child: content,
),
),
);
}
}
@@ -28,6 +28,7 @@ import 'chat_models.dart';
import 'chat_row_keys.dart';
import 'chat_scroll.dart';
import 'chat_thread_controller.dart';
import 'chat_thread_header.dart';
import 'chat_transcript_cache.dart';
import 'chat_permission_card_model.dart';
import 'chat_permission_group.dart';
@@ -717,78 +718,8 @@ class _ChatThreadViewState extends State<ChatThreadView> {
final inShellBar = ShellBar.available(context);
final column = Column(
children: [
// The conversation's own bar first, outside the transcript's scroller,
// as on the web (chat-bar, then background tasks, then transcript).
if (!inShellBar)
Padding(
key: const Key('chat-thread-header'),
padding: const EdgeInsetsDirectional.fromSTEB(4, 8, 4, 4),
child: LayoutBuilder(
builder: (context, constraints) {
final title = widget.title.trim();
final session = widget.session;
final hasModel =
session != null &&
supportsChatModels('${session['backend'] ?? ''}');
// One compact row, as on the web: back, the conversation's
// name, its model and the information action. The title
// takes the remaining width and ellipsises; the model keeps
// a bounded share so neither control is pushed off a narrow
// 2x phone and the header never wraps onto a second line.
return Row(
children: [
_backButton(cupertino),
Expanded(
child: Padding(
padding: const EdgeInsetsDirectional.only(start: 4),
child: title.isEmpty
? const SizedBox.shrink()
: Text(
title,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: cupertino
? CupertinoTheme.of(
context,
).textTheme.navTitleTextStyle
: Theme.of(context).textTheme.titleLarge,
),
),
),
if (hasModel)
ConstrainedBox(
constraints: BoxConstraints(
maxWidth: constraints.maxWidth * 0.4,
),
child: ChatModelControl(
key: ValueKey<String>(
'chat-model-${widget.sessionId}',
),
gateway: widget.gateway,
sessionId: widget.sessionId,
session: session,
onChanged: widget.onModelChanged,
answeredBy: latestReplyModel(_controller.messages),
),
),
ConstrainedBox(
constraints: BoxConstraints(
maxWidth: constraints.maxWidth * 0.3,
),
child: _modeControl(),
),
ChatBarAction(
key: const Key('chat-info'),
label: 'Chat information',
onPressed: _openInfo,
materialIcon: Icons.info_outline,
cupertinoIcon: CupertinoIcons.info,
),
],
);
},
),
),
// The conversation's own floating bar, above its transcript.
_header(context, inShellBar),
Expanded(
child: NotificationListener<ScrollMetricsNotification>(
onNotification: _onScrollMetrics,
@@ -922,42 +853,60 @@ class _ChatThreadViewState extends State<ChatThreadView> {
],
);
if (!inShellBar) return column;
final session = widget.session;
final hasModel =
session != null && supportsChatModels('${session['backend'] ?? ''}');
// The conversation draws its own floating bar; the page shows none.
final title = widget.title.trim();
return ShellBar(
barless: true,
title: title.isEmpty ? null : title,
actions: [
if (hasModel)
ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 180),
child: ChatModelControl(
key: ValueKey<String>('chat-model-${widget.sessionId}'),
gateway: widget.gateway,
sessionId: widget.sessionId,
session: session,
onChanged: widget.onModelChanged,
answeredBy: latestReplyModel(_controller.messages),
),
),
ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 160),
child: _modeControl(),
),
ChatBarAction(
key: const Key('chat-info'),
label: 'Chat information',
onPressed: _openInfo,
materialIcon: Icons.info_outline,
cupertinoIcon: CupertinoIcons.info,
),
],
child: column,
);
},
);
/// The floating bar: Back where this page can go back (a pushed phone page,
/// or a standalone screen), the title pill that opens the information, and
/// the model and mode controls.
Widget _header(BuildContext context, bool inShellBar) {
final session = widget.session;
final hasModel =
session != null && supportsChatModels('${session['backend'] ?? ''}');
final canGoBack = inShellBar
? ModalRoute.of(context)?.impliesAppBarDismissal ?? false
: true;
final directory = '${session?['directory'] ?? ''}'.trim();
final folder = directory
.split(RegExp(r'[/\\]'))
.lastWhere((part) => part.isNotEmpty, orElse: () => '');
return ChatThreadHeader(
key: const Key('chat-thread-header'),
title: widget.title.trim(),
folder: folder,
onInfo: _openInfo,
showBack: canGoBack,
// A pushed page keeps the platform's stock Back (pop this route); a
// standalone conversation calls its owner.
onBack: inShellBar ? null : widget.onBack,
controls: [
if (hasModel)
ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 160),
child: ChatModelControl(
key: ValueKey<String>('chat-model-${widget.sessionId}'),
gateway: widget.gateway,
sessionId: widget.sessionId,
session: session,
onChanged: widget.onModelChanged,
answeredBy: latestReplyModel(_controller.messages),
),
),
ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 140),
child: _modeControl(),
),
],
);
}
/// The session's permission mode, keyed per session so another chat
/// re-reads its own modes.
Widget _modeControl() => ChatModeControl(
@@ -1037,32 +986,6 @@ class _ChatThreadViewState extends State<ChatThreadView> {
images: _images,
);
}
/// The header's compact Back control, matching the web's icon-only control.
/// Its accessible name states the destination; Material keeps a tooltip and
/// Cupertino the same label on the wrapping node.
Widget _backButton(bool cupertino) {
const label = 'Back to chats';
final icon = Icon(cupertino ? CupertinoIcons.back : Icons.arrow_back);
if (!cupertino) {
return IconButton(
key: const Key('chat-back'),
tooltip: label,
onPressed: widget.onBack,
icon: icon,
);
}
return Semantics(
label: label,
button: true,
child: CupertinoButton(
key: const Key('chat-back'),
padding: EdgeInsets.zero,
onPressed: widget.onBack,
child: icon,
),
);
}
}
/// The native dismiss control shared by a local notice and a running
+31 -20
View File
@@ -129,32 +129,43 @@ class _DetailPaneState extends State<DetailPane> {
final spec = _bar.specFor(widget.destination);
final title = spec?.title ?? '';
final actions = spec?.actions ?? const <Widget>[];
// Content with its own bar reaches the top edge and owns the
// status-bar inset itself. The scaffold and the body's place in it stay
// the same either way, so switching never remounts the content.
final barless = spec?.barless ?? false;
if (usesCupertinoHost(context)) {
return CupertinoPageScaffold(
navigationBar: CupertinoNavigationBar(
automaticallyImplyLeading: widget.implyBack,
// Inline pane bars are not routes and must not join the shell's
// navigation-bar hero when resizing into a pushed phone page.
transitionBetweenRoutes: widget.implyBack,
middle: Text(title),
trailing: actions.isEmpty
? null
: Row(
mainAxisSize: MainAxisSize.min,
children: [
for (final action in actions) Flexible(child: action),
],
),
),
navigationBar: barless
? null
: CupertinoNavigationBar(
automaticallyImplyLeading: widget.implyBack,
// Inline pane bars are not routes and must not join the shell's
// navigation-bar hero when resizing into a pushed phone page.
transitionBetweenRoutes: widget.implyBack,
middle: Text(title),
trailing: actions.isEmpty
? null
: Row(
mainAxisSize: MainAxisSize.min,
children: [
for (final action in actions)
Flexible(child: action),
],
),
),
child: SafeArea(child: body),
);
}
return Scaffold(
appBar: AppBar(
automaticallyImplyLeading: widget.implyBack,
title: Text(title),
actions: [for (final action in actions) Flexible(child: action)],
),
appBar: barless
? null
: AppBar(
automaticallyImplyLeading: widget.implyBack,
title: Text(title),
actions: [
for (final action in actions) Flexible(child: action),
],
),
body: SafeArea(top: false, child: body),
);
},
+15 -1
View File
@@ -5,11 +5,20 @@ import 'destination.dart';
/// What the open screen of a tab puts in the app bar: its title, a Back
/// control and its actions. Absent, the bar shows the tab's own name.
class ShellBarSpec {
const ShellBarSpec({this.title, this.onBack, this.actions = const []});
const ShellBarSpec({
this.title,
this.onBack,
this.actions = const [],
this.barless = false,
});
final String? title;
final VoidCallback? onBack;
final List<Widget> actions;
/// The content draws its own floating bar: a detail page shows no bar of
/// its own and lets the content reach the top edge.
final bool barless;
}
/// The bar contents each tab's open screen has published.
@@ -86,9 +95,13 @@ class ShellBar extends StatefulWidget {
this.onBack,
this.actions = const [],
this.active = true,
this.barless = false,
required this.child,
});
/// See [ShellBarSpec.barless].
final bool barless;
/// False while the screen is kept mounted but hidden (a list under an open
/// detail): it then gives the bar up.
final bool active;
@@ -130,6 +143,7 @@ class _ShellBarState extends State<ShellBar> {
title: widget.title,
onBack: widget.onBack,
actions: widget.actions,
barless: widget.barless,
);
// The bar is above this screen; update it after this frame's build.
WidgetsBinding.instance.addPostFrameCallback((_) {