import 'dart:math' as math; import 'package:flutter/material.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. static const Duration period = Duration(seconds: 4); /// 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 createState() => _ComposerRunningBorderState(); } class _ComposerRunningBorderState extends State 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? 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; } else { final shader = SweepGradient( colors: [ primary.withValues(alpha: 0), primary, tertiary, tertiary.withValues(alpha: 0), primary.withValues(alpha: 0), ], stops: const [0, .12, .24, .38, 1], transform: GradientRotation(progress * 2 * math.pi), ).createShader(rect); glow.shader = shader; line.shader = shader; } canvas ..drawPath(path, glow) ..drawPath(path, line); } @override bool shouldRepaint(_RunningBorderPainter oldDelegate) => oldDelegate.shape != shape || oldDelegate.primary != primary || oldDelegate.tertiary != tertiary || oldDelegate.lap != lap; }