Skip to content

Commit

Permalink
fix: Wire in background and foreground colors in TextPaint (#3191)
Browse files Browse the repository at this point in the history
Wire in background and foreground colors in TextPaint.
  • Loading branch information
luanpotter authored Jun 7, 2024
1 parent 9019a57 commit 983cfab
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/flame/lib/src/text/renderers/text_paint.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ class TextPaint extends TextRenderer {
decorationColor: style.decorationColor,
decorationStyle: style.decorationStyle,
decorationThickness: style.decorationThickness,
background: _extractBackground(style),
foreground: style.foreground,
);
}

Paint? _extractBackground(TextStyle style) {
if (style.background != null) {
return style.background;
}
if (style.backgroundColor != null) {
return Paint()..color = style.backgroundColor!;
}
return null;
}
}
28 changes: 28 additions & 0 deletions packages/flame/test/text/text_paint_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ void main() {
decorationColor: Color(0xFF0000FF),
decorationStyle: TextDecorationStyle.dashed,
decorationThickness: 1.5,
backgroundColor: Color(0xFFFF00FF),
);
final textPaint = TextPaint(style: flutterStyle);

Expand Down Expand Up @@ -77,6 +78,7 @@ void main() {
expect(inlineTextStyle.decorationColor, const Color(0xFF0000FF));
expect(inlineTextStyle.decorationStyle, TextDecorationStyle.dashed);
expect(inlineTextStyle.decorationThickness, 1.5);
expect(inlineTextStyle.background!.color, const Color(0xFFFF00FF));

final newTextPaint = inlineTextStyle.asTextRenderer();
expect(newTextPaint.style.fontSize, 12);
Expand Down Expand Up @@ -108,7 +110,33 @@ void main() {
expect(newTextPaint.style.decorationColor, const Color(0xFF0000FF));
expect(newTextPaint.style.decorationStyle, TextDecorationStyle.dashed);
expect(newTextPaint.style.decorationThickness, 1.5);
expect(newTextPaint.style.background!.color, const Color(0xFFFF00FF));
},
);
});

test(
'TextPaint and InlineTextStyle can receive Paint instead of Color',
() {
final flutterStyle = flutter.TextStyle(
fontSize: 12,
fontFamily: 'Times',
foreground: Paint()..color = const Color(0xFF0000FF),
background: Paint()..color = const Color(0xFFFF00FF),
);
final textPaint = TextPaint(style: flutterStyle);

final inlineTextStyle = textPaint.asInlineTextStyle();
expect(inlineTextStyle.fontSize, 12);
expect(inlineTextStyle.fontFamily, 'Times');
expect(inlineTextStyle.foreground!.color, const Color(0xFF0000FF));
expect(inlineTextStyle.background!.color, const Color(0xFFFF00FF));

final newTextPaint = inlineTextStyle.asTextRenderer();
expect(newTextPaint.style.fontSize, 12);
expect(newTextPaint.style.fontFamily, 'Times');
expect(newTextPaint.style.foreground!.color, const Color(0xFF0000FF));
expect(newTextPaint.style.background!.color, const Color(0xFFFF00FF));
},
);
}

0 comments on commit 983cfab

Please sign in to comment.