d9ec43e607
Lay the running highlight out by rounded-border arc length so it advances at a steady linear speed without a loop jump, while preserving the existing colours and reduced-motion border. Add focused geometry tests and implementation notes.
152 lines
4.4 KiB
Dart
152 lines
4.4 KiB
Dart
import 'package:flutter/material.dart';
|
||
|
||
import 'composer_running_arc.dart';
|
||
|
||
/// The composer's "agent is working" edge: a soft highlight that travels once
|
||
/// around the pill's rounded border in [primary] and [tertiary], with a faint
|
||
/// glow beneath it. Mount it only while a turn runs; disposing it stops the
|
||
/// animation, so the border ends cleanly with the turn.
|
||
///
|
||
/// With reduced motion (the platform's disable-animations setting) it draws
|
||
/// a static, evenly highlighted border instead and never ticks.
|
||
class ComposerRunningBorder extends StatefulWidget {
|
||
const ComposerRunningBorder({
|
||
super.key,
|
||
required this.shape,
|
||
required this.primary,
|
||
required this.tertiary,
|
||
});
|
||
|
||
/// The pill's shape, so the highlight follows its edge.
|
||
final OutlinedBorder shape;
|
||
final Color primary;
|
||
final Color tertiary;
|
||
|
||
/// One lap of the highlight around the border (about 2.5–3 s).
|
||
static const Duration period = Duration(milliseconds: 2750);
|
||
|
||
/// The stroke drawn just inside the pill's edge.
|
||
static const double strokeWidth = 2;
|
||
|
||
/// The static border's opacity under reduced motion.
|
||
static const double staticOpacity = 0.7;
|
||
|
||
@override
|
||
State<ComposerRunningBorder> createState() => _ComposerRunningBorderState();
|
||
}
|
||
|
||
class _ComposerRunningBorderState extends State<ComposerRunningBorder>
|
||
with SingleTickerProviderStateMixin {
|
||
late final AnimationController _lap = AnimationController(
|
||
vsync: this,
|
||
duration: ComposerRunningBorder.period,
|
||
);
|
||
bool _reduced = false;
|
||
|
||
@override
|
||
void initState() {
|
||
super.initState();
|
||
_lap.repeat();
|
||
}
|
||
|
||
@override
|
||
void didChangeDependencies() {
|
||
super.didChangeDependencies();
|
||
final reduced = MediaQuery.maybeDisableAnimationsOf(context) ?? false;
|
||
if (reduced == _reduced) return;
|
||
_reduced = reduced;
|
||
if (reduced) {
|
||
_lap.stop();
|
||
} else {
|
||
_lap.repeat();
|
||
}
|
||
}
|
||
|
||
@override
|
||
void dispose() {
|
||
_lap.dispose();
|
||
super.dispose();
|
||
}
|
||
|
||
@override
|
||
Widget build(BuildContext context) => RepaintBoundary(
|
||
key: const Key('chat-composer-running-border'),
|
||
child: CustomPaint(
|
||
painter: _RunningBorderPainter(
|
||
shape: widget.shape,
|
||
primary: widget.primary,
|
||
tertiary: widget.tertiary,
|
||
lap: _reduced ? null : _lap,
|
||
),
|
||
),
|
||
);
|
||
}
|
||
|
||
class _RunningBorderPainter extends CustomPainter {
|
||
_RunningBorderPainter({
|
||
required this.shape,
|
||
required this.primary,
|
||
required this.tertiary,
|
||
required this.lap,
|
||
}) : super(repaint: lap);
|
||
|
||
final OutlinedBorder shape;
|
||
final Color primary;
|
||
final Color tertiary;
|
||
|
||
/// The lap's progress, or null for the static reduced-motion border.
|
||
final Animation<double>? lap;
|
||
|
||
@override
|
||
void paint(Canvas canvas, Size size) {
|
||
if (size.isEmpty) return;
|
||
const width = ComposerRunningBorder.strokeWidth;
|
||
final rect = Offset.zero & size;
|
||
final path = shape.getOuterPath(rect.deflate(width / 2));
|
||
final glow = Paint()
|
||
..style = PaintingStyle.stroke
|
||
..strokeWidth = width * 3
|
||
..maskFilter = const MaskFilter.blur(BlurStyle.normal, 4);
|
||
final line = Paint()
|
||
..style = PaintingStyle.stroke
|
||
..strokeWidth = width;
|
||
final progress = lap?.value;
|
||
if (progress == null) {
|
||
final solid = primary.withValues(
|
||
alpha: ComposerRunningBorder.staticOpacity,
|
||
);
|
||
glow.color = solid.withValues(alpha: solid.a * .4);
|
||
line.color = solid;
|
||
canvas
|
||
..drawPath(path, glow)
|
||
..drawPath(path, line);
|
||
return;
|
||
}
|
||
// The highlight is laid along the outline by arc length, one piece at a
|
||
// time, so its speed is even around corners and straight edges alike.
|
||
final metrics = path.computeMetrics().toList();
|
||
final length = metrics.fold<double>(0, (sum, m) => sum + m.length);
|
||
final segments = runningHighlightSegments(
|
||
progress: progress,
|
||
length: length,
|
||
primary: primary,
|
||
tertiary: tertiary,
|
||
);
|
||
for (final segment in segments) {
|
||
final piece = runningArcPath(metrics, segment.start, segment.end);
|
||
glow.color = segment.color;
|
||
line.color = segment.color;
|
||
canvas
|
||
..drawPath(piece, glow)
|
||
..drawPath(piece, line);
|
||
}
|
||
}
|
||
|
||
@override
|
||
bool shouldRepaint(_RunningBorderPainter oldDelegate) =>
|
||
oldDelegate.shape != shape ||
|
||
oldDelegate.primary != primary ||
|
||
oldDelegate.tertiary != tertiary ||
|
||
oldDelegate.lap != lap;
|
||
}
|