From 38ba6b7e06b397ea223484a2e7afe5428eadc00b Mon Sep 17 00:00:00 2001 From: Matthias Ngeo Date: Mon, 19 Aug 2024 11:35:42 +0800 Subject: [PATCH] Refactor badge --- forui/lib/src/widgets/badge/badge.dart | 27 +++------------- .../lib/src/widgets/badge/badge_content.dart | 31 +++++++------------ forui/lib/src/widgets/badge/badge_styles.dart | 17 +++------- forui/lib/widgets/badge.dart | 2 ++ 4 files changed, 22 insertions(+), 55 deletions(-) diff --git a/forui/lib/src/widgets/badge/badge.dart b/forui/lib/src/widgets/badge/badge.dart index e90691c9b..94ab9296f 100644 --- a/forui/lib/src/widgets/badge/badge.dart +++ b/forui/lib/src/widgets/badge/badge.dart @@ -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. @@ -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({ @@ -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. /// @@ -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, @@ -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, diff --git a/forui/lib/src/widgets/badge/badge_content.dart b/forui/lib/src/widgets/badge/badge_content.dart index 6cb705c5b..64898f216 100644 --- a/forui/lib/src/widgets/badge/badge_content.dart +++ b/forui/lib/src/widgets/badge/badge_content.dart @@ -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 @@ -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, @@ -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 diff --git a/forui/lib/src/widgets/badge/badge_styles.dart b/forui/lib/src/widgets/badge/badge_styles.dart index 77225b094..067db89fe 100644 --- a/forui/lib/src/widgets/badge/badge_styles.dart +++ b/forui/lib/src/widgets/badge/badge_styles.dart @@ -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 { @@ -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, diff --git a/forui/lib/widgets/badge.dart b/forui/lib/widgets/badge.dart index 2a6548cdf..4faf71b67 100644 --- a/forui/lib/widgets/badge.dart +++ b/forui/lib/widgets/badge.dart @@ -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';