Line data Source code
1 : import 'package:flutter/widgets.dart';
2 : import 'package:fossui/src/foundation/foss_since.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 : /// The type step of a [FossText], one entry per [FossTypography] size. [base]
8 : /// (16 logical pixels) is the default.
9 : enum FossTextSize {
10 : /// 12 px: captions and fine print.
11 : xs,
12 :
13 : /// 14 px: the most common body size.
14 : sm,
15 :
16 : /// 16 px: body (the default).
17 : base,
18 :
19 : /// 18 px: subheadings.
20 : lg,
21 :
22 : /// 20 px: headings.
23 : xl,
24 :
25 : /// 24 px: large headings.
26 : xl2;
27 :
28 1 : TextStyle _style(FossTypography t) => switch (this) {
29 2 : FossTextSize.xs => t.xs,
30 2 : FossTextSize.sm => t.sm,
31 2 : FossTextSize.base => t.base,
32 2 : FossTextSize.lg => t.lg,
33 2 : FossTextSize.xl => t.xl,
34 2 : FossTextSize.xl2 => t.xl2,
35 : };
36 : }
37 :
38 : /// The weight of a [FossText]. [regular] (400) is the default.
39 : enum FossTextWeight {
40 : /// Regular weight (400): body text.
41 : regular(FontWeight.w400),
42 :
43 : /// Medium weight (500): labels.
44 : medium(FontWeight.w500),
45 :
46 : /// Semibold weight (600): headings.
47 : semibold(FontWeight.w600),
48 :
49 : /// Bold weight (700): emphasis.
50 : bold(FontWeight.w700);
51 :
52 : const FossTextWeight(this.value);
53 :
54 : /// The underlying [FontWeight].
55 : final FontWeight value;
56 : }
57 :
58 : /// A semantic color role for a [FossText], resolved from `FossColors`. Leave
59 : /// the color null to use the `foreground` role.
60 : enum FossTextColor {
61 : /// The default text color.
62 : foreground,
63 :
64 : /// Dimmed text for secondary content.
65 : mutedForeground,
66 :
67 : /// The brand accent color.
68 : primary,
69 :
70 : /// The destructive (danger) color.
71 : destructive;
72 :
73 1 : Color _resolve(FossColors c) => switch (this) {
74 2 : FossTextColor.foreground => c.foreground,
75 2 : FossTextColor.mutedForeground => c.mutedForeground,
76 2 : FossTextColor.primary => c.primary,
77 2 : FossTextColor.destructive => c.destructive,
78 : };
79 : }
80 :
81 : /// {@category Typography}
82 : /// {@template foss.text.preview}
83 : /// <img src="https://fossui.org/components/text/overview/light.png"
84 : /// alt="FossText, light theme" width="480"
85 : /// style="max-width:100%;height:auto" />
86 : /// <img src="https://fossui.org/components/text/overview/dark.png"
87 : /// alt="FossText, dark theme" width="480"
88 : /// style="max-width:100%;height:auto" />
89 : ///
90 : /// See the [text documentation ↗](https://fossui.org/docs/components/text) or
91 : /// try it live in the
92 : /// [playground ↗](https://play.fossui.org/components/#/?path=components/text/fosstext/playground).
93 : /// {@endtemplate}
94 : ///
95 : /// On-brand text in one line. [FossText] renders a string in a [FossTypography]
96 : /// step at one of four weights, resolving the type scale and the bundled font
97 : /// from `context.fossTheme` so the text stays on theme without reaching into
98 : /// the tokens.
99 : ///
100 : /// Pick the step and weight with [size] and [weight], or use a named
101 : /// constructor ([FossText.body], [FossText.title], [FossText.heading], and the
102 : /// like) for a common role. [color] takes a semantic [FossTextColor] role; when
103 : /// null the text uses the `foreground` role. [style] merges over the resolved
104 : /// style, winning on any field it sets. Set [header] on a true document heading
105 : /// so assistive technology can navigate to it. The remaining parameters pass
106 : /// straight through to the underlying `Text`.
107 : ///
108 : /// {@macro foss.customize}
109 : ///
110 : /// ```dart
111 : /// FossText.title('Account settings');
112 : /// FossText(
113 : /// 'The quick brown fox.',
114 : /// size: FossTextSize.sm,
115 : /// color: FossTextColor.mutedForeground,
116 : /// );
117 : /// ```
118 : @FossSince('0.1.1')
119 : class FossText extends StatelessWidget {
120 : /// {@macro foss.text.preview}
121 : ///
122 : /// Creates on-brand text. [data] is the string; [size], [weight], and [color]
123 : /// pick the style, the rest pass through to `Text`.
124 1 : const FossText(
125 : this.data, {
126 : this.size = FossTextSize.base,
127 : this.weight = FossTextWeight.regular,
128 : this.color,
129 : this.textAlign,
130 : this.maxLines,
131 : this.overflow,
132 : this.softWrap,
133 : this.semanticsLabel,
134 : this.style,
135 : this.header = false,
136 : super.key,
137 : });
138 :
139 : /// Caption text: [FossTextSize.xs], [FossTextWeight.regular].
140 1 : const FossText.caption(
141 : this.data, {
142 : this.color,
143 : this.textAlign,
144 : this.maxLines,
145 : this.overflow,
146 : this.softWrap,
147 : this.semanticsLabel,
148 : this.style,
149 : this.header = false,
150 : super.key,
151 : }) : size = FossTextSize.xs,
152 : weight = FossTextWeight.regular;
153 :
154 : /// Body text: [FossTextSize.sm], [FossTextWeight.regular].
155 1 : const FossText.body(
156 : this.data, {
157 : this.color,
158 : this.textAlign,
159 : this.maxLines,
160 : this.overflow,
161 : this.softWrap,
162 : this.semanticsLabel,
163 : this.style,
164 : this.header = false,
165 : super.key,
166 : }) : size = FossTextSize.sm,
167 : weight = FossTextWeight.regular;
168 :
169 : /// Label text: [FossTextSize.sm], [FossTextWeight.medium].
170 1 : const FossText.label(
171 : this.data, {
172 : this.color,
173 : this.textAlign,
174 : this.maxLines,
175 : this.overflow,
176 : this.softWrap,
177 : this.semanticsLabel,
178 : this.style,
179 : this.header = false,
180 : super.key,
181 : }) : size = FossTextSize.sm,
182 : weight = FossTextWeight.medium;
183 :
184 : /// Title text: [FossTextSize.lg], [FossTextWeight.semibold].
185 1 : const FossText.title(
186 : this.data, {
187 : this.color,
188 : this.textAlign,
189 : this.maxLines,
190 : this.overflow,
191 : this.softWrap,
192 : this.semanticsLabel,
193 : this.style,
194 : this.header = false,
195 : super.key,
196 : }) : size = FossTextSize.lg,
197 : weight = FossTextWeight.semibold;
198 :
199 : /// Heading text: [FossTextSize.xl], [FossTextWeight.semibold].
200 1 : const FossText.heading(
201 : this.data, {
202 : this.color,
203 : this.textAlign,
204 : this.maxLines,
205 : this.overflow,
206 : this.softWrap,
207 : this.semanticsLabel,
208 : this.style,
209 : this.header = false,
210 : super.key,
211 : }) : size = FossTextSize.xl,
212 : weight = FossTextWeight.semibold;
213 :
214 : /// Display text: [FossTextSize.xl2], [FossTextWeight.bold].
215 1 : const FossText.display(
216 : this.data, {
217 : this.color,
218 : this.textAlign,
219 : this.maxLines,
220 : this.overflow,
221 : this.softWrap,
222 : this.semanticsLabel,
223 : this.style,
224 : this.header = false,
225 : super.key,
226 : }) : size = FossTextSize.xl2,
227 : weight = FossTextWeight.bold;
228 :
229 : /// The string to render.
230 : final String data;
231 :
232 : /// The type step. Defaults to [FossTextSize.base].
233 : final FossTextSize size;
234 :
235 : /// The weight. Defaults to [FossTextWeight.regular].
236 : final FossTextWeight weight;
237 :
238 : /// The color role. Defaults to the `foreground` role when null.
239 : final FossTextColor? color;
240 :
241 : /// How the text aligns horizontally.
242 : final TextAlign? textAlign;
243 :
244 : /// The maximum number of lines before the text clips or ellipsizes.
245 : final int? maxLines;
246 :
247 : /// How visual overflow is handled.
248 : final TextOverflow? overflow;
249 :
250 : /// Whether the text should wrap at soft line breaks.
251 : final bool? softWrap;
252 :
253 : /// Accessibility name that replaces the visible string when set.
254 : final String? semanticsLabel;
255 :
256 : /// A per-instance style merged over the resolved style, winning on any field
257 : /// it sets.
258 : final TextStyle? style;
259 :
260 : /// Whether to mark the text as a heading for assistive technology, so screen
261 : /// readers can navigate to it by heading. Defaults to false; set it on true
262 : /// document headings, not on every visually large label.
263 : final bool header;
264 :
265 1 : @override
266 : Widget build(BuildContext context) {
267 1 : final theme = context.fossTheme;
268 1 : final resolved = size
269 2 : ._style(theme.typography)
270 1 : .copyWith(
271 2 : fontWeight: weight.value,
272 5 : color: color?._resolve(theme.colors) ?? theme.colors.foreground,
273 : )
274 2 : .merge(style);
275 :
276 1 : final text = Text(
277 1 : data,
278 : style: resolved,
279 1 : textAlign: textAlign,
280 1 : maxLines: maxLines,
281 1 : overflow: overflow,
282 1 : softWrap: softWrap,
283 1 : semanticsLabel: semanticsLabel,
284 : );
285 :
286 1 : if (!header) return text;
287 1 : return Semantics(header: true, child: text);
288 : }
289 : }
|