From b110b35b980afe5bcee8f8a188bf2ce58bdc1655 Mon Sep 17 00:00:00 2001 From: Luan Nico Date: Mon, 30 Dec 2024 11:50:10 -0500 Subject: [PATCH] feat: Add validation against passing both color and foreground to InlineTextStyle --- .../src/text/styles/inline_text_style.dart | 12 +++++-- packages/flame/test/text/text_style_test.dart | 32 +++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/packages/flame/lib/src/text/styles/inline_text_style.dart b/packages/flame/lib/src/text/styles/inline_text_style.dart index 51e2cb748e0..7217eccf1b8 100644 --- a/packages/flame/lib/src/text/styles/inline_text_style.dart +++ b/packages/flame/lib/src/text/styles/inline_text_style.dart @@ -28,7 +28,10 @@ class InlineTextStyle extends FlameTextStyle { this.decorationThickness, this.background, this.foreground, - }); + }) : assert( + color == null || foreground == null, + 'Cannot provide both color and foreground', + ); final Color? color; final String? fontFamily; @@ -84,10 +87,15 @@ class InlineTextStyle extends FlameTextStyle { } TextStyle asTextStyle() { + final fontSize = this.fontSize; + if (fontSize == null) { + throw Exception('fontSize must be set'); + } + return TextStyle( color: color, fontFamily: fontFamily, - fontSize: fontSize! * (fontScale ?? 1.0), + fontSize: fontSize * (fontScale ?? 1.0), fontWeight: fontWeight, fontStyle: fontStyle, letterSpacing: letterSpacing, diff --git a/packages/flame/test/text/text_style_test.dart b/packages/flame/test/text/text_style_test.dart index 850eb50d1a3..7fb2b9ec3e0 100644 --- a/packages/flame/test/text/text_style_test.dart +++ b/packages/flame/test/text/text_style_test.dart @@ -1,5 +1,6 @@ import 'package:flame/text.dart'; import 'package:flutter/rendering.dart'; +import 'package:flutter_test/flutter_test.dart' show throwsAssertionError; import 'package:test/test.dart'; void main() { @@ -140,5 +141,36 @@ void main() { expect(styles[3].fontStyle, FontStyle.italic); expect(styles[3].fontFamily, 'Arial'); }); + + test("inline text style can be converted to Flutter's text style", () { + final withColor = InlineTextStyle( + color: const Color(0xFF00FF00), + fontSize: 12, + ); + final withColorTextStyle = withColor.asTextStyle(); + expect(withColorTextStyle.color, const Color(0xFF00FF00)); + expect(withColorTextStyle.foreground, isNull); + + final paint = Paint()..color = const Color(0xFF00FF00); + final withForeground = InlineTextStyle( + foreground: paint, + fontSize: 12, + ); + final withForegroundTextStyle = withForeground.asTextStyle(); + expect(withForegroundTextStyle.foreground, paint); + expect(withForegroundTextStyle.color, isNull); + + // try setting both options will throw an error + expect( + () { + InlineTextStyle( + color: const Color(0xFF00FF00), + foreground: paint, + fontSize: 12, + ).asTextStyle(); + }, + throwsAssertionError, + ); + }); }); }