30d2d8c4f4
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
151 lines
4.7 KiB
Dart
151 lines
4.7 KiB
Dart
import 'package:flutter/material.dart';
|
|
import '../theme/app_design_tokens.dart';
|
|
import '../theme/high_contrast_palette.dart';
|
|
|
|
/// The leading circle every list row uses, as large as a native contact
|
|
/// avatar ([CircleAvatar]'s default).
|
|
const double kAppListAvatarSize = 40;
|
|
|
|
/// The leading avatar's size under the design at [context]:
|
|
/// [kAppListAvatarSize], or a dense design's smaller one.
|
|
double appListAvatarSize(BuildContext context) =>
|
|
AppDesignTokens.of(context).density.avatarSize;
|
|
|
|
/// 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 tokens = AppDesignTokens.of(context);
|
|
var mark = foreground ?? Theme.of(context).colorScheme.onSurfaceVariant;
|
|
// A design with a contrast floor keeps the mark (and its frame) at it.
|
|
final floor = tokens.minContrast;
|
|
if (floor != null) {
|
|
mark = colorWithContrast(
|
|
mark,
|
|
Theme.of(context).scaffoldBackgroundColor,
|
|
floor,
|
|
);
|
|
}
|
|
final fill =
|
|
background ??
|
|
(foreground == null
|
|
? Theme.of(context).colorScheme.surfaceContainerHighest
|
|
: mark.withValues(alpha: 0.14));
|
|
// The mark keeps the same proportion of the circle at every density.
|
|
final size = tokens.density.avatarSize;
|
|
final scale = size / kAppListAvatarSize;
|
|
final initialStyle =
|
|
(Theme.of(context).textTheme.titleMedium ?? const TextStyle()).copyWith(
|
|
color: mark,
|
|
fontSize: 18 * scale,
|
|
fontWeight: FontWeight.w700,
|
|
height: 1,
|
|
);
|
|
// A flat design (Terminal) frames the mark in a square box instead of a
|
|
// tinted circle.
|
|
final decoration = tokens.flat
|
|
? BoxDecoration(
|
|
border: Border.all(
|
|
color: floor == null ? mark.withValues(alpha: 0.55) : mark,
|
|
width: tokens.stroke,
|
|
),
|
|
borderRadius: tokens.corners(BorderRadius.zero),
|
|
)
|
|
: BoxDecoration(color: fill, shape: BoxShape.circle);
|
|
final circle = SizedBox.square(
|
|
dimension: size,
|
|
child: DecoratedBox(
|
|
decoration: decoration,
|
|
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: EdgeInsets.all(8 * scale),
|
|
child: FittedBox(
|
|
fit: BoxFit.scaleDown,
|
|
child: IconTheme.merge(
|
|
data: IconThemeData(color: mark, size: 22 * scale),
|
|
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),
|
|
);
|
|
}
|
|
}
|