168 lines
4.9 KiB
Dart
168 lines
4.9 KiB
Dart
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
import '../features/attention/domain/attention_models.dart';
|
|
import '../ui/motion/motion.dart';
|
|
|
|
/// Mirrors the Runs-heading bell count onto the Runs destination icon.
|
|
///
|
|
/// The bell itself lives in the Runs heading, so without this the count would
|
|
/// be visible only on that destination; the web mirrors it on the rail and
|
|
/// dock tab. Material hosts use the framework [Badge]; Cupertino has no badge
|
|
/// control, so its tab bar and sidebar get the host's red count overlay. Zero
|
|
/// draws the bare icon, and a count labels the item exactly like the bell:
|
|
/// `Notifications, {n} needing attention`.
|
|
class AttentionDestinationIcon extends StatelessWidget {
|
|
const AttentionDestinationIcon({
|
|
super.key,
|
|
required this.count,
|
|
required this.cupertino,
|
|
required this.child,
|
|
});
|
|
|
|
final int count;
|
|
final bool cupertino;
|
|
final Widget child;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final visible = count > 0;
|
|
final Widget icon = cupertino
|
|
? _CupertinoCountOverlay(count: count, visible: visible, child: child)
|
|
: Badge(
|
|
label: visible
|
|
? MotionSwitcher(
|
|
scale: true,
|
|
child: Text('$count', key: ValueKey(count)),
|
|
)
|
|
: null,
|
|
isLabelVisible: visible,
|
|
child: child,
|
|
);
|
|
if (!visible) return icon;
|
|
// The number alone would read as a stray "3"; the item carries the bell's
|
|
// full label instead.
|
|
return Semantics(
|
|
label: attentionBadgeLabel(count),
|
|
child: ExcludeSemantics(child: icon),
|
|
);
|
|
}
|
|
}
|
|
|
|
/// The iOS tab-badge idiom: a red stadium with the count over the icon's
|
|
/// trailing corner. Content-driven size; only the overlap offset is fixed.
|
|
class _CupertinoCountOverlay extends StatelessWidget {
|
|
const _CupertinoCountOverlay({
|
|
required this.count,
|
|
required this.visible,
|
|
required this.child,
|
|
});
|
|
|
|
final int count;
|
|
final bool visible;
|
|
final Widget child;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
if (!visible) return child;
|
|
final style = CupertinoTheme.of(
|
|
context,
|
|
).textTheme.tabLabelTextStyle.copyWith(color: CupertinoColors.white);
|
|
return Stack(
|
|
clipBehavior: Clip.none,
|
|
children: [
|
|
child,
|
|
PositionedDirectional(
|
|
top: -6,
|
|
end: -10,
|
|
child: DecoratedBox(
|
|
decoration: ShapeDecoration(
|
|
color: CupertinoColors.systemRed.resolveFrom(context),
|
|
shape: const StadiumBorder(),
|
|
),
|
|
child: Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 5, vertical: 1),
|
|
child: MotionSwitcher(
|
|
scale: true,
|
|
child: Text('$count', key: ValueKey(count), style: style),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
}
|
|
|
|
/// A small count on a tab icon: blue for activity (runs in progress, unread
|
|
/// chats), so it reads apart from the red needs-you count.
|
|
class ActivityBadge extends StatelessWidget {
|
|
const ActivityBadge({
|
|
super.key,
|
|
required this.count,
|
|
required this.label,
|
|
required this.cupertino,
|
|
required this.child,
|
|
this.bottom = false,
|
|
});
|
|
|
|
final int count;
|
|
final String label;
|
|
final bool cupertino;
|
|
final bool bottom;
|
|
final Widget child;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final visible = count > 0;
|
|
final color = cupertino
|
|
? CupertinoColors.systemBlue.resolveFrom(context)
|
|
: Theme.of(context).colorScheme.primary;
|
|
final text = cupertino
|
|
? CupertinoColors.white
|
|
: Theme.of(context).colorScheme.onPrimary;
|
|
final stack = Stack(
|
|
clipBehavior: Clip.none,
|
|
children: [
|
|
child,
|
|
PositionedDirectional(
|
|
top: bottom ? null : -6,
|
|
bottom: bottom ? -6 : null,
|
|
end: -10,
|
|
child: AnimatedScale(
|
|
scale: visible ? 1 : 0,
|
|
duration: Motion.of(context, Motion.quick),
|
|
curve: Motion.curve,
|
|
child: DecoratedBox(
|
|
decoration: ShapeDecoration(
|
|
color: color,
|
|
shape: const StadiumBorder(),
|
|
),
|
|
child: Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 5, vertical: 1),
|
|
child: MotionSwitcher(
|
|
scale: true,
|
|
child: Text(
|
|
visible ? '$count' : '',
|
|
key: ValueKey(count),
|
|
style: TextStyle(
|
|
color: text,
|
|
fontSize: 10,
|
|
fontWeight: FontWeight.w700,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
if (!visible) return stack;
|
|
return Semantics(
|
|
label: '$count $label',
|
|
child: ExcludeSemantics(child: stack),
|
|
);
|
|
}
|
|
}
|