Files
pipelines/app/lib/ui/layout/app_list_row.dart
T
alan a40a6e2e1c Narrow list panes: readable bar title, More overflow menu, rows keep status and actions
The floating bar keeps the title at its own width (up to half the bar) and
moves overflowable actions into one More menu instead of squeezing them
below their touch targets. List rows keep their leading status avatar and
More actions at any list-pane width; the time moves under the subtitle.
2026-11-03 18:04:10 +00:00

431 lines
14 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../adaptive/host.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 = 12;
const double _compactAvatarGap = 8;
/// 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');
bool _darkSurface(BuildContext context, bool cupertino) => cupertino
? CupertinoTheme.brightnessOf(context) == Brightness.dark
: Theme.of(context).brightness == Brightness.dark;
/// The app's one list row (runs, pipelines, chats, search results,
/// schedules), on both hosts.
///
/// 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`, Cupertino
/// `tertiarySystemFill`); [emphasized] marks unread/attention (a heavier
/// title and an accent time). Material rows ripple; Cupertino rows highlight
/// while pressed, like a native table cell.
class AppListRow extends StatelessWidget {
const AppListRow({
super.key,
required this.title,
this.leading,
this.time,
this.timeWidget,
this.subtitle,
this.compactSubtitle,
this.subtitleLeading,
this.subtitleTrailing,
this.menu,
this.onTap,
this.onLongPress,
this.selectionControl,
this.tapKey,
this.selected = false,
this.emphasized = false,
this.divider = true,
this.compactWidth = kAppListRowCompactWidth,
this.cupertino,
this.accent,
});
/// 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;
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;
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;
/// Overrides the host detection (some features pick a style per platform).
final bool? cupertino;
/// The open row's fill on each host.
static Color selectedColor(BuildContext context, {required bool cupertino}) =>
cupertino
? CupertinoColors.tertiarySystemFill.resolveFrom(context)
: Theme.of(context).colorScheme.secondaryContainer;
@override
Widget build(BuildContext context) {
final cupertino = this.cupertino ?? usesCupertinoHost(context);
final styles = _RowStyles.of(
context,
cupertino: cupertino,
selected: selected,
emphasized: emphasized,
);
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 ? 12.0 : kAppListRowInset;
final avatarGap = compact ? _compactAvatarGap : _avatarGap;
final content = Padding(
padding: EdgeInsetsDirectional.only(
start: start,
end: showMenu ? 4 : kAppListRowInset,
top: 10,
bottom: 10,
),
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(child: _text(styles, compact)),
if (showMenu)
Padding(
padding: const EdgeInsetsDirectional.only(start: 4),
child: menu,
),
],
),
);
final tint = accent?.withValues(
alpha: _darkSurface(context, cupertino) ? 0.12 : 0.07,
);
final surface = cupertino
? _CupertinoRowSurface(
tapKey: tapKey,
onTap: onTap,
onLongPress: onLongPress,
selected: selected,
tint: tint,
child: content,
)
: Material(
key: appListRowSurfaceKey,
color: selected
? selectedColor(context, cupertino: false)
: tint ?? Colors.transparent,
child: InkWell(
key: tapKey,
onTap: onTap,
onLongPress: onLongPress,
child: content,
),
);
// 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.
Widget marked(Widget row) => accent == null
? row
: DecoratedBox(
key: appListRowAccentKey,
position: DecorationPosition.foreground,
decoration: BoxDecoration(
border: BorderDirectional(
start: BorderSide(color: accent!, width: 4),
),
),
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 ? kAppListAvatarSize + avatarGap : 0);
final line = Padding(
padding: EdgeInsetsDirectional.only(start: textStart),
child: SizedBox(
key: appListRowDividerKey,
height: cupertino
? 1 / MediaQuery.devicePixelRatioOf(context)
: 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, 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;
if (line == null && subtitleLeading == null && subtitleTrailing == null) {
if (timeLine == null) return titleLine;
return Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [titleLine, const SizedBox(height: 2), timeLine],
);
}
return Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
titleLine,
const SizedBox(height: 2),
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,
),
],
),
?timeLine,
],
);
}
}
/// 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(
BuildContext context, {
required bool cupertino,
required bool selected,
required bool emphasized,
}) {
final weight = emphasized ? FontWeight.w700 : FontWeight.w600;
if (cupertino) {
final theme = CupertinoTheme.of(context);
final base = theme.textTheme.textStyle;
final muted = CupertinoColors.secondaryLabel.resolveFrom(context);
final small = base.copyWith(fontSize: 15, color: muted);
return _RowStyles(
base.copyWith(fontWeight: weight, letterSpacing: 0),
small,
small.copyWith(
color: emphasized ? theme.primaryColor : muted,
fontWeight: emphasized ? FontWeight.w600 : null,
),
CupertinoColors.separator.resolveFrom(context),
);
}
final theme = Theme.of(context);
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;
}
/// A Cupertino table cell: highlights while pressed, tinted while selected.
class _CupertinoRowSurface extends StatefulWidget {
const _CupertinoRowSurface({
required this.tapKey,
required this.onTap,
required this.selected,
this.onLongPress,
this.tint,
required this.child,
});
/// The unselected row's tint, transparent when null.
final Color? tint;
final Key? tapKey;
final VoidCallback? onTap;
final VoidCallback? onLongPress;
final bool selected;
final Widget child;
@override
State<_CupertinoRowSurface> createState() => _CupertinoRowSurfaceState();
}
class _CupertinoRowSurfaceState extends State<_CupertinoRowSurface> {
bool _pressed = false;
void _press(bool value) {
if (_pressed != value) setState(() => _pressed = value);
}
@override
Widget build(BuildContext context) {
final color = _pressed
? CupertinoColors.systemGrey4.resolveFrom(context)
: widget.selected
? AppListRow.selectedColor(context, cupertino: true)
: widget.tint ?? const Color(0x00000000);
final enabled = widget.onTap != null;
return GestureDetector(
key: widget.tapKey,
behavior: HitTestBehavior.opaque,
onTap: widget.onTap,
onLongPress: widget.onLongPress,
onTapDown: enabled ? (_) => _press(true) : null,
onTapUp: enabled ? (_) => _press(false) : null,
onTapCancel: enabled ? () => _press(false) : null,
child: ColoredBox(
key: appListRowSurfaceKey,
color: color,
child: widget.child,
),
);
}
}