[go: nahoru, domu]

Skip to content
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

Merged
merged 13 commits into from
Sep 9, 2024

Conversation

evan361425
Copy link
Contributor
@evan361425 evan361425 commented Jun 16, 2024

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.

@flutter-dashboard
Copy link

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.

@github-actions github-actions bot added the framework flutter/packages/flutter repository. See also f: labels. label Jun 16, 2024
@evan361425 evan361425 changed the title Expose text style foreground to icon 2 tasks done Expose foreground property of TextStyle in Icon widget Jun 16, 2024
@evan361425 evan361425 changed the title 2 tasks done Expose foreground property of TextStyle in Icon widget Expose foreground property of TextStyle in Icon widget Jun 16, 2024
@goderbauer
Copy link
Member

@evan361425 Thanks for the contribution. Could you please add a test as instructed by the bot message above? Thank you.

@evan361425
Copy link
Contributor Author

@goderbauer thanks for your response.
Sure, I will test it by passing the foreground into the icon and checking if the foreground is added.

Is there any hint on how to test if the foreground painter exists?

@evan361425
Copy link
Contributor Author

@goderbauer can you verify the test has match your standards

@goderbauer
Copy link
Member

It looks like this PR is still failing a lot of checks. Can you please take a look at fix those? Thank you.

@evan361425
Copy link
Contributor Author
evan361425 commented Jul 12, 2024

@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
Copy link
Contributor

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.

Copy link
Contributor Author

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?

Copy link
Contributor
@LongCatIsLooong LongCatIsLooong Jul 16, 2024

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).

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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
}

Copy link
Contributor

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 or foreground.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).

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

Copy link
Contributor
@LongCatIsLooong LongCatIsLooong Jul 19, 2024

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?

@goderbauer
Copy link
Member

(triage): @evan361425 It looks like the analyzer is currently not happy with this change. Could you address that?

@evan361425
Copy link
Contributor Author

Sure! I will update the code maybe this weekend.

@evan361425
Copy link
Contributor Author
evan361425 commented Aug 24, 2024

@LongCatIsLooong I've updated it as discussed in the issue. Could you give it a check?

engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 9, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 10, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 10, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 10, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 10, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 10, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 10, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 11, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 11, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 11, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 11, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 11, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 11, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 11, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 12, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 12, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 12, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
autosubmit Merge PR when tree becomes green via auto submit App framework flutter/packages/flutter repository. See also f: labels.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants