@@ -59,6 +59,8 @@ class TextLayoutService {
59
59
String ? get ellipsis => paragraph.paragraphStyle.ellipsis;
60
60
bool get hasEllipsis => ellipsis != null ;
61
61
62
+ late final List <LineBreakResult > breaks = computeLineBreaks (paragraph.toPlainText ());
63
+
62
64
/// Performs the layout on a paragraph given the [constraints] .
63
65
///
64
66
/// The function starts by resetting all layout-related properties. Then it
@@ -142,7 +144,7 @@ class TextLayoutService {
142
144
spanIndex++ ;
143
145
} else if (span is FlatTextSpan ) {
144
146
spanometer.currentSpan = span;
145
- final DirectionalPosition nextBreak = currentLine. findNextBreak ();
147
+ final DirectionalPosition nextBreak = findNextBreak (currentLine );
146
148
final double additionalWidth =
147
149
currentLine.getAdditionalWidthTo (nextBreak.lineBreak);
148
150
@@ -275,7 +277,7 @@ class TextLayoutService {
275
277
spanIndex++ ;
276
278
} else if (span is FlatTextSpan ) {
277
279
spanometer.currentSpan = span;
278
- final DirectionalPosition nextBreak = currentLine. findNextBreak ();
280
+ final DirectionalPosition nextBreak = findNextBreak (currentLine );
279
281
280
282
// For the purpose of max intrinsic width, we don't care if the line
281
283
// fits within the constraints or not. So we always extend it.
@@ -395,6 +397,24 @@ class TextLayoutService {
395
397
}
396
398
}
397
399
400
+ /// Finds the next line break after the end of the line being built.
401
+ DirectionalPosition findNextBreak (LineBuilder lineBuilder) {
402
+ final String text = paragraph.toPlainText ();
403
+ final int maxEnd = lineBuilder.spanometer.currentSpan.end;
404
+
405
+ int i = lineBuilder.end.index;
406
+ LineBreakResult nextBreak = breaks[i];
407
+ while (++ i <= maxEnd) {
408
+ nextBreak = breaks[i];
409
+ if (nextBreak.type != LineBreakType .prohibited) {
410
+ break ;
411
+ }
412
+ }
413
+
414
+ // The current end of the line is the beginning of the next block.
415
+ return getDirectionalBlockEnd (text, lineBuilder.end, nextBreak);
416
+ }
417
+
398
418
/// Positions a sequence of boxes in the direction opposite to the paragraph
399
419
/// text direction.
400
420
///
@@ -1540,23 +1560,6 @@ class LineBuilder {
1540
1560
}
1541
1561
}
1542
1562
1543
- LineBreakResult ? _cachedNextBreak;
1544
-
1545
- /// Finds the next line break after the end of this line.
1546
- DirectionalPosition findNextBreak () {
1547
- LineBreakResult ? nextBreak = _cachedNextBreak;
1548
- final String text = paragraph.toPlainText ();
1549
- // Don't recompute the `nextBreak` until the line has reached the previously
1550
- // computed `nextBreak`.
1551
- if (nextBreak == null || end.index >= nextBreak.index) {
1552
- final int maxEnd = spanometer.currentSpan.end;
1553
- nextBreak = nextLineBreak (text, end.index, maxEnd: maxEnd);
1554
- _cachedNextBreak = nextBreak;
1555
- }
1556
- // The current end of the line is the beginning of the next block.
1557
- return getDirectionalBlockEnd (text, end, nextBreak);
1558
- }
1559
-
1560
1563
/// Creates a new [LineBuilder] to build the next line in the paragraph.
1561
1564
LineBuilder nextLine () {
1562
1565
return LineBuilder ._(
0 commit comments