Skip to content

Commit 346ec0c

Browse files
committed
[FTD #6] fix gap of horizontal underline painter
1 parent 28e62e4 commit 346ec0c

1 file changed

Lines changed: 13 additions & 23 deletions

File tree

lib/src/modules/underline/painter/horizontal_underline_painter.dart

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class HorizontalUnderlinePainter extends UnderlinePainter {
3939
super.horizontalOffset,
4040
});
4141

42+
@override
4243
@override
4344
void paint(Canvas canvas, Size size) {
4445
final paint = Paint()
@@ -47,44 +48,33 @@ class HorizontalUnderlinePainter extends UnderlinePainter {
4748
..strokeWidth = strokeWidth
4849
..strokeCap = StrokeCap.square;
4950

50-
final currentTextStyle = super.textStyle ?? const TextStyle();
51-
final textSpan = TextSpan(text: text, style: currentTextStyle);
5251
final textPainter = TextPainter(
53-
text: textSpan,
52+
text: TextSpan(text: text, style: textStyle ?? const TextStyle()),
5453
textDirection: ui.TextDirection.ltr,
5554
)..layout(maxWidth: size.width);
5655

57-
final List<ui.LineMetrics> lines = textPainter.computeLineMetrics();
58-
59-
for (int i = 0; i < lines.length; i++) {
60-
final line = lines[i];
56+
final lines = textPainter.computeLineMetrics();
57+
double yOffset = 0;
6158

59+
for (final line in lines) {
6260
final double startX = line.left + horizontalOffset.left;
6361
final double endX = line.left + line.width - horizontalOffset.right;
62+
final double underlineY = yOffset + line.ascent + line.descent + strokeWidth;
6463

65-
final gapY = _calculateGapBetweenLines(i, line);
66-
67-
if (_isLineLengthPositiv(startX, endX)) {
68-
canvas.drawLine(Offset(startX, gapY), Offset(endX, gapY), paint);
64+
if (_isLineLengthPositive(startX, endX)) {
65+
canvas.drawLine(Offset(startX, underlineY), Offset(endX, underlineY), paint);
6966
}
67+
68+
yOffset += _calculateGapBetweenLines(line.lineNumber, line);
7069
}
7170
}
7271

73-
bool _isLineLengthPositiv(double x, double y) {
74-
return x < y;
75-
}
72+
bool _isLineLengthPositive(double x, double y) => x < y;
7673

7774
double _calculateGapBetweenLines(int lineIndex, ui.LineMetrics line) {
78-
double desiredGap = 5;
79-
if (lineIndex == 0) {
80-
desiredGap = 1;
81-
}
82-
83-
return line.baseline + line.descent + desiredGap + (strokeWidth / 2.0);
75+
return line.height + (line.descent / 2) + strokeWidth;
8476
}
8577

8678
@override
87-
bool shouldRepaint(covariant CustomPainter oldDelegate) {
88-
return false;
89-
}
79+
bool shouldRepaint(covariant CustomPainter oldDelegate) => false;
9080
}

0 commit comments

Comments
 (0)