Files
pipelines/app/lib/ui/layout/app_list_row.dart
T
2026-12-03 23:56:39 +00:00

538 lines
19 KiB
Dart

import 'package:flutter/material.dart';
import '../theme/app_design_tokens.dart';
import '../theme/high_contrast_palette.dart';
import 'app_list_avatar.dart';
/// Below this row width (a list pane squeezed beside its detail) a row is
/// compact: it keeps its avatar (a run's status) and its menu, so no row
/// loses its state or its actions, and moves the time under the subtitle,
/// leaving the title the line's width.
const double kAppListRowCompactWidth = 280;
/// Below this row width (narrower than any list pane, as in a row squeezed
/// by its host) the avatar and the menu would leave the text no room, so the
/// row drops them rather than overflow.
const double kAppListRowMinimalWidth = 160;
/// The rows' horizontal inset and the gap after the avatar.
const double kAppListRowInset = 16;
const double _avatarGap = 14;
const double _compactAvatarGap = 8;
/// The rows' vertical breathing room: the padding above and below the
/// content and the gap between the title and the lines under it. Rows stay
/// content-driven; these only space them out so they are easier to hit.
const double kAppListRowVerticalPadding = 13;
const double _lineGap = 4;
/// Keys on a row's highlight surface and inset divider, for tests.
const appListRowSurfaceKey = ValueKey<String>('app-list-row-surface');
const appListRowDividerKey = ValueKey<String>('app-list-row-divider');
const appListRowAccentKey = ValueKey<String>('app-list-row-accent');
const appListRowSelectionKey = ValueKey<String>('app-list-row-selection');
/// The width of a row's leading bar: the open row's accent, or an identity
/// colour's mark.
const double kAppListRowBarWidth = 4;
/// The app's one list row (runs, pipelines, chats, search results,
/// schedules).
///
/// Layout: [leading] (an [AppListAvatar]), then a bold one-line [title] with
/// the [time] at its trailing edge, then one muted [subtitle] line (with an
/// optional [subtitleLeading] dot/prefix and [subtitleTrailing] mark), then
/// the optional [menu] (an `AppOverflowMenu`). A row in a multi-select list
/// puts its checkbox in [selectionControl], which leads the row. A compact
/// row (narrower than [compactWidth]) keeps all of these and shows the time
/// on a line of its own under the subtitle. An inset hairline [divider]
/// under the text column separates rows. Nothing has a fixed height: the row
/// grows with text scaling and every line ellipsises.
///
/// [selected] paints the open row: Material `secondaryContainer` with one
/// [selectionAccent] bar on its leading edge, the one selection style every
/// list shares; [emphasized]
/// marks unread/attention (a heavier title and an accent time). Rows ripple.
class AppListRow extends StatelessWidget {
const AppListRow({
super.key,
required this.title,
this.leading,
this.time,
this.timeWidget,
this.subtitle,
this.compactSubtitle,
this.detail,
this.compactDetail = true,
this.subtitleLeading,
this.subtitleTrailing,
this.menu,
this.onTap,
this.onLongPress,
this.selectionControl,
this.tapKey,
this.selected = false,
this.selectedFill,
this.emphasized = false,
this.divider = true,
this.compactWidth = kAppListRowCompactWidth,
this.accent,
this.showAccentBorder = true,
this.selectionAccent,
});
/// The leading bar's colour on the open row; Material `primary` when null.
/// Every list marks its open row the same way: the tonal fill plus this
/// one bar.
final Color? selectionAccent;
/// The item's own colour (a chat's highlight): a bar on the row's leading
/// edge and a faint tint under an unselected row. The selected fill still
/// marks the open row, so the two never read as one another.
final Color? accent;
/// Optional feature-specific selection surface, retaining selection semantics.
final Color? selectedFill;
/// Retain identity tint while a feature supplies its own visibility marker.
final bool showAccentBorder;
final Widget title;
final Widget? leading;
final String? time;
final Widget? timeWidget;
final Widget? subtitle;
/// Replaces [subtitle] in a compact row, which has less room for facts.
final Widget? compactSubtitle;
/// An optional third line under the subtitle (a failed run's reason), one
/// ellipsised line in the subtitle style unless the widget styles itself.
final Widget? detail;
/// Whether a compact row keeps [detail]; false leaves a narrow row its
/// title and [compactSubtitle] only.
final bool compactDetail;
final Widget? subtitleLeading;
final Widget? subtitleTrailing;
final Widget? menu;
final VoidCallback? onTap;
/// A touch long-press on the row (for example, entering a select mode).
final VoidCallback? onLongPress;
/// A checkbox leading the row in a multi-select list.
final Widget? selectionControl;
/// Placed on the tap target ([InkWell] / [GestureDetector]).
final Key? tapKey;
final bool selected;
final bool emphasized;
final bool divider;
final double compactWidth;
/// The faint tint an unselected row with [accent] wears over the list;
/// none under a design that keeps chat colours to bars
/// ([AppDesignTokens.minContrast]).
static Color? tintFor(BuildContext context, Color? accent) =>
AppDesignTokens.of(context).minContrast != null
? null
: accent?.withValues(
alpha: Theme.of(context).brightness == Brightness.dark ? 0.12 : 0.07,
);
/// The width of an inverted open row's bar: thicker, as the fill already
/// marks the row.
static const double invertedBarWidth = 6;
/// How strongly a flat design's open row washes its bar colour over the
/// page: Terminal's framed row strongly, Paper's unframed wash a little
/// lighter (its frameless row still stands well apart from tinted rows).
static double _washAlpha(SelectedRowStyle style, bool dark) =>
switch (style) {
SelectedRowStyle.washed => dark ? .22 : .15,
_ => dark ? .26 : .2,
};
/// The open row's fill is Material `secondaryContainer`.
static Color selectedColor(BuildContext context) =>
Theme.of(context).colorScheme.secondaryContainer;
@override
Widget build(BuildContext context) {
final rowStyle = AppDesignTokens.of(context).selectedRow;
// An inverted design draws every row's content under a theme: the
// page's own, or the opposite brightness's for the open row, so its
// text, icons and status colours keep their contrast on the inverted
// fill. Every row has the Theme, so selecting one keeps its subtree.
final invertible = rowStyle == SelectedRowStyle.inverted;
final inverted = invertible && selected;
final outer = Theme.of(context);
final theme = inverted ? AppDesignTokens.inverseTheme(outer) : outer;
final styles = _RowStyles.of(
outer,
selected: selected && !inverted,
emphasized: emphasized,
);
final metrics = _RowMetrics.of(context);
return Semantics(
container: true,
selected: selected,
button: onTap != null,
child: LayoutBuilder(
builder: (context, constraints) {
final compact = constraints.maxWidth < compactWidth;
final minimal = constraints.maxWidth < kAppListRowMinimalWidth;
final showLeading = leading != null && !minimal;
final showMenu = menu != null && !minimal;
final start = compact ? metrics.compactInset : metrics.inset;
final avatarGap = compact
? metrics.compactAvatarGap
: metrics.avatarGap;
final content = Padding(
padding: EdgeInsetsDirectional.only(
start: start,
end: showMenu ? 4 : metrics.inset,
top: metrics.vertical,
bottom: metrics.vertical,
),
child: Row(
children: [
if (selectionControl case final control?)
Padding(
padding: const EdgeInsetsDirectional.only(end: 8),
child: control,
),
if (showLeading)
Padding(
padding: EdgeInsetsDirectional.only(end: avatarGap),
child: leading,
),
Expanded(
// Under the row's own Theme the text reads its styles
// from it (localized, as Theme.of gives them).
child: invertible
? Builder(
builder: (context) => _text(
_RowStyles.of(
Theme.of(context),
selected: false,
emphasized: emphasized,
),
metrics,
compact,
),
)
: _text(styles, metrics, compact),
),
if (showMenu)
Padding(
padding: const EdgeInsetsDirectional.only(start: 4),
child: menu,
),
],
),
);
final tint = tintFor(context, accent);
// How the design marks the open row: Material's tonal fill, or
// (on a flat design, where a tonal fill is too faint beside tinted
// rows) its bar colour washed over the page, stronger, with a
// hairline frame in it when the design frames it (below), or the
// ink as the fill (inverted).
final washed =
selected &&
rowStyle != SelectedRowStyle.tonal &&
rowStyle != SelectedRowStyle.inverted;
final framed = selected && rowStyle == SelectedRowStyle.framed;
final frameColor =
selectionAccent ??
accent ??
Theme.of(context).colorScheme.primary;
final themed = invertible
? Theme(data: theme, child: content)
: content;
final surface = Material(
key: appListRowSurfaceKey,
color: inverted
? theme.scaffoldBackgroundColor
: washed
? Color.alphaBlend(
frameColor.withValues(
alpha: _washAlpha(
rowStyle,
Theme.of(context).brightness == Brightness.dark,
),
),
Theme.of(context).scaffoldBackgroundColor,
)
: selected
? selectedFill ?? selectedColor(context)
: tint ?? Colors.transparent,
child: InkWell(
key: tapKey,
onTap: onTap,
onLongPress: onLongPress,
child: metrics.minHeight > 0
? ConstrainedBox(
constraints: BoxConstraints(minHeight: metrics.minHeight),
child: Center(widthFactor: 1, child: themed),
)
: themed,
),
);
// The colour bar runs the row's whole height, divider included, and
// the tint fills the divider's inset too, so coloured rows read as
// one band each.
// The open row's bar, else an identity colour's own bar.
var bar = selected
? selectionAccent ?? accent ?? theme.colorScheme.primary
: showAccentBorder
? accent
: null;
// On the inverted fill the bar keeps a 3:1 contrast with it.
if (inverted && bar != null) {
bar = colorWithContrast(bar, theme.scaffoldBackgroundColor, 3);
}
Widget marked(Widget row) => bar == null
? row
: DecoratedBox(
key: selected ? appListRowSelectionKey : appListRowAccentKey,
position: DecorationPosition.foreground,
decoration: BoxDecoration(
border: BorderDirectional(
start: BorderSide(
color: bar,
width: inverted
? invertedBarWidth
: kAppListRowBarWidth,
),
top: framed ? BorderSide(color: bar) : BorderSide.none,
end: framed ? BorderSide(color: bar) : BorderSide.none,
bottom: framed ? BorderSide(color: bar) : BorderSide.none,
),
),
child: row,
);
if (!divider) return marked(surface);
// The divider stays under the text column; a checkbox's width is
// not known here, so a selectable row's divider starts at the inset.
final textStart = selectionControl != null
? start
: start + (showLeading ? metrics.avatar + avatarGap : 0);
final line = Padding(
padding: EdgeInsetsDirectional.only(start: textStart),
child: SizedBox(
key: appListRowDividerKey,
height: 1,
child: ColoredBox(color: styles.divider),
),
);
return marked(
Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
surface,
if (tint != null && !selected)
ColoredBox(color: tint, child: line)
else
line,
],
),
);
},
),
);
}
/// The time, one ellipsised line in the time style, or null without one.
Widget? _time(_RowStyles styles) {
if (timeWidget == null && (time == null || time!.isEmpty)) return null;
return timeWidget == null
? Text(
time!,
style: styles.time,
maxLines: 1,
overflow: TextOverflow.ellipsis,
)
: DefaultTextStyle(
style: styles.time,
maxLines: 1,
overflow: TextOverflow.ellipsis,
child: timeWidget!,
);
}
Widget _text(_RowStyles styles, _RowMetrics metrics, bool compact) {
final line = compact ? (compactSubtitle ?? subtitle) : subtitle;
final time = _time(styles);
final titleLine = LayoutBuilder(
// The time keeps at most 40% of the line, so very large text in a
// narrow row ellipsises instead of overflowing.
builder: (context, constraints) => Row(
children: [
Expanded(
child: DefaultTextStyle(
style: styles.title,
maxLines: 1,
overflow: TextOverflow.ellipsis,
child: title,
),
),
if (!compact && time != null)
ConstrainedBox(
constraints: BoxConstraints(maxWidth: constraints.maxWidth * 0.4),
child: Padding(
padding: const EdgeInsetsDirectional.only(start: 8),
child: time,
),
),
],
),
);
// A compact row has no room beside its title: the time takes a line of
// its own under the subtitle.
final timeLine = compact ? time : null;
final detailLine = detail == null || (compact && !compactDetail)
? null
: Padding(
padding: EdgeInsets.only(top: metrics.lineGap),
child: DefaultTextStyle(
style: styles.subtitle,
maxLines: 1,
overflow: TextOverflow.ellipsis,
child: detail!,
),
);
if (line == null && subtitleLeading == null && subtitleTrailing == null) {
if (timeLine == null) return titleLine;
return Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
titleLine,
SizedBox(height: metrics.lineGap),
timeLine,
],
);
}
return Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
titleLine,
SizedBox(height: metrics.lineGap),
Row(
children: [
if (subtitleLeading != null)
Padding(
padding: const EdgeInsetsDirectional.only(end: 6),
child: subtitleLeading,
),
Expanded(
child: DefaultTextStyle(
style: styles.subtitle,
maxLines: 1,
overflow: TextOverflow.ellipsis,
child: line ?? const SizedBox.shrink(),
),
),
if (subtitleTrailing != null)
Padding(
padding: const EdgeInsetsDirectional.only(start: 8),
child: subtitleTrailing,
),
],
),
?detailLine,
?timeLine,
],
);
}
}
/// The row's spacing under the design's density: the comfortable constants,
/// or a dense design's tighter ones (a 36px single-line row and a ~52px
/// two-line row on a pointer device; at least 40px on touch).
class _RowMetrics {
const _RowMetrics({
required this.inset,
required this.compactInset,
required this.avatarGap,
required this.compactAvatarGap,
required this.vertical,
required this.lineGap,
required this.avatar,
required this.minHeight,
});
factory _RowMetrics.of(BuildContext context) {
final density = AppDesignTokens.of(context).density;
if (!density.dense) return _comfortable;
return _RowMetrics(
inset: density.inset(kAppListRowInset),
compactInset: density.inset(12),
avatarGap: density.space(_avatarGap),
compactAvatarGap: density.space(_compactAvatarGap),
vertical: density.space(kAppListRowVerticalPadding),
lineGap: 0,
avatar: density.avatarSize,
minHeight: density.minTouchRow,
);
}
static const _comfortable = _RowMetrics(
inset: kAppListRowInset,
compactInset: 12,
avatarGap: _avatarGap,
compactAvatarGap: _compactAvatarGap,
vertical: kAppListRowVerticalPadding,
lineGap: _lineGap,
avatar: kAppListAvatarSize,
minHeight: 0,
);
final double inset;
final double compactInset;
final double avatarGap;
final double compactAvatarGap;
final double vertical;
final double lineGap;
final double avatar;
final double minHeight;
}
/// The row's text styles, from the host's own type scale.
class _RowStyles {
const _RowStyles(this.title, this.subtitle, this.time, this.divider);
factory _RowStyles.of(
ThemeData theme, {
required bool selected,
required bool emphasized,
}) {
final weight = emphasized ? FontWeight.w700 : FontWeight.w600;
final scheme = theme.colorScheme;
final text = theme.textTheme;
final muted = scheme.onSurfaceVariant;
return _RowStyles(
(text.titleMedium ?? const TextStyle()).copyWith(
fontWeight: weight,
letterSpacing: 0,
color: selected ? scheme.onSecondaryContainer : scheme.onSurface,
),
(text.bodyMedium ?? const TextStyle()).copyWith(color: muted),
(text.bodySmall ?? const TextStyle()).copyWith(
color: emphasized ? scheme.primary : muted,
fontWeight: emphasized ? FontWeight.w600 : null,
),
scheme.outlineVariant,
);
}
final TextStyle title;
final TextStyle subtitle;
final TextStyle time;
final Color divider;
}