Files
pipelines/app/lib/features/chat/ui/sessions/chat_adaptive.dart
T

358 lines
11 KiB
Dart

import '../../../../ui/theme/app_theme.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
/// Which standard control family this screen renders.
enum ChatControlsStyle { material, cupertino }
/// Cupertino under a Cupertino app or an iOS/macOS Material theme, Material
/// everywhere else. The shell decides the app class, so a bare Cupertino app
/// never has to grow a Material ancestor just for this screen.
ChatControlsStyle chatControlsStyleOf(BuildContext context) {
if (context.findAncestorWidgetOfExactType<MaterialApp>() == null) {
return ChatControlsStyle.cupertino;
}
final platform = Theme.of(context).platform;
return platform == TargetPlatform.iOS || platform == TargetPlatform.macOS
? ChatControlsStyle.cupertino
: ChatControlsStyle.material;
}
class ChatBarAction extends StatelessWidget {
const ChatBarAction({
super.key,
required this.label,
required this.onPressed,
required this.materialIcon,
required this.cupertinoIcon,
});
final String label;
final VoidCallback onPressed;
final IconData materialIcon;
final IconData cupertinoIcon;
@override
Widget build(BuildContext context) {
if (chatControlsStyleOf(context) == ChatControlsStyle.cupertino) {
return Semantics(
label: label,
button: true,
child: CupertinoButton(
padding: EdgeInsets.zero,
onPressed: onPressed,
child: Icon(cupertinoIcon),
),
);
}
return IconButton(
tooltip: label,
onPressed: onPressed,
icon: Icon(materialIcon),
);
}
}
/// The chat toolbar's one primary create action, labelled as on the web
/// (`+ New chat`), not an unlabelled glyph.
class ChatNewChatButton extends StatelessWidget {
const ChatNewChatButton({super.key, required this.onPressed});
final VoidCallback onPressed;
@override
Widget build(BuildContext context) {
if (chatControlsStyleOf(context) == ChatControlsStyle.cupertino) {
return CupertinoButton.filled(
sizeStyle: CupertinoButtonSize.small,
onPressed: onPressed,
child: const Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(CupertinoIcons.add, size: 16),
SizedBox(width: 4),
Text('New chat'),
],
),
);
}
return FilledButton.icon(
onPressed: onPressed,
icon: const Icon(Icons.add),
label: const Text('New chat'),
);
}
}
class ChatSearchField extends StatelessWidget {
const ChatSearchField({
super.key,
required this.controller,
required this.onChanged,
this.trailing = const [],
});
final TextEditingController controller;
final ValueChanged<String> onChanged;
/// Actions on the search row's trailing edge, as the web's chat toolbar
/// keeps New chat beside its search input.
final List<Widget> trailing;
@override
Widget build(BuildContext context) {
final field = chatControlsStyleOf(context) == ChatControlsStyle.cupertino
? CupertinoSearchTextField(
controller: controller,
placeholder: 'Search chats',
onChanged: onChanged,
)
: TextField(
controller: controller,
onChanged: onChanged,
decoration: const InputDecoration(
prefixIcon: Icon(Icons.search),
labelText: 'Search chats',
),
);
return Padding(
padding: const EdgeInsets.all(16),
child: trailing.isEmpty
? field
: Row(
children: [
Expanded(child: field),
for (final action in trailing) ...[
const SizedBox(width: 8),
action,
],
],
),
);
}
}
/// A grouped Cupertino card for a message that may wrap onto several lines.
///
/// [CupertinoListTile] forces its title to one ellipsised line, which cut long
/// no-match queries and server reasons. This layout keeps the native card but
/// lets the message wrap, with its action on a separate button below.
class ChatCupertinoMessage extends StatelessWidget {
const ChatCupertinoMessage({
super.key,
required this.message,
this.icon,
this.actionLabel,
this.onAction,
});
final String message;
final IconData? icon;
final String? actionLabel;
final VoidCallback? onAction;
@override
Widget build(BuildContext context) {
return Semantics(
container: true,
explicitChildNodes: true,
child: CupertinoListSection.insetGrouped(
backgroundColor: AppColors.groupedBackground,
children: [
Padding(
padding: const EdgeInsets.all(16),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (icon != null) ...[
Icon(
icon,
color: CupertinoColors.secondaryLabel.resolveFrom(context),
),
const SizedBox(width: 12),
],
Expanded(
// Cupertino widgets normally supply their own text style.
// This card is also mounted under a bare MaterialApp (the
// iOS/macOS theme path), where Text would otherwise inherit
// MaterialApp's "missing Material" fallback style.
child: DefaultTextStyle.merge(
style: CupertinoTheme.of(context).textTheme.textStyle,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(message),
if (actionLabel != null && onAction != null)
Align(
alignment: AlignmentDirectional.centerStart,
child: CupertinoButton(
padding: const EdgeInsets.only(top: 8),
onPressed: onAction,
child: Text(actionLabel!),
),
),
],
),
),
),
],
),
),
],
),
);
}
}
/// A row action's refusal, shown at full width beneath its tile.
///
/// It must not sit in a `CupertinoListTile.subtitle`: that slot forces every
/// paragraph to one ellipsised line, which cut the server's reason down to its
/// first few words. Full width also stops a long refusal from turning a single
/// Material row into a column taller than the viewport.
class ChatRowError extends StatelessWidget {
const ChatRowError({
super.key,
required this.message,
this.messageKey,
this.onDismiss,
});
final String message;
/// Clears this refusal; its control reads the web report's `OK`.
final VoidCallback? onDismiss;
/// Key placed on the message paragraph itself, so a test can assert that the
/// rendered text did not exceed its line limit.
final Key? messageKey;
@override
Widget build(BuildContext context) {
final cupertino =
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
final style = cupertino
? CupertinoTheme.of(context).textTheme.textStyle.copyWith(
color: CupertinoColors.systemRed.resolveFrom(context),
)
: TextStyle(color: Theme.of(context).colorScheme.error);
return Padding(
padding: const EdgeInsetsDirectional.fromSTEB(16, 0, 16, 12),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expanded(
child: DefaultTextStyle.merge(
style: style,
child: Text(message, key: messageKey),
),
),
if (onDismiss != null)
Semantics(
button: true,
label: 'Dismiss refusal',
excludeSemantics: true,
child: cupertino
? CupertinoButton(
padding: const EdgeInsetsDirectional.only(start: 8),
minimumSize: Size.zero,
onPressed: onDismiss,
child: const Text('OK'),
)
: TextButton(onPressed: onDismiss, child: const Text('OK')),
),
],
),
);
}
}
class ChatErrorNotice extends StatelessWidget {
const ChatErrorNotice({
super.key,
required this.message,
required this.onRetry,
});
final String message;
final VoidCallback onRetry;
@override
Widget build(BuildContext context) {
if (chatControlsStyleOf(context) == ChatControlsStyle.cupertino) {
return ChatCupertinoMessage(
message: message,
icon: CupertinoIcons.exclamationmark_circle,
actionLabel: 'Retry',
onAction: onRetry,
);
}
return MaterialBanner(
content: Text(message),
actions: [TextButton(onPressed: onRetry, child: const Text('Retry'))],
);
}
}
class ChatLoadingIndicator extends StatelessWidget {
const ChatLoadingIndicator({super.key});
@override
Widget build(BuildContext context) {
return Center(
child: chatControlsStyleOf(context) == ChatControlsStyle.cupertino
? const CupertinoActivityIndicator()
: const CircularProgressIndicator(),
);
}
}
/// One-row report after hiding a session on this device, with its Undo.
///
/// The screen floats this over the list like the web's fixed report, so a long
/// message can never push the list out of the body's column.
class ChatUndoBar extends StatelessWidget {
const ChatUndoBar({super.key, required this.message, required this.onUndo});
final String message;
final VoidCallback onUndo;
@override
Widget build(BuildContext context) {
final content = Padding(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
child: Row(
children: [
Expanded(
child: DefaultTextStyle.merge(
style: chatControlsStyleOf(context) == ChatControlsStyle.cupertino
? CupertinoTheme.of(context).textTheme.textStyle
: DefaultTextStyle.of(context).style,
child: Text(message),
),
),
if (chatControlsStyleOf(context) == ChatControlsStyle.cupertino)
CupertinoButton(
padding: const EdgeInsetsDirectional.only(start: 12),
onPressed: onUndo,
child: const Text('Undo'),
)
else
TextButton(onPressed: onUndo, child: const Text('Undo')),
],
),
);
if (chatControlsStyleOf(context) == ChatControlsStyle.cupertino) {
// A standard Cupertino popup surface: the bar floats over the list, so it
// needs its own painted card and shadow to stay legible.
return CupertinoPopupSurface(child: content);
}
return Material(
elevation: 3,
borderRadius: BorderRadius.circular(8),
color: Theme.of(context).colorScheme.surfaceContainerHighest,
child: content,
);
}
}