Share one list row across runs, pipelines, chat, search and schedules
AppListRow (Material + Cupertino) with avatar, bold title and trailing time, one muted line, one overflow menu style, inset dividers, selected and emphasised states and a compact mode. Pipelines get a legible initial avatar, schedule rows shrink to two lines with Edit/Pause/Delete in an overflow menu, and the shell chat list scrolls behind the bar.
This commit is contained in:
@@ -0,0 +1,132 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../adaptive/host.dart';
|
||||
|
||||
/// The leading circle every list row uses, as large as a native contact
|
||||
/// avatar ([CircleAvatar]'s default).
|
||||
const double kAppListAvatarSize = 40;
|
||||
|
||||
/// A list row's leading identity: an icon or an initial on a tinted circle,
|
||||
/// with an optional corner badge (a chat's activity indicator).
|
||||
///
|
||||
/// [foreground] colours the icon/initial; the circle is [background], or the
|
||||
/// foreground at 14% when omitted, so a status colour reads as a soft tint.
|
||||
class AppListAvatar extends StatelessWidget {
|
||||
const AppListAvatar({
|
||||
super.key,
|
||||
required this.child,
|
||||
this.foreground,
|
||||
this.background,
|
||||
this.badge,
|
||||
});
|
||||
|
||||
/// The first character of [name] (or `?`), large and bold enough to read
|
||||
/// at a glance in the circle.
|
||||
factory AppListAvatar.initial(
|
||||
String name, {
|
||||
Key? key,
|
||||
required Color color,
|
||||
Widget? badge,
|
||||
}) {
|
||||
final trimmed = name.trimLeft();
|
||||
final initial = trimmed.isEmpty
|
||||
? '?'
|
||||
: String.fromCharCode(trimmed.runes.first).toUpperCase();
|
||||
return AppListAvatar(
|
||||
key: key,
|
||||
foreground: color,
|
||||
badge: badge,
|
||||
child: Text(initial),
|
||||
);
|
||||
}
|
||||
|
||||
final Widget child;
|
||||
final Color? foreground;
|
||||
final Color? background;
|
||||
final Widget? badge;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final cupertino = usesCupertinoHost(context);
|
||||
final mark =
|
||||
foreground ??
|
||||
(cupertino
|
||||
? CupertinoColors.secondaryLabel.resolveFrom(context)
|
||||
: Theme.of(context).colorScheme.onSurfaceVariant);
|
||||
final fill =
|
||||
background ??
|
||||
(foreground == null
|
||||
? (cupertino
|
||||
? CupertinoColors.systemGrey5.resolveFrom(context)
|
||||
: Theme.of(context).colorScheme.surfaceContainerHighest)
|
||||
: mark.withValues(alpha: 0.14));
|
||||
final initialStyle =
|
||||
(cupertino
|
||||
? CupertinoTheme.of(context).textTheme.textStyle
|
||||
: Theme.of(context).textTheme.titleMedium ?? const TextStyle())
|
||||
.copyWith(
|
||||
color: mark,
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.w700,
|
||||
height: 1,
|
||||
);
|
||||
final circle = SizedBox.square(
|
||||
dimension: kAppListAvatarSize,
|
||||
child: DecoratedBox(
|
||||
decoration: BoxDecoration(color: fill, shape: BoxShape.circle),
|
||||
child: Center(
|
||||
// The glyph is identity, not reading text: it scales down to stay
|
||||
// inside the circle under large text instead of overflowing it.
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8),
|
||||
child: FittedBox(
|
||||
fit: BoxFit.scaleDown,
|
||||
child: IconTheme.merge(
|
||||
data: IconThemeData(color: mark, size: 22),
|
||||
child: DefaultTextStyle(style: initialStyle, child: child),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
if (badge == null) return circle;
|
||||
return Stack(
|
||||
clipBehavior: Clip.none,
|
||||
children: [
|
||||
circle,
|
||||
PositionedDirectional(end: -2, bottom: -2, child: badge!),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// A small coloured dot that leads a row's subtitle (a latest-run state) or
|
||||
/// trails it (unread). It follows text scaling and carries its own label.
|
||||
class AppListStatusDot extends StatelessWidget {
|
||||
const AppListStatusDot({
|
||||
super.key,
|
||||
required this.color,
|
||||
this.semanticLabel,
|
||||
this.size = 8,
|
||||
});
|
||||
|
||||
final Color color;
|
||||
final String? semanticLabel;
|
||||
final double size;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final dimension = MediaQuery.textScalerOf(context).scale(size);
|
||||
final dot = DecoratedBox(
|
||||
decoration: BoxDecoration(color: color, shape: BoxShape.circle),
|
||||
child: SizedBox.square(dimension: dimension),
|
||||
);
|
||||
if (semanticLabel == null) return ExcludeSemantics(child: dot);
|
||||
return Semantics(
|
||||
label: semanticLabel,
|
||||
child: ExcludeSemantics(child: dot),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,333 @@
|
||||
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`). 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.subtitle,
|
||||
this.compactSubtitle,
|
||||
this.subtitleLeading,
|
||||
this.subtitleTrailing,
|
||||
this.menu,
|
||||
this.onTap,
|
||||
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? 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;
|
||||
|
||||
/// 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 (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,
|
||||
selected: selected,
|
||||
child: content,
|
||||
)
|
||||
: Material(
|
||||
key: appListRowSurfaceKey,
|
||||
color: selected
|
||||
? selectedColor(context, cupertino: false)
|
||||
: Colors.transparent,
|
||||
child: InkWell(key: tapKey, onTap: onTap, child: content),
|
||||
);
|
||||
if (!divider) return surface;
|
||||
final textStart =
|
||||
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 && time != null && time!.isNotEmpty)
|
||||
ConstrainedBox(
|
||||
constraints: BoxConstraints(maxWidth: constraints.maxWidth * 0.4),
|
||||
child: Padding(
|
||||
padding: const EdgeInsetsDirectional.only(start: 8),
|
||||
child: Text(
|
||||
time!,
|
||||
style: styles.time,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
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,
|
||||
required this.child,
|
||||
});
|
||||
|
||||
final Key? tapKey;
|
||||
final VoidCallback? onTap;
|
||||
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,
|
||||
onTapDown: enabled ? (_) => _press(true) : null,
|
||||
onTapUp: enabled ? (_) => _press(false) : null,
|
||||
onTapCancel: enabled ? () => _press(false) : null,
|
||||
child: ColoredBox(
|
||||
key: appListRowSurfaceKey,
|
||||
color: color,
|
||||
child: widget.child,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../adaptive/host.dart';
|
||||
|
||||
/// One choice in an [AppOverflowMenu].
|
||||
class AppMenuAction<T> {
|
||||
const AppMenuAction({
|
||||
required this.value,
|
||||
required this.label,
|
||||
this.key,
|
||||
this.destructive = false,
|
||||
});
|
||||
|
||||
final T value;
|
||||
final String label;
|
||||
|
||||
/// Placed on the menu item (Material) or the sheet action (Cupertino), so a
|
||||
/// test finds the same item on both hosts.
|
||||
final Key? key;
|
||||
|
||||
/// Shown in the destructive colour on Cupertino's action sheet.
|
||||
final bool destructive;
|
||||
}
|
||||
|
||||
/// The app's one row/header overflow control: the host's "more" icon that
|
||||
/// opens the row's secondary actions.
|
||||
///
|
||||
/// Material shows a [PopupMenuButton] with [Icons.more_vert]; Cupertino shows
|
||||
/// a [CupertinoButton] with [CupertinoIcons.ellipsis] that opens a
|
||||
/// [CupertinoActionSheet] (with Cancel). Every list row and the run header use
|
||||
/// this, so the overflow icon reads the same everywhere on a host.
|
||||
class AppOverflowMenu<T> extends StatelessWidget {
|
||||
const AppOverflowMenu({
|
||||
super.key,
|
||||
required this.actions,
|
||||
required this.onSelected,
|
||||
this.label = 'More actions',
|
||||
this.sheetTitle,
|
||||
this.enabled = true,
|
||||
this.cupertino,
|
||||
});
|
||||
|
||||
final List<AppMenuAction<T>> actions;
|
||||
|
||||
/// Called with the picked value once the menu or sheet has closed.
|
||||
final void Function(BuildContext context, T value) onSelected;
|
||||
|
||||
/// Tooltip (Material) and accessible label (both hosts).
|
||||
final String label;
|
||||
|
||||
/// The Cupertino sheet's title, usually the row's name.
|
||||
final String? sheetTitle;
|
||||
|
||||
final bool enabled;
|
||||
|
||||
/// Overrides the host detection (some features pick a style per platform).
|
||||
final bool? cupertino;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (actions.isEmpty) return const SizedBox.shrink();
|
||||
if (!(cupertino ?? usesCupertinoHost(context))) {
|
||||
return PopupMenuButton<T>(
|
||||
icon: const Icon(Icons.more_vert),
|
||||
tooltip: label,
|
||||
enabled: enabled,
|
||||
onSelected: (value) => onSelected(context, value),
|
||||
itemBuilder: (_) => [
|
||||
for (final action in actions)
|
||||
PopupMenuItem<T>(
|
||||
key: action.key,
|
||||
value: action.value,
|
||||
child: Text(action.label),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
// Its own node, so a row's merged label never swallows the button.
|
||||
return Semantics(
|
||||
container: true,
|
||||
button: true,
|
||||
enabled: enabled,
|
||||
label: label,
|
||||
excludeSemantics: true,
|
||||
child: CupertinoButton(
|
||||
padding: EdgeInsets.zero,
|
||||
minimumSize: const Size.square(kMinInteractiveDimensionCupertino),
|
||||
onPressed: enabled ? () => _showSheet(context) : null,
|
||||
child: const Icon(CupertinoIcons.ellipsis),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _showSheet(BuildContext context) async {
|
||||
final picked = await showCupertinoModalPopup<T>(
|
||||
context: context,
|
||||
builder: (sheet) => CupertinoActionSheet(
|
||||
title: sheetTitle == null ? null : Text(sheetTitle!),
|
||||
actions: [
|
||||
for (final action in actions)
|
||||
CupertinoActionSheetAction(
|
||||
key: action.key,
|
||||
isDestructiveAction: action.destructive,
|
||||
onPressed: () => Navigator.of(sheet).pop(action.value),
|
||||
child: Text(action.label),
|
||||
),
|
||||
],
|
||||
cancelButton: CupertinoActionSheetAction(
|
||||
isDefaultAction: true,
|
||||
onPressed: () => Navigator.of(sheet).pop(),
|
||||
child: const Text('Cancel'),
|
||||
),
|
||||
),
|
||||
);
|
||||
if (picked != null && context.mounted) onSelected(context, picked);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user