-
-
Notifications
You must be signed in to change notification settings - Fork 82
feat: Introduce stacBadge widget and parser with example JSON #388
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
base: dev
Are you sure you want to change the base?
Conversation
- Added StacBadge widget to represent notifications and counts. - Implemented StacBadgeParser for parsing badge JSON configurations. - Created badge_example.json to demonstrate badge usage in the gallery. - Updated home_screen.json to include a navigation tile for the badge example. - Registered StacBadgeParser in the StacService for widget parsing.
WalkthroughThis PR adds support for the Badge widget in Stac, enabling Server Driven UI declaration of badges in Flutter apps. Changes include a new StacBadge model in stac_core, a StacBadgeParser in stac, registration in StacService, addition to the WidgetType enum, and example gallery assets showcasing badge usage patterns. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20–30 minutes
Possibly related PRs
Suggested reviewers
Poem
Pre-merge checks and finishing touches✅ Passed checks (5 passed)
✨ Finishing touches🧪 Generate unit tests (beta)
Tip 📝 Customizable high-level summaries are now available in beta!You can now customize how CodeRabbit generates the high-level summary in your pull requests — including its content, structure, tone, and formatting.
Example instruction:
Note: This feature is currently in beta for Pro-tier users, and pricing will be announced later. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
@divyanshub024 hi can you please review this |
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.
Actionable comments posted: 0
🧹 Nitpick comments (3)
packages/stac/lib/src/parsers/widgets/stac_badge/stac_badge_parser.dart (1)
21-23: Consider validation instead of assertions for production safety.The assertions validate count and maxCount, but assertions are disabled in production/release builds. While this matches Flutter's Badge widget pattern, invalid values could pass through silently in production.
Consider adding explicit validation:
- assert(model.count! >= 0, 'count must be non-negative'); + if (model.count! < 0) { + throw ArgumentError('count must be non-negative'); + } final maxCount = model.maxCount ?? 999; - assert(maxCount > 0, 'maxCount must be positive'); + if (maxCount <= 0) { + throw ArgumentError('maxCount must be positive'); + }packages/stac_core/lib/widgets/badge/stac_badge.dart (2)
127-147: Enforce or centralizecount >= 0andmaxCount > 0constraints.The docs state that
countmust be non‑negative andmaxCountmust be positive, but the model itself doesn’t enforce this. If validation currently lives only inStacBadgeParser, consider either:
- Adding simple assertions in the constructor, or
- Clearly documenting in this file that validation is performed at the parser layer.
This keeps behavior predictable if other consumers instantiate
StacBadgedirectly.
81-95: Align JSON deserialization defaults formaxCount/isLabelVisiblewith documented values.You document
maxCountas defaulting to999andisLabelVisibleas effectively default‑true, and you set those as constructor defaults. Depending on yourjson_serializableconfiguration, objects built viafromJsonmay still see these asnullwhen the keys are absent, pushing defaulting logic into the parser.To keep defaults declarative at the model level and reduce duplication in parsers, consider adding explicit
@JsonKey(defaultValue: ...)annotations, e.g.:@@ - /// Maximum count value before showing '[maxCount]+' format. - /// - /// Only used when [count] is provided. Defaults to 999. - /// Must be positive (> 0). - final int? maxCount; + /// Maximum count value before showing '[maxCount]+' format. + /// + /// Only used when [count] is provided. Defaults to 999. + /// Must be positive (> 0). + @JsonKey(defaultValue: 999) + final int? maxCount; @@ - /// If false, the badge's [label] is not included. - final bool? isLabelVisible; + /// If false, the badge's [label] is not included. + @JsonKey(defaultValue: true) + final bool? isLabelVisible;This keeps model semantics consistent whether instances are created directly or via JSON.
Also applies to: 140-147
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (9)
examples/stac_gallery/assets/json/badge_example.json(1 hunks)examples/stac_gallery/assets/json/home_screen.json(2 hunks)packages/stac/lib/src/framework/stac_service.dart(1 hunks)packages/stac/lib/src/parsers/widgets/stac_badge/stac_badge_parser.dart(1 hunks)packages/stac/lib/src/parsers/widgets/widgets.dart(1 hunks)packages/stac_core/lib/foundation/specifications/widget_type.dart(1 hunks)packages/stac_core/lib/widgets/badge/stac_badge.dart(1 hunks)packages/stac_core/lib/widgets/badge/stac_badge.g.dart(1 hunks)packages/stac_core/lib/widgets/widgets.dart(1 hunks)
🔇 Additional comments (10)
packages/stac_core/lib/widgets/badge/stac_badge.g.dart (1)
1-54: LGTM! Generated serialization code.This is auto-generated JSON serialization code for StacBadge. The default values (maxCount: 999, isLabelVisible: true) align with Flutter's Badge widget behavior.
packages/stac/lib/src/framework/stac_service.dart (1)
101-101: LGTM! Badge parser registered correctly.The StacBadgeParser is properly registered in the parsers list, following the established pattern.
packages/stac_core/lib/widgets/widgets.dart (1)
9-9: LGTM! Badge widget export added correctly.The export is properly placed in alphabetical order and follows the established pattern.
packages/stac_core/lib/foundation/specifications/widget_type.dart (1)
24-25: LGTM! Badge enum member added correctly.The badge widget type is properly documented and alphabetically ordered within the WidgetType enum.
examples/stac_gallery/assets/json/home_screen.json (1)
48-69: LGTM! Badge navigation tile added correctly.The list tile for Stac Badge follows the established pattern and correctly navigates to the badge example JSON asset.
packages/stac/lib/src/parsers/widgets/widgets.dart (1)
6-6: LGTM! Badge parser export added correctly.The export is properly placed in alphabetical order.
examples/stac_gallery/assets/json/badge_example.json (1)
1-201: LGTM! Comprehensive badge examples.This example JSON demonstrates various badge configurations effectively:
- Label-based badges (text and numeric)
- Count-based badges with maxCount behavior
- Small badges for notification dots
- Badge wrapping interactive widgets
The examples align well with Flutter's Badge widget capabilities.
packages/stac/lib/src/parsers/widgets/stac_badge/stac_badge_parser.dart (2)
26-29: LGTM! Count-to-label conversion logic is correct.The implementation properly handles maxCount clamping and label generation, matching Flutter's Badge.count behavior:
- Shows actual count when ≤ maxCount
- Shows "maxCount+" when count exceeds maxCount
35-47: LGTM! Badge widget construction is correct.All properties are properly mapped from the StacBadge model to Flutter's Badge widget:
- Color conversions handled correctly
- Size properties passed through
- Style and layout properties parsed appropriately
- Label and child resolved from model
packages/stac_core/lib/widgets/badge/stac_badge.dart (1)
7-70: Docs and examples forStacBadgeare clear and aligned with FlutterBadge.The top-level documentation plus Dart/JSON snippets mirror Flutter’s
Badgesemantics and should make both direct Dart usage and server‑driven JSON configuration straightforward. Nice coverage oflabelvscount/maxCountbehavior.
Description
Add support for Badge widget in Stac.
Changes Made
Core Implementation:
badgetoWidgetTypeenumStacBadgemodel class with all Flutter Badge properties (includingcount/maxCountfor Badge.count() support)StacBadgeParserto convert JSON → Flutter Badge widgetStacServiceFiles:
packages/stac_core/lib/widgets/badge/stac_badge.dart(new)packages/stac/lib/src/parsers/widgets/stac_badge/stac_badge_parser.dart(new)packages/stac_core/lib/foundation/specifications/widget_type.dart(modified)packages/stac/lib/src/framework/stac_service.dart(modified)examples/stac_gallery/assets/json/badge_example.json(new)examples/stac_gallery/assets/json/home_screen.json(modified)Features
countandmaxCountpropertiesRelated Issues
Closes #384
Type of Change
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.