Line data Source code
1 : part of 'foss_text_field.dart';
2 :
3 : const double _iconSize = 18;
4 :
5 : // Leading and trailing icon glyphs sit at 80% of the text color so they read as
6 : // quieter than the value.
7 : const double _affixOpacity = 0.8;
8 :
9 : // Placeholder text sits at 72% of the muted-foreground alpha.
10 : const double _placeholderOpacity = 0.72;
11 :
12 : // Dark surfaces lift the fill by the input color at 32% of its alpha.
13 : const double _darkFillOpacity = 0.32;
14 :
15 : // The label tightens its line height to 18px against the 16px base.
16 : const double _labelLineHeight = 18 / 16;
17 :
18 : /// The size-driven field geometry: resting fill, corner radius, minimum height,
19 : /// and horizontal inset. Shared so every field surface (single-line input,
20 : /// chips input) resolves the same box from the same [size].
21 5 : ({double minHeight, double padX, Color fill, double radius}) fieldMetrics(
22 : FossThemeData theme,
23 : FossTextFieldSize size,
24 : ) {
25 5 : final c = theme.colors;
26 :
27 : // Horizontal inset from the spacing scale: sm sits tighter than md and lg.
28 : // The border paints over the edge without consuming layout, so the inset is
29 : // the padding alone and needs no border compensation.
30 : final (minHeight, padX) = switch (size) {
31 7 : FossTextFieldSize.sm => (30.0, theme.spacing(2.5)),
32 15 : FossTextFieldSize.md => (34.0, theme.spacing(3)),
33 6 : FossTextFieldSize.lg => (38.0, theme.spacing(3)),
34 : };
35 :
36 : // Dark adds a faint lift over the surface: the input color at 32% of its
37 : // alpha, composited to opaque. Light is the bare surface.
38 5 : final fill = c.isDark
39 2 : ? Color.alphaBlend(
40 10 : c.input.withValues(alpha: c.input.a * _darkFillOpacity),
41 2 : c.background,
42 : )
43 5 : : c.background;
44 :
45 10 : return (minHeight: minHeight, padX: padX, fill: fill, radius: theme.radii.lg);
46 : }
47 :
48 : /// Builds the default appearance for a [size] from the theme tokens.
49 4 : _FieldVisuals _resolve(FossThemeData theme, FossTextFieldSize size) {
50 4 : final c = theme.colors;
51 4 : final m = fieldMetrics(theme, size);
52 :
53 4 : return _FieldVisuals(
54 : background: m.fill,
55 4 : borderColor: c.input,
56 4 : textColor: c.foreground,
57 8 : hintColor: c.mutedForeground.withValues(alpha: _placeholderOpacity),
58 4 : labelColor: c.foreground,
59 4 : helperColor: c.mutedForeground,
60 : borderRadius: m.radius,
61 4 : padding: EdgeInsets.symmetric(horizontal: m.padX),
62 : minHeight: m.minHeight,
63 8 : textStyle: theme.typography.base,
64 : // The label uses the tightened 18px line height.
65 16 : labelStyle: theme.typography.base.medium.copyWith(height: _labelLineHeight),
66 8 : helperStyle: theme.typography.xs,
67 : iconSize: _iconSize,
68 8 : gap: theme.spacing(2),
69 8 : shadow: theme.shadows.xs,
70 : );
71 : }
72 :
73 : /// Lays a per-instance [override] over the resolved [base], field by field.
74 4 : _FieldVisuals _apply(_FieldVisuals base, FossTextFieldStyle? override) {
75 : if (override == null) return base;
76 3 : return _FieldVisuals(
77 5 : background: override.backgroundColor ?? base.background,
78 6 : borderColor: override.borderColor ?? base.borderColor,
79 3 : textColor: base.textColor,
80 3 : hintColor: base.hintColor,
81 3 : labelColor: base.labelColor,
82 3 : helperColor: base.helperColor,
83 6 : borderRadius: override.borderRadius ?? base.borderRadius,
84 6 : padding: override.contentPadding ?? base.padding,
85 6 : minHeight: override.minHeight ?? base.minHeight,
86 6 : textStyle: override.textStyle ?? base.textStyle,
87 5 : labelStyle: override.labelStyle ?? base.labelStyle,
88 5 : helperStyle: override.helperStyle ?? base.helperStyle,
89 6 : iconSize: override.iconSize ?? base.iconSize,
90 6 : gap: override.gap ?? base.gap,
91 6 : shadow: override.shadow ?? base.shadow,
92 : );
93 : }
94 :
95 : /// The fully resolved, non-null appearance for one size. A [FossTextFieldStyle]
96 : /// override is laid over it by [_apply], so the widget reads only non-null
97 : /// fields and never needs the null-assertion operator.
98 : @immutable
99 : class _FieldVisuals {
100 4 : const _FieldVisuals({
101 : required this.background,
102 : required this.borderColor,
103 : required this.textColor,
104 : required this.hintColor,
105 : required this.labelColor,
106 : required this.helperColor,
107 : required this.borderRadius,
108 : required this.padding,
109 : required this.minHeight,
110 : required this.textStyle,
111 : required this.labelStyle,
112 : required this.helperStyle,
113 : required this.iconSize,
114 : required this.gap,
115 : required this.shadow,
116 : });
117 :
118 : final Color background;
119 : final Color borderColor;
120 : final Color textColor;
121 : final Color hintColor;
122 : final Color labelColor;
123 : final Color helperColor;
124 : final double borderRadius;
125 : final EdgeInsetsGeometry padding;
126 : final double minHeight;
127 : final TextStyle textStyle;
128 : final TextStyle labelStyle;
129 : final TextStyle helperStyle;
130 : final double iconSize;
131 : final double gap;
132 : final List<BoxShadow> shadow;
133 :
134 8 : IconThemeData get iconTheme => IconThemeData(
135 4 : size: iconSize,
136 20 : color: textColor.withValues(alpha: textColor.a * _affixOpacity),
137 : );
138 : }
|