Files
pipelines/app/lib/ui/layout/app_list_avatar.dart
alan a5912d9a1e Remove Cupertino branches from layout widgets
Keep the public cupertino parameters for existing callers while making the layout widgets and focused tests Material-only. Remove unreachable Cupertino imports, helpers, widgets, and test variants without changing the Material path.
2026-11-11 16:35:38 +00:00

120 lines
3.5 KiB
Dart

import 'package:flutter/material.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 mark = foreground ?? Theme.of(context).colorScheme.onSurfaceVariant;
final fill =
background ??
(foreground == null
? Theme.of(context).colorScheme.surfaceContainerHighest
: mark.withValues(alpha: 0.14));
final initialStyle =
(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),
);
}
}