-
Notifications
You must be signed in to change notification settings - Fork 29.5k
feat: Add maxCount parameter to Badge.count constructor.
#171054
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
Conversation
- This allows for dynamic maximum limits on badge counts, removing the need for hardcoded values.
- Adds comprehensive API documentation for the newly introduced `maxCount` parameter.
|
It looks like this pull request may not have tests. Please make sure to add tests or get an explicit test exemption before merging. 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.If you believe this PR qualifies for a test exemption, contact "@test-exemption-reviewer" in the #hackers channel in Discord (don't just cc them here, they won't see it!). The test exemption team is a small volunteer group, so all reviewers should feel empowered to ask for tests, without delegating that responsibility entirely to the test exemption group. |
QuncCccccc
left a comment
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.
Thank you for your contribution! Could you help add some unit tests to verify the code change?
Adds an example demonstrating the `maxCount` property of the `Badge` widget. The example showcases how the badge displays a maximum count with an overflow indicator when the actual count exceeds the specified maximum.
Hey, thanks for the feedback! I’ve added a
You’ll find them in |
Thanks a lot for adding them! I actually think it's not necessary to add an example just for the new parameter. What I mean in my last comment is to add unit tests in |
|
|
||
| /// Convenience constructor for creating a badge with a numeric | ||
| /// label with 1-3 digits based on [count]. | ||
| /// label with 1-[maxCount] digits based on [count]. |
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 change seems not accurate. maxCount means the max number showing up on badge instead of digits, right?
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.
Thank you for the feedback! You’re right — maxCount limits the maximum number shown on the badge, not the number of digits. I’ll update the doc comment to clarify this. Please let me know if you have suggestions for the wording!
The `Badge.count` constructor now accurately displays the badge label based on the `count` and `maxCount` properties. - When `count` is less than or equal to `maxCount`, the badge displays the `count` value. - When `count` exceeds `maxCount`, the badge displays '[maxCount]+'. This commit also includes a new test case to verify the correct behavior of `Badge.count` with `maxCount`. The example demonstrating the `maxCount` property of the `Badge` widget has been removed.
| this.isLabelVisible = true, | ||
| this.child, | ||
| }) : label = Text(count > 999 ? '999+' : '$count'); | ||
| }) : label = Text(count > maxCount ? '$maxCount+' : '$count'); |
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.
Move Label Creation to a Getter or Method
Right now, label assignment is inline in the initializer list. Moving this to a small private method improves readability:
class Badge extends StatelessWidget {
final int maxCount;
late final Widget label; // ✅ Make it late so we can assign it in the constructor
}) : maxCount = displayMaxCount {
label = _buildLabel(count, maxCount); // ✅ assign here using the method
}
Text _buildLabel(int count, int maxCount) {
return Text(count > maxCount ? '$maxCount+' : '$count');
}
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 am not sure this is necessary, but thanks for the feedback!
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.
Just to improve code readability 😅
| ); | ||
| } | ||
|
|
||
| await tester.pumpWidget(buildFrame(5, 99)); |
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.
Add Edge Case Test for Negative Count
Even though it’s unlikely to happen in production, explicitly asserting that negative count values throw an AssertionError or are clamped would increase robustness.
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.
+1, that is a good case to cover!
QuncCccccc
left a comment
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.
LGTM! Thank you for your contribution!
| this.isLabelVisible = true, | ||
| this.child, | ||
| }) : label = Text(count > 999 ? '999+' : '$count'); | ||
| }) : label = Text(count > maxCount ? '$maxCount+' : '$count'); |
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.
Adding the assertion here that we're non-negative is probably the right spot.
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.
Adding the assertion here that we're non-negative is probably the right spot.
Awesome, thanks for the confirmation! I've added the assert(count >= 0, ...) and assert(maxCount > 0, ...) checks in the constructor, and expanded our unit tests to include negative, zero, and invalid maxCount cases. The updated code and tests are pushed—please let me know if anything else needs tweaking! 😊
This commit adds assertions to `Badge.count` to ensure that the `count` is non-negative and `maxCount` is positive. It also ensures that "0" is displayed when the count is zero.
victorsanni
left a comment
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.
LGTM. Thanks for the PR.
flutter/flutter@9c626d9...440713c 2025-07-18 stuartmorgan@google.com Add OS version support section to plugin docs (flutter/flutter#172316) 2025-07-18 engine-flutter-autoroll@skia.org Roll Dart SDK from 7fcc5f48c335 to fe2232bd5b9f (2 revisions) (flutter/flutter#172339) 2025-07-18 robert.ancell@canonical.com Refactor handling of frame waiting. (flutter/flutter#172277) 2025-07-18 137456488+flutter-pub-roller-bot@users.noreply.github.com Roll pub packages (flutter/flutter#172332) 2025-07-17 matanlurey@users.noreply.github.com Refactor and forbid `base/exit.dart` outside of `lib/runner.dart` (flutter/flutter#171923) 2025-07-17 matanlurey@users.noreply.github.com Use UTC timezone for `stamp_command_test` to avoid local TZ flakes (flutter/flutter#172319) 2025-07-17 1961493+harryterkelsen@users.noreply.github.com [web] Add tests for unified platform view embedding behavior (flutter/flutter#172313) 2025-07-17 jmccandless@google.com Platform views shouldn't receive pointer events when not laid out (flutter/flutter#172043) 2025-07-17 ahmedsameha1@gmail.com Fix Size.isEmpty description (flutter/flutter#172021) 2025-07-17 iamtoricool@gmail.com feat: Add `maxCount` parameter to `Badge.count` constructor. (flutter/flutter#171054) 2025-07-17 31859944+LongCatIsLooong@users.noreply.github.com add `debugPaintTextLayoutBoxes` for debugging text layout (flutter/flutter#168876) 2025-07-17 engine-flutter-autoroll@skia.org Roll Packages from 4a231ae to cb8fef6 (31 revisions) (flutter/flutter#172303) 2025-07-17 jmccandless@google.com Fix mutating Paint bug (flutter/flutter#171180) 2025-07-17 jmccandless@google.com No SystemContextMenu when readOnly is true (flutter/flutter#171242) 2025-07-17 muhammad.mohiuddin@live.com Add a getter to get the recorder used by painting context (flutter/flutter#170223) 2025-07-17 jason-simmons@users.noreply.github.com [Impeller] Add missing SetPipeline call for the vertices uber pipeline in AtlasContents (flutter/flutter#172273) 2025-07-17 engine-flutter-autoroll@skia.org Roll Dart SDK from 486f9c0663bc to 7fcc5f48c335 (1 revision) (flutter/flutter#172279) 2025-07-17 engine-flutter-autoroll@skia.org Roll Skia from 3673a1f26a63 to 9587301e33bc (3 revisions) (flutter/flutter#172281) 2025-07-17 jason-simmons@users.noreply.github.com Fix a race in FlutterEngineTest.CanLogToStdout (flutter/flutter#172025) If this roll has caused a breakage, revert this CL and stop the roller using the controls here: https://autoroll.skia.org/r/flutter-packages Please CC bmparr@google.com,stuartmorgan@google.com on the revert to ensure that a human is aware of the problem. To file a bug in Packages: https://github.com/flutter/flutter/issues/new/choose To report a problem with the AutoRoller itself, please file a bug: https://issues.skia.org/issues/new?component=1389291&template=1850622 Documentation for the AutoRoller is here: https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
…#171054) feat: Add `maxCount` parameter to `Badge.count` constructor This enhancement provides the ability to specify a **dynamic maximum limit** for the badge count, offering greater flexibility and eliminating the reliance on hardcoded values within the widget. This allows developers to, for example, display "99+" instead of "100" or "9+" instead of "10", improving UI consistency and preventing overly large numbers on badges. --- ## Description of Changes This PR introduces a new optional parameter, `maxCount`, to the `Badge.count` constructor. This provides a flexible way to define a numerical upper bound for the badge display. Key aspects of this change: * **Dynamic Limits:** Developers can now set custom maximum values (e.g., 9, 99, 999) that, when exceeded, will display the `maxCount` value followed by a `+` symbol (e.g., "9+", "99+"). * **Improved UI/UX:** Prevents large, potentially overflowing numbers from appearing on badges, maintaining visual integrity and a cleaner user interface. * **Reduced Hardcoding:** Centralizes the logic for maximum badge values within the `Badge` widget itself, reducing the need for manual checks and conditional rendering outside the widget. * **Comprehensive Documentation:** Includes updated API documentation for the `maxCount` parameter, clarifying its usage, behavior, and the visual outcome when the maximum is reached. This change aligns with Flutter's principles of providing highly customizable and intuitive widgets. --- ## Pre-launch Checklist - [X] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [X] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [X] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [X] I signed the [CLA]. - [X] I listed at least one issue that this PR fixes in the description above. - [X] I updated/added relevant documentation (doc comments with `///`). - [X] I added new tests to check the change I am making, or this PR is [test-exempt]. - [X] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. (Assuming no breaking changes) - [X] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
…r#9646) flutter/flutter@9c626d9...440713c 2025-07-18 stuartmorgan@google.com Add OS version support section to plugin docs (flutter/flutter#172316) 2025-07-18 engine-flutter-autoroll@skia.org Roll Dart SDK from 7fcc5f48c335 to fe2232bd5b9f (2 revisions) (flutter/flutter#172339) 2025-07-18 robert.ancell@canonical.com Refactor handling of frame waiting. (flutter/flutter#172277) 2025-07-18 137456488+flutter-pub-roller-bot@users.noreply.github.com Roll pub packages (flutter/flutter#172332) 2025-07-17 matanlurey@users.noreply.github.com Refactor and forbid `base/exit.dart` outside of `lib/runner.dart` (flutter/flutter#171923) 2025-07-17 matanlurey@users.noreply.github.com Use UTC timezone for `stamp_command_test` to avoid local TZ flakes (flutter/flutter#172319) 2025-07-17 1961493+harryterkelsen@users.noreply.github.com [web] Add tests for unified platform view embedding behavior (flutter/flutter#172313) 2025-07-17 jmccandless@google.com Platform views shouldn't receive pointer events when not laid out (flutter/flutter#172043) 2025-07-17 ahmedsameha1@gmail.com Fix Size.isEmpty description (flutter/flutter#172021) 2025-07-17 iamtoricool@gmail.com feat: Add `maxCount` parameter to `Badge.count` constructor. (flutter/flutter#171054) 2025-07-17 31859944+LongCatIsLooong@users.noreply.github.com add `debugPaintTextLayoutBoxes` for debugging text layout (flutter/flutter#168876) 2025-07-17 engine-flutter-autoroll@skia.org Roll Packages from 4a231ae to cb8fef6 (31 revisions) (flutter/flutter#172303) 2025-07-17 jmccandless@google.com Fix mutating Paint bug (flutter/flutter#171180) 2025-07-17 jmccandless@google.com No SystemContextMenu when readOnly is true (flutter/flutter#171242) 2025-07-17 muhammad.mohiuddin@live.com Add a getter to get the recorder used by painting context (flutter/flutter#170223) 2025-07-17 jason-simmons@users.noreply.github.com [Impeller] Add missing SetPipeline call for the vertices uber pipeline in AtlasContents (flutter/flutter#172273) 2025-07-17 engine-flutter-autoroll@skia.org Roll Dart SDK from 486f9c0663bc to 7fcc5f48c335 (1 revision) (flutter/flutter#172279) 2025-07-17 engine-flutter-autoroll@skia.org Roll Skia from 3673a1f26a63 to 9587301e33bc (3 revisions) (flutter/flutter#172281) 2025-07-17 jason-simmons@users.noreply.github.com Fix a race in FlutterEngineTest.CanLogToStdout (flutter/flutter#172025) If this roll has caused a breakage, revert this CL and stop the roller using the controls here: https://autoroll.skia.org/r/flutter-packages Please CC bmparr@google.com,stuartmorgan@google.com on the revert to ensure that a human is aware of the problem. To file a bug in Packages: https://github.com/flutter/flutter/issues/new/choose To report a problem with the AutoRoller itself, please file a bug: https://issues.skia.org/issues/new?component=1389291&template=1850622 Documentation for the AutoRoller is here: https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
…#171054) feat: Add `maxCount` parameter to `Badge.count` constructor This enhancement provides the ability to specify a **dynamic maximum limit** for the badge count, offering greater flexibility and eliminating the reliance on hardcoded values within the widget. This allows developers to, for example, display "99+" instead of "100" or "9+" instead of "10", improving UI consistency and preventing overly large numbers on badges. --- ## Description of Changes This PR introduces a new optional parameter, `maxCount`, to the `Badge.count` constructor. This provides a flexible way to define a numerical upper bound for the badge display. Key aspects of this change: * **Dynamic Limits:** Developers can now set custom maximum values (e.g., 9, 99, 999) that, when exceeded, will display the `maxCount` value followed by a `+` symbol (e.g., "9+", "99+"). * **Improved UI/UX:** Prevents large, potentially overflowing numbers from appearing on badges, maintaining visual integrity and a cleaner user interface. * **Reduced Hardcoding:** Centralizes the logic for maximum badge values within the `Badge` widget itself, reducing the need for manual checks and conditional rendering outside the widget. * **Comprehensive Documentation:** Includes updated API documentation for the `maxCount` parameter, clarifying its usage, behavior, and the visual outcome when the maximum is reached. This change aligns with Flutter's principles of providing highly customizable and intuitive widgets. --- ## Pre-launch Checklist - [X] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [X] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [X] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [X] I signed the [CLA]. - [X] I listed at least one issue that this PR fixes in the description above. - [X] I updated/added relevant documentation (doc comments with `///`). - [X] I added new tests to check the change I am making, or this PR is [test-exempt]. - [X] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. (Assuming no breaking changes) - [X] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
…#171054) feat: Add `maxCount` parameter to `Badge.count` constructor This enhancement provides the ability to specify a **dynamic maximum limit** for the badge count, offering greater flexibility and eliminating the reliance on hardcoded values within the widget. This allows developers to, for example, display "99+" instead of "100" or "9+" instead of "10", improving UI consistency and preventing overly large numbers on badges. --- ## Description of Changes This PR introduces a new optional parameter, `maxCount`, to the `Badge.count` constructor. This provides a flexible way to define a numerical upper bound for the badge display. Key aspects of this change: * **Dynamic Limits:** Developers can now set custom maximum values (e.g., 9, 99, 999) that, when exceeded, will display the `maxCount` value followed by a `+` symbol (e.g., "9+", "99+"). * **Improved UI/UX:** Prevents large, potentially overflowing numbers from appearing on badges, maintaining visual integrity and a cleaner user interface. * **Reduced Hardcoding:** Centralizes the logic for maximum badge values within the `Badge` widget itself, reducing the need for manual checks and conditional rendering outside the widget. * **Comprehensive Documentation:** Includes updated API documentation for the `maxCount` parameter, clarifying its usage, behavior, and the visual outcome when the maximum is reached. This change aligns with Flutter's principles of providing highly customizable and intuitive widgets. --- ## Pre-launch Checklist - [X] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [X] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [X] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [X] I signed the [CLA]. - [X] I listed at least one issue that this PR fixes in the description above. - [X] I updated/added relevant documentation (doc comments with `///`). - [X] I added new tests to check the change I am making, or this PR is [test-exempt]. - [X] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. (Assuming no breaking changes) - [X] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
feat: Add
maxCountparameter toBadge.countconstructorThis enhancement provides the ability to specify a dynamic maximum limit for the badge count, offering greater flexibility and eliminating the reliance on hardcoded values within the widget. This allows developers to, for example, display "99+" instead of "100" or "9+" instead of "10", improving UI consistency and preventing overly large numbers on badges.
Description of Changes
This PR introduces a new optional parameter,
maxCount, to theBadge.countconstructor. This provides a flexible way to define a numerical upper bound for the badge display.Key aspects of this change:
maxCountvalue followed by a+symbol (e.g., "9+", "99+").Badgewidget itself, reducing the need for manual checks and conditional rendering outside the widget.maxCountparameter, clarifying its usage, behavior, and the visual outcome when the maximum is reached.This change aligns with Flutter's principles of providing highly customizable and intuitive widgets.
Pre-launch Checklist
///).If you need help, consider asking for advice on the #hackers-new channel on Discord.