Skip to content

Commit 311d58f

Browse files
committed
fix: Wire in background and foreground colors in TextPaint
1 parent 9019a57 commit 311d58f

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

packages/flame/lib/src/text/renderers/text_paint.dart

+12
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,18 @@ class TextPaint extends TextRenderer {
8282
decorationColor: style.decorationColor,
8383
decorationStyle: style.decorationStyle,
8484
decorationThickness: style.decorationThickness,
85+
background: _extractBackground(style),
86+
foreground: style.foreground,
8587
);
8688
}
89+
90+
Paint? _extractBackground(TextStyle style) {
91+
if (style.background != null) {
92+
return style.background;
93+
}
94+
if (style.backgroundColor != null) {
95+
return Paint()..color = style.backgroundColor!;
96+
}
97+
return null;
98+
}
8799
}

packages/flame/test/text/text_paint_test.dart

+28
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ void main() {
4444
decorationColor: Color(0xFF0000FF),
4545
decorationStyle: TextDecorationStyle.dashed,
4646
decorationThickness: 1.5,
47+
backgroundColor: Color(0xFFFF00FF),
4748
);
4849
final textPaint = TextPaint(style: flutterStyle);
4950

@@ -77,6 +78,7 @@ void main() {
7778
expect(inlineTextStyle.decorationColor, const Color(0xFF0000FF));
7879
expect(inlineTextStyle.decorationStyle, TextDecorationStyle.dashed);
7980
expect(inlineTextStyle.decorationThickness, 1.5);
81+
expect(inlineTextStyle.background!.color, const Color(0xFFFF00FF));
8082

8183
final newTextPaint = inlineTextStyle.asTextRenderer();
8284
expect(newTextPaint.style.fontSize, 12);
@@ -108,7 +110,33 @@ void main() {
108110
expect(newTextPaint.style.decorationColor, const Color(0xFF0000FF));
109111
expect(newTextPaint.style.decorationStyle, TextDecorationStyle.dashed);
110112
expect(newTextPaint.style.decorationThickness, 1.5);
113+
expect(newTextPaint.style.background!.color, const Color(0xFFFF00FF));
111114
},
112115
);
113116
});
117+
118+
test(
119+
'TextPaint and InlineTextStyle can receive Paint instead of Color',
120+
() {
121+
final flutterStyle = flutter.TextStyle(
122+
fontSize: 12,
123+
fontFamily: 'Times',
124+
foreground: Paint()..color = const Color(0xFF0000FF),
125+
background: Paint()..color = const Color(0xFFFF00FF),
126+
);
127+
final textPaint = TextPaint(style: flutterStyle);
128+
129+
final inlineTextStyle = textPaint.asInlineTextStyle();
130+
expect(inlineTextStyle.fontSize, 12);
131+
expect(inlineTextStyle.fontFamily, 'Times');
132+
expect(inlineTextStyle.foreground!.color, const Color(0xFF0000FF));
133+
expect(inlineTextStyle.background!.color, const Color(0xFFFF00FF));
134+
135+
final newTextPaint = inlineTextStyle.asTextRenderer();
136+
expect(newTextPaint.style.fontSize, 12);
137+
expect(newTextPaint.style.fontFamily, 'Times');
138+
expect(newTextPaint.style.foreground!.color, const Color(0xFF0000FF));
139+
expect(newTextPaint.style.background!.color, const Color(0xFFFF00FF));
140+
},
141+
);
114142
}

0 commit comments

Comments
 (0)