Skip to content

Commit 14552a9

Browse files
authored
Toggable Refactor (#76745)
1 parent fc99e7c commit 14552a9

8 files changed

Lines changed: 1048 additions & 1307 deletions

File tree

‎packages/flutter/lib/src/material/checkbox.dart‎

Lines changed: 116 additions & 229 deletions
Large diffs are not rendered by default.

‎packages/flutter/lib/src/material/radio.dart‎

Lines changed: 62 additions & 201 deletions
Original file line numberDiff line numberDiff line change
@@ -353,45 +353,14 @@ class Radio<T> extends StatefulWidget {
353353
/// {@macro flutter.widgets.Focus.autofocus}
354354
final bool autofocus;
355355

356+
bool get _selected => value == groupValue;
357+
356358
@override
357359
_RadioState<T> createState() => _RadioState<T>();
358360
}
359361

360-
class _RadioState<T> extends State<Radio<T>> with TickerProviderStateMixin {
361-
bool get enabled => widget.onChanged != null;
362-
late Map<Type, Action<Intent>> _actionMap;
363-
364-
@override
365-
void initState() {
366-
super.initState();
367-
_actionMap = <Type, Action<Intent>>{
368-
ActivateIntent: CallbackAction<ActivateIntent>(
369-
onInvoke: _actionHandler,
370-
),
371-
};
372-
}
373-
374-
void _actionHandler(ActivateIntent intent) {
375-
if (widget.onChanged != null) {
376-
widget.onChanged!(widget.value);
377-
}
378-
final RenderObject renderObject = context.findRenderObject()!;
379-
renderObject.sendSemanticsEvent(const TapSemanticEvent());
380-
}
381-
382-
bool _focused = false;
383-
void _handleHighlightChanged(bool focused) {
384-
if (_focused != focused) {
385-
setState(() { _focused = focused; });
386-
}
387-
}
388-
389-
bool _hovering = false;
390-
void _handleHoverChanged(bool hovering) {
391-
if (_hovering != hovering) {
392-
setState(() { _hovering = hovering; });
393-
}
394-
}
362+
class _RadioState<T> extends State<Radio<T>> with TickerProviderStateMixin, ToggleableStateMixin {
363+
final _RadioPainter _painter = _RadioPainter();
395364

396365
void _handleChanged(bool? selected) {
397366
if (selected == null) {
@@ -403,14 +372,28 @@ class _RadioState<T> extends State<Radio<T>> with TickerProviderStateMixin {
403372
}
404373
}
405374

406-
bool get _selected => widget.value == widget.groupValue;
375+
@override
376+
void didUpdateWidget(Radio<T> oldWidget) {
377+
super.didUpdateWidget(oldWidget);
378+
if (widget._selected != oldWidget._selected) {
379+
animateToValue();
380+
}
381+
}
382+
383+
@override
384+
void dispose() {
385+
_painter.dispose();
386+
super.dispose();
387+
}
388+
389+
@override
390+
ValueChanged<bool?>? get onChanged => widget.onChanged != null ? _handleChanged : null;
391+
392+
@override
393+
bool get tristate => widget.toggleable;
407394

408-
Set<MaterialState> get _states => <MaterialState>{
409-
if (!enabled) MaterialState.disabled,
410-
if (_hovering) MaterialState.hovered,
411-
if (_focused) MaterialState.focused,
412-
if (_selected) MaterialState.selected,
413-
};
395+
@override
396+
bool? get value => widget._selected;
414397

415398
MaterialStateProperty<Color?> get _widgetFillColor {
416399
return MaterialStateProperty.resolveWith((Set<MaterialState> states) {
@@ -457,15 +440,17 @@ class _RadioState<T> extends State<Radio<T>> with TickerProviderStateMixin {
457440
break;
458441
}
459442
size += effectiveVisualDensity.baseSizeAdjustment;
460-
final BoxConstraints additionalConstraints = BoxConstraints.tight(size);
461-
final MouseCursor effectiveMouseCursor = MaterialStateProperty.resolveAs<MouseCursor?>(widget.mouseCursor, _states)
462-
?? themeData.radioTheme.mouseCursor?.resolve(_states)
463-
?? MaterialStateProperty.resolveAs<MouseCursor>(MaterialStateMouseCursor.clickable, _states);
443+
444+
final MaterialStateProperty<MouseCursor> effectiveMouseCursor = MaterialStateProperty.resolveWith<MouseCursor>((Set<MaterialState> states) {
445+
return MaterialStateProperty.resolveAs<MouseCursor?>(widget.mouseCursor, states)
446+
?? themeData.radioTheme.mouseCursor?.resolve(states)
447+
?? MaterialStateProperty.resolveAs<MouseCursor>(MaterialStateMouseCursor.clickable, states);
448+
});
464449

465450
// Colors need to be resolved in selected and non selected states separately
466451
// so that they can be lerped between.
467-
final Set<MaterialState> activeStates = _states..add(MaterialState.selected);
468-
final Set<MaterialState> inactiveStates = _states..remove(MaterialState.selected);
452+
final Set<MaterialState> activeStates = states..add(MaterialState.selected);
453+
final Set<MaterialState> inactiveStates = states..remove(MaterialState.selected);
469454
final Color effectiveActiveColor = widget.fillColor?.resolve(activeStates)
470455
?? _widgetFillColor.resolve(activeStates)
471456
?? themeData.radioTheme.fillColor?.resolve(activeStates)
@@ -475,13 +460,13 @@ class _RadioState<T> extends State<Radio<T>> with TickerProviderStateMixin {
475460
?? themeData.radioTheme.fillColor?.resolve(inactiveStates)
476461
?? _defaultFillColor.resolve(inactiveStates);
477462

478-
final Set<MaterialState> focusedStates = _states..add(MaterialState.focused);
463+
final Set<MaterialState> focusedStates = states..add(MaterialState.focused);
479464
final Color effectiveFocusOverlayColor = widget.overlayColor?.resolve(focusedStates)
480465
?? widget.focusColor
481466
?? themeData.radioTheme.overlayColor?.resolve(focusedStates)
482467
?? themeData.focusColor;
483468

484-
final Set<MaterialState> hoveredStates = _states..add(MaterialState.hovered);
469+
final Set<MaterialState> hoveredStates = states..add(MaterialState.hovered);
485470
final Color effectiveHoverOverlayColor = widget.overlayColor?.resolve(hoveredStates)
486471
?? widget.hoverColor
487472
?? themeData.radioTheme.overlayColor?.resolve(hoveredStates)
@@ -497,156 +482,40 @@ class _RadioState<T> extends State<Radio<T>> with TickerProviderStateMixin {
497482
?? themeData.radioTheme.overlayColor?.resolve(inactivePressedStates)
498483
?? effectiveActiveColor.withAlpha(kRadialReactionAlpha);
499484

500-
501-
return FocusableActionDetector(
502-
actions: _actionMap,
503-
focusNode: widget.focusNode,
504-
autofocus: widget.autofocus,
505-
mouseCursor: effectiveMouseCursor,
506-
enabled: enabled,
507-
onShowFocusHighlight: _handleHighlightChanged,
508-
onShowHoverHighlight: _handleHoverChanged,
509-
child: Builder(
510-
builder: (BuildContext context) {
511-
return _RadioRenderObjectWidget(
512-
selected: _selected,
513-
activeColor: effectiveActiveColor,
514-
inactiveColor: effectiveInactiveColor,
515-
focusColor: effectiveFocusOverlayColor,
516-
hoverColor: effectiveHoverOverlayColor,
517-
reactionColor: effectiveActivePressedOverlayColor,
518-
inactiveReactionColor: effectiveInactivePressedOverlayColor,
519-
splashRadius: widget.splashRadius ?? themeData.radioTheme.splashRadius ?? kRadialReactionRadius,
520-
onChanged: enabled ? _handleChanged : null,
521-
toggleable: widget.toggleable,
522-
additionalConstraints: additionalConstraints,
523-
vsync: this,
524-
hasFocus: _focused,
525-
hovering: _hovering,
526-
);
527-
},
485+
return Semantics(
486+
inMutuallyExclusiveGroup: true,
487+
checked: widget._selected,
488+
child: buildToggleable(
489+
focusNode: widget.focusNode,
490+
autofocus: widget.autofocus,
491+
mouseCursor: effectiveMouseCursor,
492+
size: size,
493+
painter: _painter
494+
..position = position
495+
..reaction = reaction
496+
..reactionFocusFade = reactionFocusFade
497+
..reactionHoverFade = reactionHoverFade
498+
..inactiveReactionColor = effectiveInactivePressedOverlayColor
499+
..reactionColor = effectiveActivePressedOverlayColor
500+
..hoverColor = effectiveHoverOverlayColor
501+
..focusColor = effectiveFocusOverlayColor
502+
..splashRadius = widget.splashRadius ?? themeData.radioTheme.splashRadius ?? kRadialReactionRadius
503+
..downPosition = downPosition
504+
..isFocused = states.contains(MaterialState.focused)
505+
..isHovered = states.contains(MaterialState.hovered)
506+
..activeColor = effectiveActiveColor
507+
..inactiveColor = effectiveInactiveColor
528508
),
529509
);
530510
}
531511
}
532512

533-
class _RadioRenderObjectWidget extends LeafRenderObjectWidget {
534-
const _RadioRenderObjectWidget({
535-
Key? key,
536-
required this.selected,
537-
required this.activeColor,
538-
required this.inactiveColor,
539-
required this.focusColor,
540-
required this.hoverColor,
541-
required this.reactionColor,
542-
required this.inactiveReactionColor,
543-
required this.additionalConstraints,
544-
this.onChanged,
545-
required this.toggleable,
546-
required this.vsync,
547-
required this.hasFocus,
548-
required this.hovering,
549-
required this.splashRadius,
550-
}) : assert(selected != null),
551-
assert(activeColor != null),
552-
assert(inactiveColor != null),
553-
assert(vsync != null),
554-
assert(toggleable != null),
555-
super(key: key);
556-
557-
final bool selected;
558-
final bool hasFocus;
559-
final bool hovering;
560-
final Color inactiveColor;
561-
final Color activeColor;
562-
final Color focusColor;
563-
final Color hoverColor;
564-
final Color reactionColor;
565-
final Color inactiveReactionColor;
566-
final double splashRadius;
567-
final ValueChanged<bool?>? onChanged;
568-
final bool toggleable;
569-
final TickerProvider vsync;
570-
final BoxConstraints additionalConstraints;
571-
513+
class _RadioPainter extends ToggleablePainter {
572514
@override
573-
_RenderRadio createRenderObject(BuildContext context) => _RenderRadio(
574-
value: selected,
575-
activeColor: activeColor,
576-
inactiveColor: inactiveColor,
577-
focusColor: focusColor,
578-
hoverColor: hoverColor,
579-
reactionColor: reactionColor,
580-
inactiveReactionColor: inactiveReactionColor,
581-
splashRadius: splashRadius,
582-
onChanged: onChanged,
583-
tristate: toggleable,
584-
vsync: vsync,
585-
additionalConstraints: additionalConstraints,
586-
hasFocus: hasFocus,
587-
hovering: hovering,
588-
);
515+
void paint(Canvas canvas, Size size) {
516+
paintRadialReaction(canvas: canvas, origin: size.center(Offset.zero));
589517

590-
@override
591-
void updateRenderObject(BuildContext context, _RenderRadio renderObject) {
592-
renderObject
593-
..value = selected
594-
..activeColor = activeColor
595-
..inactiveColor = inactiveColor
596-
..focusColor = focusColor
597-
..hoverColor = hoverColor
598-
..reactionColor = reactionColor
599-
..inactiveReactionColor = inactiveReactionColor
600-
..splashRadius = splashRadius
601-
..onChanged = onChanged
602-
..tristate = toggleable
603-
..additionalConstraints = additionalConstraints
604-
..vsync = vsync
605-
..hasFocus = hasFocus
606-
..hovering = hovering;
607-
}
608-
}
609-
610-
class _RenderRadio extends RenderToggleable {
611-
_RenderRadio({
612-
required bool value,
613-
required Color activeColor,
614-
required Color inactiveColor,
615-
required Color focusColor,
616-
required Color hoverColor,
617-
required Color reactionColor,
618-
required Color inactiveReactionColor,
619-
required double splashRadius,
620-
required ValueChanged<bool?>? onChanged,
621-
required bool tristate,
622-
required BoxConstraints additionalConstraints,
623-
required TickerProvider vsync,
624-
required bool hasFocus,
625-
required bool hovering,
626-
}) : super(
627-
value: value,
628-
activeColor: activeColor,
629-
inactiveColor: inactiveColor,
630-
focusColor: focusColor,
631-
hoverColor: hoverColor,
632-
reactionColor: reactionColor,
633-
inactiveReactionColor: inactiveReactionColor,
634-
splashRadius: splashRadius,
635-
onChanged: onChanged,
636-
tristate: tristate,
637-
additionalConstraints: additionalConstraints,
638-
vsync: vsync,
639-
hasFocus: hasFocus,
640-
hovering: hovering,
641-
);
642-
643-
@override
644-
void paint(PaintingContext context, Offset offset) {
645-
final Canvas canvas = context.canvas;
646-
647-
paintRadialReaction(canvas, offset, size.center(Offset.zero));
648-
649-
final Offset center = (offset & size).center;
518+
final Offset center = (Offset.zero & size).center;
650519

651520
// Outer circle
652521
final Paint paint = Paint()
@@ -661,12 +530,4 @@ class _RenderRadio extends RenderToggleable {
661530
canvas.drawCircle(center, _kInnerRadius * position.value, paint);
662531
}
663532
}
664-
665-
@override
666-
void describeSemanticsConfiguration(SemanticsConfiguration config) {
667-
super.describeSemanticsConfiguration(config);
668-
config
669-
..isInMutuallyExclusiveGroup = true
670-
..isChecked = value == true;
671-
}
672533
}

0 commit comments

Comments
 (0)