import 'dart:math' as math; import 'package:flutter/material.dart'; /// A rounded rectangle's outline measured by arc length, drawn piece by piece /// with plain lines and arcs. /// /// The running highlight moves every frame. Cutting its pieces out of a /// [Path] (`PathMetric.extractPath`) costs a new path per piece per frame, /// which the web engine rebuilds again when it draws; for the pill's shape the /// same pieces are straight lines and circular arcs, so they are drawn as such. /// /// The outline starts where [Path.addRRect] starts it (the left edge's bottom /// end) and runs the same way (up the left edge, then clockwise), so a range /// of arc length covers the same part of the edge as on the path. class RunningRRectOutline { RunningRRectOutline._(this._rect, this._radius) : _side = _rect.height - 2 * _radius, _across = _rect.width - 2 * _radius, _corner = math.pi / 2 * _radius; /// The outline [shape] draws around [rect], or null when it is not a /// rectangle with one circular radius on every corner (then the path is /// measured instead). static RunningRRectOutline? of(OutlinedBorder shape, Rect rect) { if (shape is! RoundedRectangleBorder || rect.isEmpty) return null; final radii = shape.borderRadius.resolve(TextDirection.ltr); final r = radii.topLeft; if (r.x != r.y || radii.topRight != r || radii.bottomLeft != r || radii.bottomRight != r) { return null; } // Radii larger than half a side are scaled down, as a path's are. final radius = math.max( 0.0, math.min(r.x, math.min(rect.width, rect.height) / 2), ); return RunningRRectOutline._(rect, radius); } final Rect _rect; final double _radius; final double _side; final double _across; final double _corner; /// The outline's whole length. double get length => 2 * (_side + _across) + 4 * _corner; /// Strokes the outline from arc length [start] to [end] (within one lap). void draw(Canvas canvas, double start, double end, Paint paint) { final l = _rect.left, t = _rect.top, r = _rect.right, b = _rect.bottom; final rad = _radius; var at = 0.0; // Each part of the outline, in path order: a straight edge from one point // to another, or a quarter circle from a start angle. void edge(double length, Offset from, Offset to) { final lo = math.max(start, at), hi = math.min(end, at + length); if (hi > lo && length > 0) { canvas.drawLine( Offset.lerp(from, to, (lo - at) / length)!, Offset.lerp(from, to, (hi - at) / length)!, paint, ); } at += length; } void corner(Offset centre, double angle) { final lo = math.max(start, at), hi = math.min(end, at + _corner); if (hi > lo && rad > 0) { canvas.drawArc( Rect.fromCircle(center: centre, radius: rad), angle + (lo - at) / rad, (hi - lo) / rad, false, paint, ); } at += _corner; } edge(_side, Offset(l, b - rad), Offset(l, t + rad)); corner(Offset(l + rad, t + rad), math.pi); edge(_across, Offset(l + rad, t), Offset(r - rad, t)); corner(Offset(r - rad, t + rad), 3 * math.pi / 2); edge(_side, Offset(r, t + rad), Offset(r, b - rad)); corner(Offset(r - rad, b - rad), 0); edge(_across, Offset(r - rad, b), Offset(l + rad, b)); corner(Offset(l + rad, b - rad), math.pi / 2); } /// The point at arc length [distance] along the outline (for tests). Offset pointAt(double distance) { final l = _rect.left, t = _rect.top, r = _rect.right, b = _rect.bottom; final rad = _radius; var d = distance - (distance / length).floorToDouble() * length; final parts = <(double, Offset Function(double))>[ (_side, (s) => Offset(l, b - rad - s)), (_corner, (s) => _arc(Offset(l + rad, t + rad), math.pi + s / rad)), (_across, (s) => Offset(l + rad + s, t)), (_corner, (s) => _arc(Offset(r - rad, t + rad), 1.5 * math.pi + s / rad)), (_side, (s) => Offset(r, t + rad + s)), (_corner, (s) => _arc(Offset(r - rad, b - rad), s / rad)), (_across, (s) => Offset(r - rad - s, b)), (_corner, (s) => _arc(Offset(l + rad, b - rad), .5 * math.pi + s / rad)), ]; for (final (len, at) in parts) { if (len > 0 && d <= len) return at(d); d -= len; } return Offset(l, b - rad); } Offset _arc(Offset centre, double angle) => centre + Offset(math.cos(angle), math.sin(angle)) * _radius; }