Skip to content

feat: Add validation against passing both color and foreground to InlineTextStyle #3427

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
12 changes: 10 additions & 2 deletions packages/flame/lib/src/text/styles/inline_text_style.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -84,10 +87,15 @@ class InlineTextStyle extends FlameTextStyle {
}

TextStyle asTextStyle() {
final fontSize = this.fontSize;
if (fontSize == null) {
throw Exception('fontSize must be set');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't an assertion enough here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I plan on working on standardizing asserts, exceptions and tests thereof on a followup, so I am not making any particular choice in this PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But you did make the choice of casting an unspecified exception, it didn't have that before right? Which is like the worst of both worlds 😅
Or are you doing the refactoring like today? Then I guess it is fine.

}

return TextStyle(
color: color,
fontFamily: fontFamily,
fontSize: fontSize! * (fontScale ?? 1.0),
fontSize: fontSize * (fontScale ?? 1.0),
fontWeight: fontWeight,
fontStyle: fontStyle,
letterSpacing: letterSpacing,
Expand Down
32 changes: 32 additions & 0 deletions packages/flame/test/text/text_style_test.dart
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -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,
);
});
});
}
Loading