Skip to content

Commit

Permalink
feat: override badge color
Browse files Browse the repository at this point in the history
  • Loading branch information
vanlooverenkoen committed Mar 25, 2024
1 parent eccb644 commit 45782ff
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 0.4.0

## Feat

- Override color of ImpaktfullBadge

# 0.3.0

## Feat
Expand Down
31 changes: 12 additions & 19 deletions lib/src/components/badge/impaktfull_badge.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ enum ImpaktfullBadgeLocation {
class ImpaktfullBadge extends StatelessWidget {
final Widget child;
final bool showBadge;
final Color? color;
final String? badgeText;
final ImpaktfullBadgeLocation location;

const ImpaktfullBadge({
required this.child,
this.color,
this.showBadge = false,
this.badgeText,
this.location = ImpaktfullBadgeLocation.topRight,
Expand All @@ -29,6 +31,7 @@ class ImpaktfullBadge extends StatelessWidget {
Widget build(BuildContext context) {
final showBadge = this.showBadge || badgeText != null;
return ImpaktfullThemeLocalizer(builder: (context, theme) {
final color = this.color ?? theme.colors.accent1;
final textStyle = theme.textStyles.onPrimary.smallBody;
final textSize = _textWidth(badgeText ?? '', textStyle);
final textWidth = textSize.width + 12;
Expand All @@ -54,7 +57,7 @@ class ImpaktfullBadge extends StatelessWidget {
minHeight: 16,
),
decoration: BoxDecoration(
color: theme.colors.accent1,
color: color,
borderRadius: BorderRadius.circular(
theme.dimens.generalBorderRadius,
),
Expand All @@ -72,9 +75,10 @@ class ImpaktfullBadge extends StatelessWidget {
width: 4,
height: 4,
decoration: BoxDecoration(
color: theme.colors.accent1,
color: color,
borderRadius: BorderRadius.circular(
theme.dimens.generalBorderRadius),
theme.dimens.generalBorderRadius,
),
),
);
}
Expand All @@ -99,46 +103,35 @@ class ImpaktfullBadge extends StatelessWidget {
}

Size _textWidth(String text, TextStyle style) {
final TextPainter textPainter = TextPainter(
text: TextSpan(text: text, style: style),
maxLines: 1,
textDirection: TextDirection.ltr)
final TextPainter textPainter = TextPainter(text: TextSpan(text: text, style: style), maxLines: 1, textDirection: TextDirection.ltr)
..layout(minWidth: 0, maxWidth: double.infinity);
return textPainter.size;
}

double? _getTop(double textWidth, double textHeight) {
final alignment = location.alignment;
if (alignment == Alignment.bottomCenter ||
alignment == Alignment.bottomLeft ||
alignment == Alignment.bottomRight) return null;
if (alignment == Alignment.bottomCenter || alignment == Alignment.bottomLeft || alignment == Alignment.bottomRight) return null;
if (badgeText == null) return -4;
return -(textHeight / 2);
}

double? _getBottom(double textWidth, double textHeight) {
final alignment = location.alignment;
if (alignment == Alignment.topCenter ||
alignment == Alignment.topLeft ||
alignment == Alignment.topRight) return null;
if (alignment == Alignment.topCenter || alignment == Alignment.topLeft || alignment == Alignment.topRight) return null;
if (badgeText == null) return -4;
return -(textHeight / 2);
}

double? _getRight(double textWidth, double textHeight) {
final alignment = location.alignment;
if (alignment == Alignment.centerLeft ||
alignment == Alignment.topLeft ||
alignment == Alignment.bottomLeft) return null;
if (alignment == Alignment.centerLeft || alignment == Alignment.topLeft || alignment == Alignment.bottomLeft) return null;
if (badgeText == null) return -4;
return -(textWidth / 2);
}

double? _getLeft(double textWidth, double textHeight) {
final alignment = location.alignment;
if (alignment == Alignment.centerRight ||
alignment == Alignment.topRight ||
alignment == Alignment.bottomRight) return null;
if (alignment == Alignment.centerRight || alignment == Alignment.topRight || alignment == Alignment.bottomRight) return null;
return -(textWidth / 2);
}
}

0 comments on commit 45782ff

Please sign in to comment.