Line data Source code
1 : part of 'foss_bottom_nav_bar.dart';
2 :
3 : // Fixed cell geometry. The glyph is a touch dimension rather than type, so it
4 : // does not grow with the text scaler; the label absorbs the scale and the cell
5 : // grows past the floor with it.
6 : const double _minCellHeight = 56;
7 : const double _disabledOpacity = 0.64;
8 :
9 : /// One destination cell: the glyph over its label, the badge pinned to the
10 : /// glyph, hover cursor, focus wiring and the keyboard focus ring, and the
11 : /// semantics that expose it as a selectable tab. Selection and keyboard state
12 : /// are owned by [_FossBottomNavBarState] and flow in through callbacks; this
13 : /// widget only renders and forwards intent.
14 : class _NavCell extends StatefulWidget {
15 1 : const _NavCell({
16 : required this.label,
17 : required this.icon,
18 : required this.badge,
19 : required this.enabled,
20 : required this.selected,
21 : required this.visuals,
22 : required this.gap,
23 : required this.padY,
24 : required this.badgeOffset,
25 : required this.focusNode,
26 : required this.onSelect,
27 : required this.onKeyEvent,
28 : });
29 :
30 : final String label;
31 : final Widget? icon;
32 : final Widget? badge;
33 : final bool enabled;
34 : final bool selected;
35 : final _NavVisuals visuals;
36 : final double gap;
37 : final double padY;
38 : final double badgeOffset;
39 : final FocusNode focusNode;
40 : final VoidCallback onSelect;
41 : final KeyEventResult Function(KeyEvent event) onKeyEvent;
42 :
43 1 : @override
44 1 : State<_NavCell> createState() => _NavCellState();
45 : }
46 :
47 : class _NavCellState extends State<_NavCell> {
48 : // True only while focused under keyboard navigation; a tap focuses the cell
49 : // too, but in touch highlight mode, so it paints no ring.
50 : bool _showRing = false;
51 :
52 1 : @override
53 : Widget build(BuildContext context) {
54 2 : final visuals = widget.visuals;
55 2 : final enabled = widget.enabled;
56 2 : final color = widget.selected
57 1 : ? visuals.selectedColor
58 1 : : visuals.unselectedColor;
59 :
60 1 : final column = Column(
61 : mainAxisSize: MainAxisSize.min,
62 : mainAxisAlignment: MainAxisAlignment.center,
63 2 : spacing: widget.gap,
64 1 : children: <Widget>[
65 2 : if (widget.icon case final icon?)
66 1 : Stack(
67 : // The badge overhangs the glyph box; the default clip would eat it.
68 : clipBehavior: Clip.none,
69 1 : children: <Widget>[
70 1 : IconTheme.merge(
71 2 : data: IconThemeData(size: visuals.iconSize, color: color),
72 1 : child: ExcludeSemantics(child: icon),
73 : ),
74 2 : if (widget.badge case final badge?)
75 : // Anchored just inside the glyph's end edge and grown outward,
76 : // so a badge wider than the gap overhangs the cell rather than
77 : // covering the glyph it belongs to.
78 1 : PositionedDirectional(
79 3 : top: -widget.badgeOffset,
80 4 : start: visuals.iconSize - widget.badgeOffset,
81 1 : child: ExcludeSemantics(child: badge),
82 : ),
83 : ],
84 : ),
85 1 : Text(
86 2 : widget.label,
87 : maxLines: 1,
88 : overflow: TextOverflow.ellipsis,
89 : textAlign: TextAlign.center,
90 2 : style: visuals.labelStyle.copyWith(color: color),
91 : ),
92 : ],
93 : );
94 :
95 : // Keys reach this node from the cell's own node below it, so the bar's
96 : // navigation runs before the app-level activate shortcuts.
97 1 : Widget interactive = Focus(
98 : canRequestFocus: false,
99 : skipTraversal: true,
100 4 : onKeyEvent: (_, event) => widget.onKeyEvent(event),
101 1 : child: FocusableActionDetector(
102 2 : focusNode: widget.focusNode,
103 : enabled: enabled,
104 : mouseCursor: enabled
105 : ? SystemMouseCursors.click
106 : : SystemMouseCursors.basic,
107 4 : onShowFocusHighlight: (value) => setState(() => _showRing = value),
108 : // The floor sits on the cell rather than the bar, so the target is the
109 : // full bar height without an intrinsic-height pass over the row.
110 1 : child: ConstrainedBox(
111 2 : constraints: BoxConstraints(minHeight: visuals.minHeight),
112 1 : child: GestureDetector(
113 : behavior: HitTestBehavior.opaque,
114 : excludeFromSemantics: true,
115 : onTap: enabled
116 1 : ? () {
117 3 : widget.focusNode.requestFocus();
118 3 : widget.onSelect();
119 : }
120 : : null,
121 1 : child: CustomPaint(
122 1 : foregroundPainter: _showRing
123 1 : ? _FocusRingPainter(
124 1 : color: visuals.ringColor,
125 1 : radius: visuals.ringRadius,
126 : )
127 : : null,
128 : // No Center here: the floor already tightens the cell's minimum
129 : // height and the column centres inside it. A Center would expand
130 : // to whatever maximum height the parent offers, stretching the
131 : // bar in any bounded-height slot.
132 1 : child: Padding(
133 3 : padding: EdgeInsets.symmetric(vertical: widget.padY),
134 : child: column,
135 : ),
136 : ),
137 : ),
138 : ),
139 : ),
140 : );
141 :
142 : if (!enabled) {
143 1 : interactive = Opacity(
144 : opacity: _disabledOpacity,
145 1 : child: IgnorePointer(child: interactive),
146 : );
147 : }
148 :
149 1 : return Semantics(
150 : role: SemanticsRole.tab,
151 : container: true,
152 2 : selected: widget.selected,
153 : enabled: enabled,
154 : button: true,
155 2 : label: widget.label,
156 2 : onTap: enabled ? widget.onSelect : null,
157 1 : child: ExcludeSemantics(child: interactive),
158 : );
159 : }
160 : }
161 :
162 : /// Paints the keyboard focus ring: a superellipse stroke inset just inside the
163 : /// cell edge, so the abutting cells and the bar edge never clip it.
164 : class _FocusRingPainter extends CustomPainter {
165 1 : const _FocusRingPainter({required this.color, required this.radius});
166 :
167 : final Color color;
168 : final double radius;
169 :
170 1 : @override
171 : void paint(Canvas canvas, Size size) {
172 1 : canvas.drawRSuperellipse(
173 1 : RSuperellipse.fromRectAndRadius(
174 3 : (Offset.zero & size).deflate(_ringWidth / 2),
175 4 : Radius.circular(radius - _ringWidth / 2),
176 : ),
177 1 : Paint()
178 2 : ..color = color
179 1 : ..style = PaintingStyle.stroke
180 1 : ..strokeWidth = _ringWidth,
181 : );
182 : }
183 :
184 0 : @override
185 : bool shouldRepaint(_FocusRingPainter old) =>
186 0 : old.color != color || old.radius != radius;
187 : }
|