Skip to content

Commit

Permalink
fix: render objects not rendering text (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirpal authored Sep 13, 2023
1 parent b54138c commit ac2c611
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 3 deletions.
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

0 comments on commit ac2c611

Please sign in to comment.