Line data Source code
1 : part of 'foss_tabs.dart';
2 :
3 : // Fixed tab geometry, the mobile base values, not the desktop step. Vertical
4 : // padding plus the label line height sums to the 36 tab height, so the tab
5 : // grows with the text under a larger scale instead of clipping.
6 : const double _tabHeight = 36;
7 : const double _tabPadX = 10;
8 : const double _tabPadY = (_tabHeight - 24) / 2;
9 : const double _iconSize = 18;
10 : const double _barThickness = 2;
11 : const double _disabledOpacity = 0.64;
12 :
13 : // The inactive label is dimmed to 72% inside the segmented bar, lifting to
14 : // full strength on hover. The underline variant keeps it at full strength.
15 : const double _segmentedInactiveOpacity = 0.72;
16 :
17 : /// One interactive tab: its label, optional icon, hover and focus wiring, and
18 : /// the semantics that expose it as a selectable tab. Selection, hover, and
19 : /// keyboard state are owned by [_FossTabsState] and flow in through callbacks;
20 : /// this widget only renders and forwards intent.
21 : class _TabButton extends StatelessWidget {
22 1 : const _TabButton({
23 : required this.label,
24 : required this.icon,
25 : required this.enabled,
26 : required this.selected,
27 : required this.hovered,
28 : required this.variant,
29 : required this.horizontal,
30 : required this.visuals,
31 : required this.iconGap,
32 : required this.focusNode,
33 : required this.onSelect,
34 : required this.onEnter,
35 : required this.onExit,
36 : required this.onKeyEvent,
37 : });
38 :
39 : final String label;
40 : final Widget? icon;
41 : final bool enabled;
42 : final bool selected;
43 : final bool hovered;
44 : final FossTabsVariant variant;
45 : final bool horizontal;
46 : final _TabsVisuals visuals;
47 : final double iconGap;
48 : final FocusNode focusNode;
49 : final VoidCallback onSelect;
50 : final VoidCallback onEnter;
51 : final VoidCallback onExit;
52 : final KeyEventResult Function(KeyEvent event) onKeyEvent;
53 :
54 1 : @override
55 : Widget build(BuildContext context) {
56 2 : final underline = variant == FossTabsVariant.underline;
57 1 : final textColor = switch ((selected, underline)) {
58 3 : (true, _) => visuals.activeForeground,
59 4 : (false, true) => visuals.inactiveForeground,
60 4 : (false, false) => visuals.inactiveForeground.withValues(
61 1 : alpha: hovered ? 1 : _segmentedInactiveOpacity,
62 : ),
63 : };
64 :
65 1 : final row = Row(
66 1 : mainAxisSize: horizontal ? MainAxisSize.min : MainAxisSize.max,
67 1 : mainAxisAlignment: horizontal
68 : ? MainAxisAlignment.center
69 : : MainAxisAlignment.start,
70 1 : spacing: iconGap,
71 1 : children: <Widget>[
72 1 : if (icon case final icon?)
73 1 : IconTheme.merge(
74 1 : data: IconThemeData(size: _iconSize, color: textColor),
75 1 : child: ExcludeSemantics(child: icon),
76 : ),
77 1 : Text(
78 1 : label,
79 : maxLines: 1,
80 : softWrap: false,
81 : overflow: TextOverflow.clip,
82 3 : style: visuals.labelStyle.copyWith(color: textColor),
83 : ),
84 : ],
85 : );
86 :
87 : // Underline tabs tint on hover. The segmented active background is the
88 : // sliding indicator, not the tab.
89 1 : Widget interactive = DecoratedBox(
90 1 : decoration: ShapeDecoration(
91 3 : color: underline && hovered ? visuals.hoverColor : null,
92 1 : shape: RoundedSuperellipseBorder(
93 3 : borderRadius: BorderRadius.circular(visuals.tabRadius),
94 : ),
95 : ),
96 1 : child: MouseRegion(
97 1 : cursor: enabled ? SystemMouseCursors.click : SystemMouseCursors.basic,
98 4 : onEnter: enabled ? (_) => onEnter() : null,
99 4 : onExit: enabled ? (_) => onExit() : null,
100 1 : child: GestureDetector(
101 : behavior: HitTestBehavior.opaque,
102 : excludeFromSemantics: true,
103 1 : onTap: enabled
104 1 : ? () {
105 2 : focusNode.requestFocus();
106 2 : onSelect();
107 : }
108 : : null,
109 1 : child: Focus(
110 1 : focusNode: focusNode,
111 1 : canRequestFocus: enabled,
112 1 : skipTraversal: !enabled,
113 3 : onKeyEvent: (_, event) => onKeyEvent(event),
114 1 : child: Padding(
115 : padding: const EdgeInsets.symmetric(
116 : horizontal: _tabPadX,
117 : vertical: _tabPadY,
118 : ),
119 : child: row,
120 : ),
121 : ),
122 : ),
123 : ),
124 : );
125 :
126 1 : if (!enabled) {
127 1 : interactive = Opacity(
128 : opacity: _disabledOpacity,
129 1 : child: IgnorePointer(child: interactive),
130 : );
131 : }
132 :
133 1 : return Semantics(
134 : role: SemanticsRole.tab,
135 : container: true,
136 1 : selected: selected,
137 1 : enabled: enabled,
138 : button: true,
139 1 : label: label,
140 2 : onTap: enabled ? onSelect : null,
141 1 : child: ExcludeSemantics(child: interactive),
142 : );
143 : }
144 : }
145 :
146 : /// The sliding marker for the active tab: an elevated pill behind it in the
147 : /// segmented look, a thin bar on its trailing edge in the underline look. It
148 : /// animates offset and extent to [rect], the active tab's box in strip space.
149 : class _TabIndicator extends StatelessWidget {
150 1 : const _TabIndicator({
151 : required this.rect,
152 : required this.variant,
153 : required this.horizontal,
154 : required this.ltr,
155 : required this.visuals,
156 : required this.duration,
157 : });
158 :
159 : final Rect rect;
160 : final FossTabsVariant variant;
161 : final bool horizontal;
162 : final bool ltr;
163 : final _TabsVisuals visuals;
164 : final Duration duration;
165 :
166 1 : @override
167 : Widget build(BuildContext context) {
168 : // The pill fills the active tab; the bar hugs one edge of it.
169 : final (
170 1 : double left,
171 1 : double top,
172 1 : double width,
173 1 : double height,
174 1 : Widget child,
175 1 : ) = switch (variant) {
176 1 : FossTabsVariant.segmented => (
177 2 : rect.left,
178 2 : rect.top,
179 2 : rect.width,
180 2 : rect.height,
181 1 : DecoratedBox(
182 1 : decoration: ShapeDecoration(
183 2 : color: visuals.indicatorColor,
184 2 : shadows: visuals.indicatorShadow,
185 1 : shape: RoundedSuperellipseBorder(
186 3 : borderRadius: BorderRadius.circular(visuals.tabRadius),
187 : ),
188 : ),
189 : ),
190 : ),
191 2 : FossTabsVariant.underline when horizontal => (
192 2 : rect.left,
193 3 : rect.bottom - _barThickness,
194 2 : rect.width,
195 : _barThickness,
196 3 : ColoredBox(color: visuals.indicatorColor),
197 : ),
198 : FossTabsVariant.underline => (
199 3 : ltr ? rect.left : rect.right - _barThickness,
200 2 : rect.top,
201 : _barThickness,
202 2 : rect.height,
203 3 : ColoredBox(color: visuals.indicatorColor),
204 : ),
205 : };
206 :
207 1 : return AnimatedPositioned(
208 : left: left,
209 : top: top,
210 : width: width,
211 : height: height,
212 1 : duration: duration,
213 : curve: Curves.easeInOut,
214 : child: child,
215 : );
216 : }
217 : }
|