Skip to content
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

fix: render objects not rendering text #98

Merged
merged 1 commit into from
Sep 13, 2023
Merged
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
13 changes: 11 additions & 2 deletions lib/src/blocked_text_image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,17 @@ class BlockedTextCanvasAdapter implements Canvas {
/// Draws a rectangle on the canvas where the [paragraph]
/// would otherwise be rendered
@override
void drawParagraph(ui.Paragraph paragraph, ui.Offset offset) => parent
.drawRect(offset & Size(paragraph.width, paragraph.height), Paint());
void drawParagraph(ui.Paragraph paragraph, ui.Offset offset) =>
parent.drawRect(
offset &
Size(
paragraph.width.isFinite
? paragraph.width
: paragraph.longestLine,
paragraph.height,
),
Paint(),
);

@override
void clipPath(ui.Path path, {bool doAntiAlias = true}) =>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
62 changes: 62 additions & 0 deletions test/smoke_tests/render_object_smoke_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import 'package:alchemist/alchemist.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
group('smoke test', () {
goldenTest(
'succeeds for custom render object drawing text',
fileName: 'render_object_text_smoke_test',
builder: () => const SizedBox(
height: 400,
width: 400,
child: _CustomExampleRenderObject(),
),
);
});
}

class _CustomExampleRenderObject extends LeafRenderObjectWidget {
const _CustomExampleRenderObject({Key? key}) : super(key: key);

@override
_CustomExampleRenderBox createRenderObject(BuildContext context) {
return _CustomExampleRenderBox();
}
}

class _CustomExampleRenderBox extends RenderBox {
@override
void paint(PaintingContext context, Offset offset) {
final textPainter = TextPainter(
text: const TextSpan(
text: 'text',
style: TextStyle(
fontFamily: 'Roboto',
color: Colors.black,
fontSize: 24,
),
),
textDirection: TextDirection.ltr,
)..layout();
textPainter.paint(
context.canvas,
const Offset(200, 200) - textPainter.size.center(Offset.zero),
);
}

@override
void performLayout() {
size = computeDryLayout(constraints);
}

@override
Size computeDryLayout(BoxConstraints constraints) {
return constraints.constrain(
Size(
200,
constraints.maxHeight,
),
);
}
}
21 changes: 20 additions & 1 deletion test/src/blocked_text_canvas_adapter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ void main() {
late ui.Canvas parent;
late BlockedTextCanvasAdapter subject;

setUpAll(() {
registerFallbackValue(ui.Paint());
});

setUp(() {
parent = MockCanvas();
subject = BlockedTextCanvasAdapter(parent);
Expand All @@ -31,7 +35,22 @@ void main() {
subject.drawParagraph(paragraph, offset);
verify(
() => parent.drawRect(
offset & ui.Size(paragraph.width, paragraph.height),
offset & const ui.Size(200, 400),
any(that: isA<ui.Paint>()),
),
).called(1);
});

test('drawParagraph draws a rectangle for infinite width', () {
const offset = ui.Offset(20, 40);
final paragraph = MockParagraph();
when(() => paragraph.width).thenReturn(double.infinity);
when(() => paragraph.longestLine).thenReturn(400);
when(() => paragraph.height).thenReturn(400);
subject.drawParagraph(paragraph, offset);
verify(
() => parent.drawRect(
offset & const ui.Size(400, 400),
any(that: isA<ui.Paint>()),
),
).called(1);
Expand Down
Loading