@@ -27,6 +27,10 @@ class StrokePainter extends CustomPainter {
2727 final StrokeCapEnum strokeCap;
2828
2929 /// The dash pattern of the stroke. Empty if solid.
30+ /// The most dashes painted along one path, so a pattern with tiny
31+ /// entries can't stall a frame.
32+ static const int maxDashes = 20000 ;
33+
3034 final List <double > dashPattern;
3135
3236 /// The side of the stroke. Whether it is all, top, bottom, left, or right.
@@ -41,10 +45,6 @@ class StrokePainter extends CustomPainter {
4145 /// The border radius of the box.
4246 final BorderRadius borderRadius;
4347
44- /// A helper list to iterate the dashes cleanly for dashed or dotted borders.
45- /// This is constructed from the dash pattern.
46- final LoopingList <double > dashArray;
47-
4848 /// A convenience getter for the inside alignment of the stroke. Helps
4949 /// simplify some calculations.
5050 double get isInside => strokeAlign == StrokeAlignC .inside ? - 1 : 1 ;
@@ -68,7 +68,7 @@ class StrokePainter extends CustomPainter {
6868 this .strokeAlign = StrokeAlignC .inside,
6969 this .strokeCap = StrokeCapEnum .square,
7070 this .borderRadius = BorderRadius .zero,
71- }) : dashArray = LoopingList (dashPattern) ;
71+ });
7272
7373 @override
7474 void paint (Canvas canvas, Size size) {
@@ -127,14 +127,22 @@ class StrokePainter extends CustomPainter {
127127 // We loop over each "segment" of the path. Not up to us to know what
128128 // those segments are, we just work with them. Flutter likes being
129129 // mysterious.
130+ // A pattern that doesn't advance (no positive entry) would loop
131+ // forever; the cap keeps a tiny pattern on a long path from stalling a
132+ // frame.
133+ final double period = dashPattern.fold (0.0 , (a, b) => a + max (0.0 , b));
130134 for (final PathMetric metric in path.computeMetrics ()) {
135+ if (period <= 0 ) break ;
131136 double totalDistance = 0 ;
132137 bool draw = true ;
138+ int dashes = 0 ;
139+ // Each path starts the pattern from its first entry.
140+ final LoopingList <double > dashArray = LoopingList (dashPattern);
133141
134142 // We need to draw a dashed line, so we iterate with a while loop
135143 // our desired dash distance.
136- while (totalDistance < metric.length) {
137- final double dashDistance = dashArray.nextValue ();
144+ while (totalDistance < metric.length && dashes ++ < maxDashes ) {
145+ final double dashDistance = max ( 0.0 , dashArray.nextValue () );
138146
139147 if (draw) {
140148 // Now that we're about to draw a singular dash, we need to do it
@@ -247,8 +255,9 @@ class StrokePainter extends CustomPainter {
247255 ..strokeCap = StrokeCap .butt;
248256 double distance = 0 ;
249257 bool draw = true ;
250- while (distance < length) {
251- final double dash = dashes.nextValue ();
258+ int count = 0 ;
259+ while (distance < length && count++ < maxDashes) {
260+ final double dash = max (0.0 , dashes.nextValue ());
252261 if (draw && dash > 0 ) {
253262 canvas.drawLine (
254263 start + direction * distance,
0 commit comments