-
Notifications
You must be signed in to change notification settings - Fork 27.2k
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
Expose foreground property of TextStyle in Icon widget #150315
Conversation
It looks like this pull request may not have tests. Please make sure to add tests before merging. If you need an exemption to this rule, contact "@test-exemption-reviewer" in the #hackers channel in Chat (don't just cc them here, they won't see it! Use Discord!). If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix? Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing. |
@evan361425 Thanks for the contribution. Could you please add a test as instructed by the bot message above? Thank you. |
@goderbauer thanks for your response. Is there any hint on how to test if the foreground painter exists? |
@goderbauer can you verify the test has match your standards |
It looks like this PR is still failing a lot of checks. Can you please take a look at fix those? Thank you. |
@goderbauer done! thanks for your patience for reviewing. |
/// | ||
/// This is useful for applying effects like blur or color filters to the icon. | ||
/// | ||
/// Paint's color property is ignored, as the color is determined by the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels a bit strange since it's very different from what TextStyle
does (either color or paint, not both), and that can be surprising. IMO this should probably be a separate constructor, or follow the TextStyle
convention and add an assert, so the API stays consistent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’m confused about what ‘follow the TextStyle convention’ means. Could you please provide more explanation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah sorry so https://main-api.flutter.dev/flutter/painting/TextStyle/color.html says:
If foreground is specified, this value must be null. The color property is shorthand for Paint()..color = color.
and in the constructor implementation it asserts: assert(color == null || foreground == null, _kColorForegroundWarning)
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok but the color
property can still be provided, but it will override the color attribute set in the foreground
. For example, when color
is set to Colors.red
and foreground
is set to Paint()..color = Colors.blue
, the final color will be red.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason color
has a higher priority than foreground.color
, and not the other way around? I think putting multiple parameters that refer to the same thing in the same API usually makes the API less self-explanatory.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds reasonable. I will give foreground.color
higher priority with the following code:
if (foreground!.color == null) {
foreground!.color = iconColor
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry my point was that color
and foreground.color
should not be specified at the same time, it should be considered an incorrect use of the API if they are both set. The reasons are:
-
Giving
color
orforeground.color
a higher priority than the other is a bit arbitrary IMO, -
I think most of the time when developers write:
Icon(color: ..., foreground: Paint()..color = ...)
, it's by mistake. It saves their time to debug this kind of error if we assert that not both of them are set, or if we prevent them from doing this at compile time (by introducing another constructor).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh finally got it! Thanks for your patience. Sounds like writing another constructor like:
// this constructor will not provide color, avoid developers making mistake
Icon.withForeground({
...,
})
is overkill for me, so I will try using assertion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@LongCatIsLooong, I encountered an issue:
If a developer only passes in foreground
, how should the foreground
color be determined? It is unclear whether foreground
has a set color (since the default color of Paint
is opaque black, not null). However, completely relying on the color provided by foreground
would mean that the developer has to inject the value from IconTheme
every time they pass in foreground
.
I believe we should not use the color from foreground
and instead directly use the final computed iconColor
in Icon
to override the Paint
's color.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how should the foreground color be determined?
I would think it's just either paint
or color
(see TextStyle
's constructor https://main-api.flutter.dev/flutter/painting/TextStyle/TextStyle.html).
However, completely relying on the color provided by foreground would mean that the developer has to inject the value from IconTheme every time they pass in foreground
Do you mean that we need an IconTheme.foreground
?
(triage): @evan361425 It looks like the analyzer is currently not happy with this change. Could you address that? |
Sure! I will update the code maybe this weekend. |
@LongCatIsLooong I've updated it as discussed in the issue. Could you give it a check? |
Expose foreground property to icon. See issues:
Pre-launch Checklist
///
).If you need help, consider asking for advice on the #hackers-new channel on Discord.