Line data Source code
1 : import 'package:flutter/widgets.dart';
2 : import 'package:fossui/src/components/alert/foss_alert.dart';
3 : import 'package:fossui/src/theme/colors/foss_colors.dart';
4 : import 'package:fossui/src/theme/foss_theme.dart';
5 : import 'package:fossui/src/theme/typography/foss_typography.dart';
6 :
7 : part 'foss_badge_style.dart';
8 :
9 : /// Uniform icon edge for every size, in logical pixels.
10 : const double _iconSize = 14;
11 :
12 : /// Opacity of a leading or trailing icon.
13 : const double _iconOpacity = 0.8;
14 :
15 : /// Soft fill alpha for the tinted variants: a fraction of the role's own alpha,
16 : /// lifted on dark so the tint stays visible on a dark surface.
17 : const double _softFillAlphaLight = 0.08;
18 : const double _softFillAlphaDark = 0.16;
19 :
20 : /// The outline pill lifts its fill on dark by the input color at 32% of its
21 : /// alpha, composited to opaque, matching the field surface.
22 : const double _outlineDarkFillAlpha = 0.32;
23 :
24 : /// The color axis of a [FossBadge]. [primary] is the default solid pill;
25 : /// [secondary], [outline], and [destructive] complete the solid set; [info],
26 : /// [success], [warning], and [error] are soft, tinting the role at low alpha.
27 : enum FossBadgeVariant {
28 : /// Solid primary pill (the default).
29 : primary,
30 :
31 : /// Subtle filled neutral pill.
32 : secondary,
33 :
34 : /// Bordered pill on the surface.
35 : outline,
36 :
37 : /// Solid pill for destructive states.
38 : destructive,
39 :
40 : /// Soft informational pill.
41 : info,
42 :
43 : /// Soft success pill.
44 : success,
45 :
46 : /// Soft warning pill.
47 : warning,
48 :
49 : /// Soft error pill.
50 : error,
51 : }
52 :
53 : /// The size axis of a [FossBadge]: the pill height, minimum width, horizontal
54 : /// padding, and type step. [md] is the default.
55 : enum FossBadgeSize {
56 : /// Compact: 20 logical pixels tall.
57 : sm._(height: 20, padding: 4, radius: 4),
58 :
59 : /// Default: 22 logical pixels tall.
60 : md._(height: 22, padding: 4),
61 :
62 : /// Prominent: 26 logical pixels tall.
63 : lg._(height: 26, padding: 6),
64 : ;
65 :
66 : const FossBadgeSize._({
67 : required this.height,
68 : required this.padding,
69 : this.radius,
70 : });
71 :
72 : /// Pill height, and the minimum width so a single glyph never reads narrower
73 : /// than it is tall.
74 : final double height;
75 :
76 : /// Horizontal padding inside the pill.
77 : final double padding;
78 :
79 : /// Corner radius override, or null to use `radii.sm`.
80 : final double? radius;
81 :
82 2 : TextStyle _type(FossTypography t) => switch (this) {
83 4 : FossBadgeSize.sm => t.xs,
84 2 : FossBadgeSize.md => t.sm,
85 2 : FossBadgeSize.lg => t.base,
86 : };
87 : }
88 :
89 : /// {@category Feedback}
90 : /// {@template foss.badge.preview}
91 : /// <img src="https://fossui.org/components/badge/overview/light.png"
92 : /// alt="FossBadge, light theme" width="480"
93 : /// style="max-width:100%;height:auto" />
94 : /// <img src="https://fossui.org/components/badge/overview/dark.png"
95 : /// alt="FossBadge, dark theme" width="480"
96 : /// style="max-width:100%;height:auto" />
97 : ///
98 : /// See the [badge documentation ↗](https://fossui.org/docs/components/badge) or
99 : /// try it live in the
100 : /// [playground ↗](https://play.fossui.org/components/#/?path=components/badge/fossbadge/playground).
101 : /// {@endtemplate}
102 : ///
103 : /// A compact status pill: a content-hugging, single-line label that tags a
104 : /// count, a state, or a category. Static and non-interactive.
105 : ///
106 : /// [label] is a [Widget] (usually [Text]); the badge applies the weight, color,
107 : /// and type step through `DefaultTextStyle`, so a bare `Text('New')` inherits
108 : /// them. [variant] picks the color (solid or soft tint); [size] the box.
109 : /// [leading] and [trailing] are agnostic icon slots, sized and dimmed by the
110 : /// badge. Colors, type, and radius come from `context.fossTheme`; pass a
111 : /// [FossBadgeStyle] for a one-off override.
112 : ///
113 : /// When the badge is the only carrier of meaning, pass [semanticLabel] so
114 : /// assistive technology announces the state rather than the bare glyph.
115 : ///
116 : /// {@macro foss.customize}
117 : ///
118 : /// See also [FossAlert] for a full-width status message.
119 : ///
120 : /// ```dart
121 : /// FossBadge(
122 : /// label: const Text('Active'),
123 : /// variant: FossBadgeVariant.success,
124 : /// leading: const Icon(checkIcon),
125 : /// );
126 : /// ```
127 : class FossBadge extends StatelessWidget {
128 : /// {@macro foss.badge.preview}
129 : ///
130 : /// Creates a badge. [label] is the pill content; the rest are optional.
131 1 : const FossBadge({
132 : required this.label,
133 : this.variant = FossBadgeVariant.primary,
134 : this.size = FossBadgeSize.md,
135 : this.leading,
136 : this.trailing,
137 : this.semanticLabel,
138 : this.style,
139 : super.key,
140 : });
141 :
142 : /// The pill content, usually [Text].
143 : final Widget label;
144 :
145 : /// The color variant. Defaults to [FossBadgeVariant.primary].
146 : final FossBadgeVariant variant;
147 :
148 : /// The size. Defaults to [FossBadgeSize.md].
149 : final FossBadgeSize size;
150 :
151 : /// Optional leading icon slot.
152 : final Widget? leading;
153 :
154 : /// Optional trailing icon slot.
155 : final Widget? trailing;
156 :
157 : /// Accessibility name that replaces the read-in-place content when set.
158 : final String? semanticLabel;
159 :
160 : /// Per-instance visual overrides.
161 : final FossBadgeStyle? style;
162 :
163 2 : @override
164 : Widget build(BuildContext context) {
165 2 : final theme = context.fossTheme;
166 2 : final colors = theme.colors;
167 2 : final s = style;
168 4 : final v = _resolve(colors, variant);
169 :
170 1 : final foreground = s?.foregroundColor ?? v.foreground;
171 7 : final radius = s?.borderRadius ?? size.radius ?? theme.radii.sm;
172 1 : final border = s?.borderColor ?? v.border;
173 :
174 2 : final labelStyle = size
175 4 : ._type(theme.typography)
176 2 : .medium
177 2 : .copyWith(color: foreground, decoration: TextDecoration.none)
178 3 : .merge(s?.labelStyle);
179 :
180 2 : final pill = DefaultTextStyle.merge(
181 : style: labelStyle,
182 : softWrap: false,
183 : overflow: TextOverflow.clip,
184 : maxLines: 1,
185 2 : child: DecoratedBox(
186 2 : decoration: ShapeDecoration(
187 1 : color: s?.backgroundColor ?? v.fill,
188 2 : shape: RoundedSuperellipseBorder(
189 1 : side: border == null ? BorderSide.none : BorderSide(color: border),
190 2 : borderRadius: BorderRadius.circular(radius),
191 : ),
192 : ),
193 2 : child: ConstrainedBox(
194 2 : constraints: BoxConstraints(
195 4 : minWidth: size.height,
196 4 : minHeight: size.height,
197 : ),
198 2 : child: Padding(
199 6 : padding: EdgeInsets.symmetric(horizontal: size.padding),
200 2 : child: Row(
201 : mainAxisSize: MainAxisSize.min,
202 : mainAxisAlignment: MainAxisAlignment.center,
203 4 : spacing: theme.spacing(1),
204 2 : children: [
205 3 : if (leading case final leading?) _icon(leading, foreground),
206 2 : label,
207 3 : if (trailing case final trailing?) _icon(trailing, foreground),
208 : ],
209 : ),
210 : ),
211 : ),
212 : ),
213 : );
214 :
215 2 : if (semanticLabel == null) return pill;
216 1 : return Semantics(
217 : container: true,
218 1 : label: semanticLabel,
219 1 : child: ExcludeSemantics(child: pill),
220 : );
221 : }
222 :
223 : /// Wraps a decorative icon slot: excluded from semantics, sized to the
224 : /// uniform icon metric, colored to the foreground, and dimmed.
225 2 : Widget _icon(Widget child, Color color) => ExcludeSemantics(
226 1 : child: Opacity(
227 : opacity: _iconOpacity,
228 1 : child: IconTheme.merge(
229 1 : data: IconThemeData(size: _iconSize, color: color),
230 : child: child,
231 : ),
232 : ),
233 : );
234 : }
235 :
236 2 : ({Color fill, Color foreground, Color? border}) _resolve(
237 : FossColors c,
238 : FossBadgeVariant variant,
239 : ) {
240 2 : Color soft(Color role) => role.withValues(
241 3 : alpha: role.a * (c.isDark ? _softFillAlphaDark : _softFillAlphaLight),
242 : );
243 :
244 : return switch (variant) {
245 2 : FossBadgeVariant.primary => (
246 2 : fill: c.primary,
247 2 : foreground: c.primaryForeground,
248 : border: null,
249 : ),
250 1 : FossBadgeVariant.secondary => (
251 1 : fill: c.secondary,
252 1 : foreground: c.secondaryForeground,
253 : border: null,
254 : ),
255 1 : FossBadgeVariant.outline => (
256 1 : fill: c.isDark
257 1 : ? Color.alphaBlend(
258 5 : c.input.withValues(alpha: c.input.a * _outlineDarkFillAlpha),
259 1 : c.background,
260 : )
261 1 : : c.background,
262 1 : foreground: c.foreground,
263 1 : border: c.border,
264 : ),
265 1 : FossBadgeVariant.destructive => (
266 1 : fill: c.destructive,
267 1 : foreground: c.destructiveForegroundOn,
268 : border: null,
269 : ),
270 1 : FossBadgeVariant.info => (
271 2 : fill: soft(c.info),
272 1 : foreground: c.infoForeground,
273 : border: null,
274 : ),
275 1 : FossBadgeVariant.success => (
276 2 : fill: soft(c.success),
277 1 : foreground: c.successForeground,
278 : border: null,
279 : ),
280 1 : FossBadgeVariant.warning => (
281 2 : fill: soft(c.warning),
282 1 : foreground: c.warningForeground,
283 : border: null,
284 : ),
285 1 : FossBadgeVariant.error => (
286 2 : fill: soft(c.destructive),
287 1 : foreground: c.destructiveForeground,
288 : border: null,
289 : ),
290 : };
291 : }
|