Skip to content

FTD#7 - 🛠 Fix: Multiline for HighlightPainter #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 28 additions & 19 deletions lib/src/modules/highlight/painter/highlight_painter.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_text_decorator/src/modules/underline/mixins/line_gap_mixin.dart';

/// A [CustomPainter] that draws a marker-style highlight over text.
///
Expand Down Expand Up @@ -28,8 +29,7 @@ import 'package:flutter/rendering.dart';
/// ),
/// )
/// ```
class HighlightPainter extends CustomPainter {

class HighlightPainter extends CustomPainter with LineGap {
HighlightPainter({required this.text, required this.color, required this.textStyle, this.strokeWidth});
final String text;
final Color color;
Expand All @@ -38,31 +38,40 @@ class HighlightPainter extends CustomPainter {

@override
void paint(Canvas canvas, Size size) {
final textSpan = TextSpan(text: text, style: textStyle);
final textPainter = TextPainter(
text: textSpan,
text: TextSpan(text: text, style: textStyle),
textDirection: TextDirection.ltr,
)..layout();
)..layout(maxWidth: size.width);

final strokeW = strokeWidth ?? textStyle.fontSize ?? 16;

final paint = Paint()
..color = color
..style = PaintingStyle.stroke
..strokeWidth = strokeWidth ?? textPainter.height;
..strokeWidth = strokeW;

double yOffset = 0;

const yThreshold = 5;
final horizontalOffset = textPainter.width / 4;
final path = Path()
..moveTo(0, textPainter.height / 2)
..cubicTo(
horizontalOffset,
size.height / 2 + yThreshold,
size.width - horizontalOffset,
size.height / 2 - yThreshold,
size.width,
size.height / 2 + yThreshold,
);
for (final line in textPainter.computeLineMetrics()) {
final width = size.width;
const yThreshold = 5;
final horizontalOffset = line.width / 4;
final height = line.height + line.descent;
final y = yOffset + height / 2;
final path = Path()
..moveTo(line.left, yOffset + line.height / 2)
..cubicTo(
horizontalOffset,
y + yThreshold,
width - horizontalOffset,
y - yThreshold,
width,
y + yThreshold,
);

canvas.drawPath(path, paint);
canvas.drawPath(path, paint);
yOffset += line.height + line.descent;
}
}

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ class HorizontalUnderlinePainter extends UnderlinePainter with LineGap {

final String text;

@override
@override
void paint(Canvas canvas, Size size) {
final paint = Paint()
Expand Down