Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
201 changes: 201 additions & 0 deletions examples/stac_gallery/assets/json/badge_example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
{
"type": "scaffold",
"appBar": {
"type": "appBar",
"title": {
"type": "text",
"data": "Badge"
}
},
"body": {
"type": "column",
"mainAxisAlignment": "center",
"crossAxisAlignment": "center",
"children": [
{
"type": "text",
"data": "Badge with Label",
"style": {
"fontSize": 18,
"fontWeight": "bold"
}
},
{
"type": "sizedBox",
"height": 16
},
{
"type": "row",
"mainAxisAlignment": "center",
"crossAxisAlignment": "center",
"children": [
{
"type": "badge",
"label": {
"type": "text",
"data": "5"
},
"child": {
"type": "icon",
"icon": "notifications",
"size": 32
}
},
{
"type": "sizedBox",
"width": 24
},
{
"type": "badge",
"label": {
"type": "text",
"data": "NEW"
},
"backgroundColor": "#4CAF50",
"textColor": "#FFFFFF",
"child": {
"type": "icon",
"icon": "mail",
"size": 32
}
}
]
},
{
"type": "sizedBox",
"height": 32
},
{
"type": "text",
"data": "Badge with Count",
"style": {
"fontSize": 18,
"fontWeight": "bold"
}
},
{
"type": "sizedBox",
"height": 16
},
{
"type": "row",
"mainAxisAlignment": "center",
"crossAxisAlignment": "center",
"children": [
{
"type": "badge",
"count": 5,
"child": {
"type": "icon",
"icon": "shopping_cart",
"size": 32
}
},
{
"type": "sizedBox",
"width": 24
},
{
"type": "badge",
"count": 99,
"maxCount": 99,
"child": {
"type": "icon",
"icon": "favorite",
"size": 32
}
},
{
"type": "sizedBox",
"width": 24
},
{
"type": "badge",
"count": 1000,
"maxCount": 99,
"child": {
"type": "icon",
"icon": "notifications",
"size": 32
}
}
]
},
{
"type": "sizedBox",
"height": 32
},
{
"type": "text",
"data": "Small Badge (No Label)",
"style": {
"fontSize": 18,
"fontWeight": "bold"
}
},
{
"type": "sizedBox",
"height": 16
},
{
"type": "row",
"mainAxisAlignment": "center",
"crossAxisAlignment": "center",
"children": [
{
"type": "badge",
"smallSize": 8,
"backgroundColor": "#F44336",
"child": {
"type": "icon",
"icon": "circle",
"size": 32
}
},
{
"type": "sizedBox",
"width": 24
},
{
"type": "badge",
"smallSize": 12,
"backgroundColor": "#4CAF50",
"child": {
"type": "icon",
"icon": "check_circle",
"size": 32
}
}
]
},
{
"type": "sizedBox",
"height": 32
},
{
"type": "text",
"data": "Badge on IconButton",
"style": {
"fontSize": 18,
"fontWeight": "bold"
}
},
{
"type": "sizedBox",
"height": 16
},
{
"type": "badge",
"count": 3,
"child": {
"type": "iconButton",
"icon": {
"type": "icon",
"icon": "notifications",
"size": 24
}
}
}
]
}
}
23 changes: 23 additions & 0 deletions examples/stac_gallery/assets/json/home_screen.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
}
}
},

{
"type": "listTile",
"leading": {
Expand All @@ -44,6 +45,28 @@
}
}
},
{
"type": "listTile",
"leading": {
"type": "icon",
"icon": "badge"
},
"title": {
"type": "text",
"data": "Stac Badge"
},
"subtitle": {
"type": "text",
"data": "Display small status descriptors, counts, or notifications"
},
"onTap": {
"actionType": "navigate",
"widgetJson": {
"type": "exampleScreen",
"assetPath": "assets/json/badge_example.json"
}
}
},
{
"type": "listTile",
"leading": {
Expand Down
1 change: 1 addition & 0 deletions packages/stac/lib/src/framework/stac_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class StacService {
const StacDefaultBottomNavigationControllerParser(),
const StacWrapParser(),
const StacAutoCompleteParser(),
const StacBadgeParser(),
const StacTableParser(),
const StacTableCellParser(),
const StacCarouselViewParser(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import 'package:flutter/material.dart';
import 'package:stac/src/parsers/foundation/foundation.dart';
import 'package:stac/stac.dart';
import 'package:stac_core/stac_core.dart';

class StacBadgeParser extends StacParser<StacBadge> {
const StacBadgeParser();

@override
String get type => WidgetType.badge.name;

@override
StacBadge getModel(Map<String, dynamic> json) => StacBadge.fromJson(json);

@override
Widget parse(BuildContext context, StacBadge model) {
// Handle count property (Badge.count equivalent)
Widget? label;
if (model.count != null) {
// Validate count and maxCount (matching Flutter's assertions)
assert(model.count! >= 0, 'count must be non-negative');
final maxCount = model.maxCount ?? 999;
assert(maxCount > 0, 'maxCount must be positive');

// Create label from count (matching Flutter's Badge.count logic)
final labelText = model.count! > maxCount
? '$maxCount+'
: '${model.count}';
label = Text(labelText);
} else {
// Use explicit label if count is not provided
label = model.label?.parse(context);
}

return Badge(
backgroundColor: model.backgroundColor?.toColor(context),
textColor: model.textColor?.toColor(context),
smallSize: model.smallSize,
largeSize: model.largeSize,
textStyle: model.textStyle?.parse(context),
padding: model.padding?.parse,
alignment: model.alignment?.parse,
offset: model.offset?.parse,
label: label,
isLabelVisible: model.isLabelVisible ?? true,
child: model.child?.parse(context),
);
}
}
1 change: 1 addition & 0 deletions packages/stac/lib/src/parsers/widgets/widgets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export 'package:stac/src/parsers/widgets/stac_align/stac_align_parser.dart';
export 'package:stac/src/parsers/widgets/stac_aspect_ratio/stac_aspect_ratio_parser.dart';
export 'package:stac/src/parsers/widgets/stac_auto_complete/stac_auto_complete_parser.dart';
export 'package:stac/src/parsers/widgets/stac_backdrop_filter/stac_backdrop_filter_parser.dart';
export 'package:stac/src/parsers/widgets/stac_badge/stac_badge_parser.dart';
export 'package:stac/src/parsers/widgets/stac_bottom_navigation_bar/stac_bottom_navigation_bar_parser.dart';
export 'package:stac/src/parsers/widgets/stac_bottom_navigation_view/stac_bottom_navigation_view_parser.dart';
export 'package:stac/src/parsers/widgets/stac_card/stac_card_parser.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ enum WidgetType {
/// Backdrop filter widget
backdropFilter,

/// Badge widget
badge,

/// Bottom navigation bar widget
bottomNavigationBar,

Expand Down
Loading