Skip to content

Commit

Permalink
Refactor badge
Browse files Browse the repository at this point in the history
  • Loading branch information
Pante committed Aug 19, 2024
1 parent d26fd2e commit 38ba6b7
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 55 deletions.
27 changes: 5 additions & 22 deletions forui/lib/src/widgets/badge/badge.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:forui/src/widgets/badge/badge_content.dart';

import 'package:meta/meta.dart';

import 'package:forui/forui.dart';

part 'badge_content.dart';
part 'badge_styles.dart';

/// A badge. Badges are typically used to draw attention to specific information, such as labels and counts.
///
/// The constants in [FBadgeStyle] provide a convenient way to style a badge.
Expand All @@ -29,7 +27,7 @@ class FBadge extends StatelessWidget {
required Widget label,
this.style = FBadgeStyle.primary,
super.key,
}) : builder = ((context, style) => _FBadgeContent(label: label, style: style));
}) : builder = ((context, style) => Content(label: label, style: style));

/// Creates a [FBadge] with custom content.
const FBadge.raw({
Expand Down Expand Up @@ -76,8 +74,8 @@ class FBadge extends StatelessWidget {

/// A [FBadge]'s style.
///
/// A style can be either one of the pre-defined styles in [FBadgeStyle] or a [FBadgeCustomStyle]. The pre-defined styles
/// are a convenient shorthand for the various [FBadgeCustomStyle]s in the current context's [FBadgeStyles].
/// A style can be either one of the pre-defined styles in [FBadgeStyle] or a [FBadgeCustomStyle]. The pre-defined
/// styles are a convenient shorthand for the various [FBadgeCustomStyle]s in the current context's [FBadgeStyles].
sealed class FBadgeStyle {
/// The badge's primary style.
///
Expand Down Expand Up @@ -131,7 +129,7 @@ final class FBadgeCustomStyle with Diagnosticable implements FBadgeStyle {
final FBadgeContentStyle content;

/// Creates a [FBadgeCustomStyle].
FBadgeCustomStyle({
const FBadgeCustomStyle({
required this.backgroundColor,
required this.borderColor,
required this.content,
Expand All @@ -149,21 +147,6 @@ final class FBadgeCustomStyle with Diagnosticable implements FBadgeStyle {
borderWidth = style.borderWidth;

/// Returns a copy of this [FBadgeCustomStyle] with the given properties replaced.
///
/// ```dart
/// final style = FBadgeCustomStyle(
/// backgroundColor: Colors.red,
/// borderColor: Colors.black,
/// // other properties omitted for brevity
/// );
///
/// final copy = style.copyWith(
/// backgroundColor: Colors.blue,
/// );
///
/// print(copy.backgroundColor); // Colors.blue
/// print(copy.borderColor); // Colors.black
/// ```
@useResult
FBadgeCustomStyle copyWith({
Color? backgroundColor,
Expand Down
31 changes: 11 additions & 20 deletions forui/lib/src/widgets/badge/badge_content.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
part of 'badge.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:forui/forui.dart';
import 'package:meta/meta.dart';

final class _FBadgeContent extends StatelessWidget {
@internal
final class Content extends StatelessWidget {
final FBadgeCustomStyle style;
final Widget label;

const _FBadgeContent({
const Content({
required this.style,
required this.label,
super.key,
});

@override
Expand All @@ -29,33 +34,19 @@ final class _FBadgeContent extends StatelessWidget {

/// [FBadge] content's style.
final class FBadgeContentStyle with Diagnosticable {
static const _defaultPadding = EdgeInsets.symmetric(horizontal: 14, vertical: 2);

/// The label's [TextStyle].
final TextStyle labelTextStyle;

/// The padding. Defaults to `EdgeInsets.symmetric(horizontal: 14, vertical: 2)`.
/// The padding.
final EdgeInsets padding;

/// Creates a [FBadgeContentStyle].
FBadgeContentStyle({
required this.labelTextStyle,
this.padding = _defaultPadding,
this.padding = const EdgeInsets.symmetric(horizontal: 14, vertical: 2),
});

/// Returns a copy of this [FBadgeContentStyle] with the given properties replaced.
///
/// ```dart
/// final style = FBadgeContentStyle(
/// labelTextStyle: TextStyle(...),
/// padding = EdgeInsets.zero,
/// );
///
/// final copy = style.copyWith(padding: EdgeInsets.symmetric(vertical: 10));
///
/// print(style.labelTextStyle == copy.labelTextStyle); // true
/// print(style.padding == copy.padding); // false
/// ```
@useResult
FBadgeContentStyle copyWith({TextStyle? labelTextStyle, EdgeInsets? padding}) => FBadgeContentStyle(
labelTextStyle: labelTextStyle ?? this.labelTextStyle,
Expand All @@ -67,7 +58,7 @@ final class FBadgeContentStyle with Diagnosticable {
super.debugFillProperties(properties);
properties
..add(DiagnosticsProperty('labelTextStyle', labelTextStyle))
..add(DiagnosticsProperty('padding', padding, defaultValue: _defaultPadding));
..add(DiagnosticsProperty('padding', padding));
}

@override
Expand Down
17 changes: 4 additions & 13 deletions forui/lib/src/widgets/badge/badge_styles.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
part of 'badge.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/rendering.dart';
import 'package:forui/forui.dart';
import 'package:meta/meta.dart';

/// The [FBadgeCustomStyle]s.
class FBadgeStyles with Diagnosticable {
Expand Down Expand Up @@ -70,18 +73,6 @@ class FBadgeStyles with Diagnosticable {
);

/// Returns a copy of this [FBadgeStyles] with the given properties replaced.
///
/// ```dart
/// final styles = FBadgeStyles(
/// primary: ...,
/// secondary: ...,
/// );
///
/// final copy = styles.copyWith(secondary: ...);
///
/// print(styles.primary == copy.primary); // true
/// print(styles.secondary == copy.secondary); // false
/// ```
@useResult
FBadgeStyles copyWith({
FBadgeCustomStyle? primary,
Expand Down
2 changes: 2 additions & 0 deletions forui/lib/widgets/badge.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
library forui.widgets.badge;

export '../src/widgets/badge/badge.dart' hide Variant;
export '../src/widgets/badge/badge_content.dart' hide Content;
export '../src/widgets/badge/badge_styles.dart';

0 comments on commit 38ba6b7

Please sign in to comment.