Line data Source code
1 : import 'dart:math' as math;
2 :
3 : import 'package:flutter/widgets.dart';
4 : import 'package:fossui/src/components/button/foss_button.dart';
5 : import 'package:fossui/src/theme/theme.dart';
6 :
7 : part 'foss_card_style.dart';
8 :
9 : // Inner top-lit rim at rest: a faint dark line in light mode, a faint white
10 : // highlight in dark mode, the same rim the controls carry.
11 : const Color _rimLight = Color(0x0A000000);
12 : const Color _rimDark = Color(0x0FFFFFFF);
13 :
14 : /// {@category Layout}
15 : /// {@template foss.card.preview}
16 : /// <img src="https://fossui.org/components/card/overview/light.png"
17 : /// alt="FossCard, light theme" width="480"
18 : /// style="max-width:100%;height:auto" />
19 : /// <img src="https://fossui.org/components/card/overview/dark.png"
20 : /// alt="FossCard, dark theme" width="480"
21 : /// style="max-width:100%;height:auto" />
22 : ///
23 : /// See the [card documentation ↗](https://fossui.org/docs/components/card) or
24 : /// try it live in the
25 : /// [playground ↗](https://play.fossui.org/components/#/?path=components/card/fosscard/playground).
26 : /// {@endtemplate}
27 : ///
28 : /// A static content container: a bordered, rounded surface that groups an
29 : /// optional header (title, description, trailing action), an optional content
30 : /// body, and an optional footer. Every slot is optional and content-agnostic;
31 : /// the surface renders, it does not respond.
32 : ///
33 : /// The seam between two adjacent slots tightens so their paddings do not double
34 : /// up: the outer edges keep the full inset while a shared seam reads as half.
35 : /// Colors, type, radius, and shadow come from `context.fossTheme`; pass a
36 : /// [FossCardStyle] for a one-off override.
37 : ///
38 : /// {@macro foss.customize}
39 : ///
40 : /// See also [FossButton] for the actions a card commonly holds.
41 : ///
42 : /// ```dart
43 : /// FossCard(
44 : /// title: const Text('Project'),
45 : /// description: const Text('Manage your settings.'),
46 : /// action: FossButton(
47 : /// variant: FossButtonVariant.outline,
48 : /// onPressed: addProject,
49 : /// child: const Text('Add'),
50 : /// ),
51 : /// content: const ProjectSummary(),
52 : /// footer: Row(
53 : /// mainAxisAlignment: MainAxisAlignment.end,
54 : /// children: [FossButton(onPressed: save, child: const Text('Save'))],
55 : /// ),
56 : /// );
57 : /// ```
58 : class FossCard extends StatelessWidget {
59 : /// {@macro foss.card.preview}
60 : ///
61 : /// Creates a card. Every slot is optional; a card with only [content] is
62 : /// valid, as is a header with no footer.
63 1 : const FossCard({
64 : this.title,
65 : this.description,
66 : this.action,
67 : this.content,
68 : this.footer,
69 : this.style,
70 : super.key,
71 : });
72 :
73 : /// The title, rendered at the top of the header.
74 : final Widget? title;
75 :
76 : /// The description, rendered below the title.
77 : final Widget? description;
78 :
79 : /// A trailing header control (a button, a menu), pinned to the top corner.
80 : final Widget? action;
81 :
82 : /// The body of the card.
83 : final Widget? content;
84 :
85 : /// A trailing or leading row of actions or metadata.
86 : final Widget? footer;
87 :
88 : /// Per-instance visual overrides.
89 : final FossCardStyle? style;
90 :
91 1 : @override
92 : Widget build(BuildContext context) {
93 1 : final theme = context.fossTheme;
94 1 : final colors = theme.colors;
95 1 : final sp = theme.spacing;
96 1 : final s = style;
97 :
98 3 : final hasHeader = title != null || description != null || action != null;
99 1 : final hasContent = content != null;
100 1 : final hasFooter = footer != null;
101 :
102 3 : final radius = s?.borderRadius ?? theme.radii.xl2;
103 1 : final shape = RoundedSuperellipseBorder(
104 3 : side: BorderSide(color: s?.borderColor ?? colors.border),
105 1 : borderRadius: BorderRadius.circular(radius),
106 : );
107 :
108 : // The seam between adjacent slots collapses to spacing(4): the header drops
109 : // its bottom pad when content follows, content drops its touching edges,
110 : // and the footer drops its top pad when content precedes. A lone slot keeps
111 : // full spacing(6) on every side.
112 1 : final column = Column(
113 : mainAxisSize: MainAxisSize.min,
114 : crossAxisAlignment: CrossAxisAlignment.stretch,
115 1 : children: [
116 : if (hasHeader)
117 1 : Padding(
118 1 : padding: EdgeInsets.fromLTRB(
119 1 : sp(6),
120 1 : sp(6),
121 1 : sp(6),
122 2 : hasContent ? sp(4) : sp(6),
123 : ),
124 1 : child: _buildHeader(theme, colors, s),
125 : ),
126 1 : if (content case final content?)
127 1 : Padding(
128 1 : padding: EdgeInsets.fromLTRB(
129 1 : sp(6),
130 1 : hasHeader ? 0 : sp(6),
131 1 : sp(6),
132 1 : hasFooter ? 0 : sp(6),
133 : ),
134 : child: content,
135 : ),
136 1 : if (footer case final footer?)
137 1 : Padding(
138 1 : padding: EdgeInsets.fromLTRB(
139 1 : sp(6),
140 2 : hasContent ? sp(4) : sp(6),
141 1 : sp(6),
142 1 : sp(6),
143 : ),
144 : child: footer,
145 : ),
146 : ],
147 : );
148 :
149 1 : final dark = colors.isDark;
150 1 : return DecoratedBox(
151 1 : decoration: ShapeDecoration(
152 2 : color: s?.backgroundColor ?? colors.card,
153 : shape: shape,
154 3 : shadows: s?.shadows ?? theme.shadows.xs,
155 : ),
156 : // Every slot is inset, so nothing reaches the corners; the surface needs
157 : // no clip. Plain text in any slot inherits the card foreground.
158 1 : child: CustomPaint(
159 1 : foregroundPainter: _RimPainter(
160 : color: dark ? _rimDark : _rimLight,
161 2 : radius: math.max(radius - 1, 0),
162 : topLit: dark,
163 : ),
164 1 : child: DefaultTextStyle.merge(
165 2 : style: TextStyle(color: colors.cardForeground),
166 : child: column,
167 : ),
168 : ),
169 : );
170 : }
171 :
172 1 : Widget _buildHeader(
173 : FossThemeData theme,
174 : FossColors colors,
175 : FossCardStyle? s,
176 : ) {
177 3 : final titleStyle = theme.typography.lg.semibold
178 2 : .copyWith(color: colors.cardForeground, height: 1)
179 1 : .merge(s?.titleStyle);
180 2 : final descriptionStyle = theme.typography.sm
181 2 : .copyWith(color: colors.mutedForeground)
182 1 : .merge(s?.descriptionStyle);
183 :
184 1 : final text = Column(
185 : crossAxisAlignment: CrossAxisAlignment.start,
186 : mainAxisSize: MainAxisSize.min,
187 2 : spacing: theme.spacing(1.5),
188 1 : children: [
189 1 : if (title case final title?)
190 1 : DefaultTextStyle.merge(style: titleStyle, child: title),
191 1 : if (description case final description?)
192 1 : DefaultTextStyle.merge(style: descriptionStyle, child: description),
193 : ],
194 : );
195 :
196 1 : if (action case final action?) {
197 : // The action pins to the top-trailing corner, spanning the text block;
198 : // Expanded mirrors it to the leading edge in RTL.
199 1 : return Row(
200 : crossAxisAlignment: CrossAxisAlignment.start,
201 2 : spacing: theme.spacing(1.5),
202 1 : children: [
203 1 : Expanded(child: text),
204 : action,
205 : ],
206 : );
207 : }
208 : return text;
209 : }
210 : }
211 :
212 : /// Paints a 1px rim inside the surface: brightest along one edge, fading to
213 : /// nothing by the middle. [topLit] lights the top edge; otherwise the bottom.
214 : class _RimPainter extends CustomPainter {
215 1 : const _RimPainter({
216 : required this.color,
217 : required this.radius,
218 : required this.topLit,
219 : });
220 :
221 : final Color color;
222 : final double radius;
223 : final bool topLit;
224 :
225 1 : @override
226 : void paint(Canvas canvas, Size size) {
227 2 : final rect = (Offset.zero & size).deflate(0.5);
228 1 : final shape = RSuperellipse.fromRectAndRadius(
229 : rect,
230 2 : Radius.circular(radius),
231 : );
232 1 : final shader = LinearGradient(
233 1 : begin: topLit ? Alignment.topCenter : Alignment.bottomCenter,
234 : end: Alignment.center,
235 4 : colors: [color, color.withValues(alpha: 0)],
236 1 : ).createShader(rect);
237 1 : final paint = Paint()
238 1 : ..shader = shader
239 1 : ..style = PaintingStyle.stroke
240 1 : ..strokeWidth = 1;
241 1 : canvas.drawRSuperellipse(shape, paint);
242 : }
243 :
244 1 : @override
245 : bool shouldRepaint(_RimPainter oldDelegate) =>
246 3 : oldDelegate.color != color ||
247 3 : oldDelegate.radius != radius ||
248 3 : oldDelegate.topLit != topLit;
249 : }
|