@@ -99,7 +99,7 @@ void _toggleRecipe(NodeBuilder b, Node n, List<RenderBox> chain, ToggleKind kind
9999
100100/// A checkbox, a switch or a radio: its value, its look in its states, the
101101/// keyboard and screen readers.
102- class RenderToggle extends RenderControl {
102+ class RenderToggle extends RenderControl implements LabelledControl {
103103 RenderToggle () {
104104 onPressed = _toggle;
105105 focusNode.onKey = _key;
@@ -141,6 +141,18 @@ class RenderToggle extends RenderControl {
141141 ..curve = kind == ToggleKind .switchToggle && n.props.has (SwitchProps .toggleCurve)
142142 ? n.prop (SwitchProps .toggleCurve)
143143 : api.AnimationCurve .easeOut;
144+ final label = switch (kind) {
145+ ToggleKind .checkbox => n.prop (CheckboxProps .label),
146+ ToggleKind .switchToggle => n.prop (SwitchProps .label),
147+ ToggleKind .radio => n.prop (RadioProps .label),
148+ };
149+ final style = TextStyle (fontFamily: tokens.fontFamily, fontSize: 13 , height: 1.5 , color: tokens.primaryText);
150+ if (_labelText.update (label, style, align: TextAlign .start) || label != _label) {
151+ _label = label;
152+ _labelText.layout (double .infinity);
153+ markNeedsLayout ();
154+ markNeedsSemanticsUpdate ();
155+ }
144156 _follow (_boundValue (n));
145157 if (changed) {
146158 markNeedsLayout ();
@@ -303,15 +315,42 @@ class RenderToggle extends RenderControl {
303315
304316 // --- geometry -----------------------------------------------------------------
305317
306- /// Its size when it hugs: its design size, else SSUI's.
307- Size get _natural {
318+ String _label = '' ;
319+ final ControlLabel _labelText = ControlLabel ();
320+
321+ bool get _hasLabel => _label.isNotEmpty;
322+
323+ /// The text beside the mark ('' for none).
324+ String get label => _label;
325+
326+ /// The gap between the mark and its label (SSUI's 8).
327+ static const double labelGap = 8 ;
328+
329+ /// The mark's size when it hugs: its design size, else SSUI's. With a
330+ /// label the mark is always SSUI's and the design size is the whole
331+ /// control's.
332+ Size get _markNatural {
308333 final ssui = switch (_kind) {
309334 ToggleKind .switchToggle => const Size (36 , 20 ),
310335 _ => const Size (16 , 16 ),
311336 };
337+ if (_hasLabel) return ssui;
312338 return Size (_design.width > 0 ? _design.width : ssui.width, _design.height > 0 ? _design.height : ssui.height);
313339 }
314340
341+ /// Its size when it hugs: the mark, and its label beside it.
342+ Size get _natural {
343+ final mark = _markNatural;
344+ if (! _hasLabel) return mark;
345+ return Size (mark.width + labelGap + _labelText.intrinsicWidth.ceilToDouble (),
346+ math.max (mark.height, _labelText.size.height.ceilToDouble ()));
347+ }
348+
349+ @override
350+ List <({String text, String ? fontFamily, List <Rect > glyphs})> get labels => ! _hasLabel || ! hasSize
351+ ? const []
352+ : [(text: _label, fontFamily: _labelText.fontFamily, glyphs: _labelText.glyphBoxes (_markNatural.width + labelGap, size.height / 2 ))];
353+
315354 @override
316355 double computeMinIntrinsicWidth (double height) => _natural.width;
317356 @override
@@ -325,10 +364,21 @@ class RenderToggle extends RenderControl {
325364 Size computeDryLayout (covariant BoxConstraints constraints) => constraints.constrain (_natural);
326365
327366 @override
328- void performLayout () => size = constraints.constrain (_natural);
367+ void performLayout () {
368+ if (_hasLabel) {
369+ final room = constraints.maxWidth - _markNatural.width - labelGap;
370+ _labelText.layout (room.isFinite ? math.max (0 , room) : double .infinity);
371+ }
372+ size = constraints.constrain (_natural);
373+ }
329374
330375 /// Where the mark is: a checkbox's box, a radio's ring, a switch's track.
331376 Rect get markRect {
377+ if (_hasLabel) {
378+ // Beside its label: SSUI's mark at the start, centred on the line.
379+ final m = _markNatural;
380+ return Rect .fromLTWH (0 , (size.height - m.height) / 2 , m.width, m.height);
381+ }
332382 final box = Offset .zero & size;
333383 final n = node;
334384 switch (_kind) {
@@ -439,6 +489,7 @@ class RenderToggle extends RenderControl {
439489 case ToggleKind .switchToggle:
440490 _paintSwitch (canvas);
441491 }
492+ if (_hasLabel) _labelText.paint (canvas, _markNatural.width + labelGap, size.height / 2 );
442493 canvas.restore ();
443494 }
444495
@@ -565,7 +616,7 @@ class RenderToggle extends RenderControl {
565616 // --- semantics -----------------------------------------------------------------
566617
567618 @override
568- String get semanticsName => '' ;
619+ String get semanticsName => _label ;
569620
570621 @override
571622 void describeSemanticsConfiguration (SemanticsConfiguration config) {
0 commit comments