82af7b250f
A shared RelativeTimeLabel refreshes at the next minute/hour/day boundary, pauses while the app is hidden and catches up on resume. It keeps the row's time style (an unread chat's accent). Continues run #218.
371 lines
12 KiB
Dart
371 lines
12 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: no avatar, no time and (by default) no menu, so the title keeps
|
|
/// the width and a tap on the row never lands on a second control.
|
|
const double kAppListRowCompactWidth = 280;
|
|
|
|
/// The rows' horizontal inset and the gap after the avatar.
|
|
const double kAppListRowInset = 16;
|
|
const double _avatarGap = 12;
|
|
|
|
/// 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');
|
|
|
|
/// 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 even when it
|
|
/// is compact. 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.keepMenuWhenCompact = false,
|
|
this.compactWidth = kAppListRowCompactWidth,
|
|
this.cupertino,
|
|
});
|
|
|
|
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; kept when compact.
|
|
final Widget? selectionControl;
|
|
|
|
/// Placed on the tap target ([InkWell] / [GestureDetector]).
|
|
final Key? tapKey;
|
|
final bool selected;
|
|
final bool emphasized;
|
|
final bool divider;
|
|
|
|
/// Keeps the menu in a compact row when the list has no other way to reach
|
|
/// the row's actions.
|
|
final bool keepMenuWhenCompact;
|
|
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 showLeading = leading != null && !compact;
|
|
final showMenu = menu != null && (!compact || keepMenuWhenCompact);
|
|
final start = compact ? 12.0 : kAppListRowInset;
|
|
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: const EdgeInsetsDirectional.only(end: _avatarGap),
|
|
child: leading,
|
|
),
|
|
Expanded(child: _text(styles, compact)),
|
|
if (showMenu)
|
|
Padding(
|
|
padding: const EdgeInsetsDirectional.only(start: 4),
|
|
child: menu,
|
|
),
|
|
],
|
|
),
|
|
);
|
|
final surface = cupertino
|
|
? _CupertinoRowSurface(
|
|
tapKey: tapKey,
|
|
onTap: onTap,
|
|
onLongPress: onLongPress,
|
|
selected: selected,
|
|
child: content,
|
|
)
|
|
: Material(
|
|
key: appListRowSurfaceKey,
|
|
color: selected
|
|
? selectedColor(context, cupertino: false)
|
|
: Colors.transparent,
|
|
child: InkWell(
|
|
key: tapKey,
|
|
onTap: onTap,
|
|
onLongPress: onLongPress,
|
|
child: content,
|
|
),
|
|
);
|
|
if (!divider) return 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);
|
|
return Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
surface,
|
|
Padding(
|
|
padding: EdgeInsetsDirectional.only(start: textStart),
|
|
child: SizedBox(
|
|
key: appListRowDividerKey,
|
|
height: cupertino
|
|
? 1 / MediaQuery.devicePixelRatioOf(context)
|
|
: 1,
|
|
child: ColoredBox(color: styles.divider),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _text(_RowStyles styles, bool compact) {
|
|
final line = compact ? (compactSubtitle ?? subtitle) : subtitle;
|
|
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 &&
|
|
(timeWidget != null || (time != null && time!.isNotEmpty)))
|
|
ConstrainedBox(
|
|
constraints: BoxConstraints(maxWidth: constraints.maxWidth * 0.4),
|
|
child: Padding(
|
|
padding: const EdgeInsetsDirectional.only(start: 8),
|
|
child: timeWidget == null
|
|
? Text(
|
|
time!,
|
|
style: styles.time,
|
|
maxLines: 1,
|
|
overflow: TextOverflow.ellipsis,
|
|
)
|
|
: DefaultTextStyle(
|
|
style: styles.time,
|
|
maxLines: 1,
|
|
overflow: TextOverflow.ellipsis,
|
|
child: timeWidget!,
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
if (line == null && subtitleLeading == null && subtitleTrailing == null) {
|
|
return titleLine;
|
|
}
|
|
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,
|
|
),
|
|
],
|
|
),
|
|
],
|
|
);
|
|
}
|
|
}
|
|
|
|
/// 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,
|
|
required this.child,
|
|
});
|
|
|
|
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)
|
|
: 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,
|
|
),
|
|
);
|
|
}
|
|
}
|