Skip to content

Conversation

@kumar-gautam24
Copy link

@kumar-gautam24 kumar-gautam24 commented Nov 29, 2025

Description

Add support for Badge widget in Stac.

Changes Made

Core Implementation:

  • Added badge to WidgetType enum
  • Created StacBadge model class with all Flutter Badge properties (including count/maxCount for Badge.count() support)
  • Created StacBadgeParser to convert JSON → Flutter Badge widget
  • Registered parser in StacService

Files:

  • 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

  • Full support for all Flutter Badge properties
  • Badge.count() equivalent functionality via count and maxCount properties
  • Proper validation matching Flutter's Badge behavior
  • Comprehensive examples in stac_gallery

Related Issues

Closes #384

Type of Change

  • New feature (non-breaking change which adds functionality)
  • Bug fix (non-breaking change which fixes an issue)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Code refactor
  • Build configuration change
  • Documentation
  • Chore

Summary by CodeRabbit

  • New Features
    • Added support for Badge widgets to display status descriptors, counts, or notifications with customizable colors, sizes, and styling options.
    • Introduced a new example gallery showcasing various badge configurations including labeled badges, count badges, and small badges on icons.

✏️ Tip: You can customize this high-level summary in your review settings.

- 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.
@CLAassistant
Copy link

CLAassistant commented Nov 29, 2025

CLA assistant check
All committers have signed the CLA.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 29, 2025

Walkthrough

This 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

Cohort / File(s) Summary
Example Assets
examples/stac_gallery/assets/json/badge_example.json, examples/stac_gallery/assets/json/home_screen.json
New badge example JSON asset demonstrating badge widget variations (with label, with count, small badges, badge on IconButton). Gallery home screen updated with new navigation entry linking to badge example.
Core Badge Model & Type
packages/stac_core/lib/widgets/badge/stac_badge.dart, packages/stac_core/lib/widgets/badge/stac_badge.g.dart, packages/stac_core/lib/foundation/specifications/widget_type.dart, packages/stac_core/lib/widgets/widgets.dart
New StacBadge model with properties for colors, sizes, styling, alignment, count, and label. Generated JSON serialization code added. WidgetType enum extended with badge member. Widget export added.
Badge Parser & Registration
packages/stac/lib/src/parsers/widgets/stac_badge/stac_badge_parser.dart, packages/stac/lib/src/parsers/widgets/widgets.dart, packages/stac/lib/src/framework/stac_service.dart
New StacBadgeParser implementing badge JSON-to-Flutter widget parsing with count validation and label fallback logic. Parser registered in StacService. Parser export added.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20–30 minutes

  • StacBadgeParser logic – Verify count validation, maxCount clamping behavior, and label fallback logic align with requirements
  • Model properties – Ensure all StacBadge properties (backgroundColor, textColor, sizes, alignment, offset, etc.) correctly map to Flutter Badge API
  • JSON serialization – Confirm generated fromJson/toJson methods handle nested objects and default values correctly (maxCount: 999, isLabelVisible: true)
  • Type enum & registration – Verify WidgetType.badge is positioned correctly and StacBadgeParser is registered in the correct location within the parsers list

Possibly related PRs

Suggested reviewers

  • Potatomonsta
  • divyanshub024

Poem

🐰 A badge hops into view,

Count it, label it, make it new!

Small or large, on buttons bright,

Your UI indicators now take flight.

Pre-merge checks and finishing touches

✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title accurately summarizes the main change: introducing a Badge widget and parser with example JSON.
Linked Issues check ✅ Passed All coding requirements from issue #384 are met: Badge model created [packages/stac_core/lib/widgets/badge/stac_badge.dart], parser implemented [stac_badge_parser.dart], example added [badge_example.json], and integration completed.
Out of Scope Changes check ✅ Passed All changes are directly scoped to implementing Badge widget support. No unrelated modifications or scope creep detected across files.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

  • Provide your own instructions using the high_level_summary_instructions setting.
  • Format the summary however you like (bullet lists, tables, multi-section layouts, contributor stats, etc.).
  • Use high_level_summary_in_walkthrough to move the summary from the description to the walkthrough section.

Example instruction:

"Divide the high-level summary into five sections:

  1. 📝 Description — Summarize the main change in 50–60 words, explaining what was done.
  2. 📓 References — List relevant issues, discussions, documentation, or related PRs.
  3. 📦 Dependencies & Requirements — Mention any new/updated dependencies, environment variable changes, or configuration updates.
  4. 📊 Contributor Summary — Include a Markdown table showing contributions:
    | Contributor | Lines Added | Lines Removed | Files Changed |
  5. ✔️ Additional Notes — Add any extra reviewer context.
    Keep each section concise (under 200 words) and use bullet or numbered lists for clarity."

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@kumar-gautam24
Copy link
Author

@divyanshub024 hi can you please review this

Copy link
Contributor

@coderabbitai coderabbitai bot left a 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 centralize count >= 0 and maxCount > 0 constraints.

The docs state that count must be non‑negative and maxCount must be positive, but the model itself doesn’t enforce this. If validation currently lives only in StacBadgeParser, 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 StacBadge directly.


81-95: Align JSON deserialization defaults for maxCount/isLabelVisible with documented values.

You document maxCount as defaulting to 999 and isLabelVisible as effectively default‑true, and you set those as constructor defaults. Depending on your json_serializable configuration, objects built via fromJson may still see these as null when 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

📥 Commits

Reviewing files that changed from the base of the PR and between 11de03f and 9a3da7b.

📒 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 for StacBadge are clear and aligned with Flutter Badge.

The top-level documentation plus Dart/JSON snippets mirror Flutter’s Badge semantics and should make both direct Dart usage and server‑driven JSON configuration straightforward. Nice coverage of label vs count/maxCount behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: Add support for badge widget

2 participants