From d7321d92b8736ab402546ba2937c8fcd9886dd8e Mon Sep 17 00:00:00 2001 From: Greg Spencer Date: Mon, 8 Jun 2020 14:04:45 -0700 Subject: [PATCH] Make _RenderSlider not be a semantics container (#58535) This PR makes _RenderSlider not be a semantics container. This is so that the FocusableActionDetector in the Slider widget will get to aggregate the semantics information, since otherwise Talkback won't focus the slider because it thinks that the focus node doesn't have anything to say (which it doesn't but the _RenderSlider child does). If the _RenderSlider is a semantics container, then it keeps its speakable information to itself, but it isn't the Focus widget, so when the keyboard focus goes to the focus node, the accessibility focus doesn't move. Since the _RenderSlider is always wrapped by the Slider widget, there's nothing lost in making it not be a container. --- packages/flutter/lib/src/material/slider.dart | 91 +++++++++------ .../flutter/test/material/slider_test.dart | 106 ++++++++++++------ 2 files changed, 126 insertions(+), 71 deletions(-) diff --git a/packages/flutter/lib/src/material/slider.dart b/packages/flutter/lib/src/material/slider.dart index 96ca44b460..acb4d05d30 100644 --- a/packages/flutter/lib/src/material/slider.dart +++ b/packages/flutter/lib/src/material/slider.dart @@ -712,32 +712,35 @@ class _SliderState extends State with TickerProviderStateMixin { // in range_slider.dart. Size _screenSize() => MediaQuery.of(context).size; - return FocusableActionDetector( - actions: _actionMap, - shortcuts: _shortcutMap, - focusNode: widget.focusNode, - autofocus: widget.autofocus, - enabled: _enabled, - onShowFocusHighlight: _handleFocusHighlightChanged, - onShowHoverHighlight: _handleHoverChanged, - mouseCursor: effectiveMouseCursor, - child: CompositedTransformTarget( - link: _layerLink, - child: _SliderRenderObjectWidget( - key: _renderObjectKey, - value: _unlerp(widget.value), - divisions: widget.divisions, - label: widget.label, - sliderTheme: sliderTheme, - textScaleFactor: MediaQuery.of(context).textScaleFactor, - screenSize: _screenSize(), - onChanged: (widget.onChanged != null) && (widget.max > widget.min) ? _handleChanged : null, - onChangeStart: widget.onChangeStart != null ? _handleDragStart : null, - onChangeEnd: widget.onChangeEnd != null ? _handleDragEnd : null, - state: this, - semanticFormatterCallback: widget.semanticFormatterCallback, - hasFocus: _focused, - hovering: _hovering, + return Semantics( + container: true, + child: FocusableActionDetector( + actions: _actionMap, + shortcuts: _shortcutMap, + focusNode: widget.focusNode, + autofocus: widget.autofocus, + enabled: _enabled, + onShowFocusHighlight: _handleFocusHighlightChanged, + onShowHoverHighlight: _handleHoverChanged, + mouseCursor: effectiveMouseCursor, + child: CompositedTransformTarget( + link: _layerLink, + child: _SliderRenderObjectWidget( + key: _renderObjectKey, + value: _unlerp(widget.value), + divisions: widget.divisions, + label: widget.label, + sliderTheme: sliderTheme, + textScaleFactor: MediaQuery.of(context).textScaleFactor, + screenSize: _screenSize(), + onChanged: (widget.onChanged != null) && (widget.max > widget.min) ? _handleChanged : null, + onChangeStart: widget.onChangeStart != null ? _handleDragStart : null, + onChangeEnd: widget.onChangeEnd != null ? _handleDragEnd : null, + state: this, + semanticFormatterCallback: widget.semanticFormatterCallback, + hasFocus: _focused, + hovering: _hovering, + ), ), ), ); @@ -1110,6 +1113,7 @@ class _RenderSlider extends RenderBox with RelayoutWhenSystemFontsChangeMixin { return; _hasFocus = value; _updateForFocusOrHover(_hasFocus); + markNeedsSemanticsUpdate(); } /// True if this slider is being hovered over by a pointer. @@ -1136,6 +1140,7 @@ class _RenderSlider extends RenderBox with RelayoutWhenSystemFontsChangeMixin { } } } + bool get showValueIndicator { bool showValueIndicator; switch (_sliderTheme.showValueIndicator) { @@ -1472,20 +1477,32 @@ class _RenderSlider extends RenderBox with RelayoutWhenSystemFontsChangeMixin { void describeSemanticsConfiguration(SemanticsConfiguration config) { super.describeSemanticsConfiguration(config); - config.isSemanticBoundary = isInteractive; + // The Slider widget has its own Focus widget with semantics information, + // and we want that semantics node to collect the semantics information here + // so that it's all in the same node: otherwise Talkback sees that the node + // has focusable children, and it won't focus the Slider's Focus widget + // because it thinks the Focus widget's node doesn't have anything to say + // (which it doesn't, but this child does). Aggregating the semantic + // information into one node means that Talkback will recognize that it has + // something to say and focus it when it receives keyboard focus. + // (See https://github.com/flutter/flutter/issues/57038 for context). + config.isSemanticBoundary = false; + + config.isEnabled = isInteractive; + config.textDirection = textDirection; if (isInteractive) { - config.textDirection = textDirection; config.onIncrease = increaseAction; config.onDecrease = decreaseAction; - if (semanticFormatterCallback != null) { - config.value = semanticFormatterCallback(_state._lerp(value)); - config.increasedValue = semanticFormatterCallback(_state._lerp((value + _semanticActionUnit).clamp(0.0, 1.0) as double)); - config.decreasedValue = semanticFormatterCallback(_state._lerp((value - _semanticActionUnit).clamp(0.0, 1.0) as double)); - } else { - config.value = '${(value * 100).round()}%'; - config.increasedValue = '${((value + _semanticActionUnit).clamp(0.0, 1.0) * 100).round()}%'; - config.decreasedValue = '${((value - _semanticActionUnit).clamp(0.0, 1.0) * 100).round()}%'; - } + } + config.label = _label ?? ''; + if (semanticFormatterCallback != null) { + config.value = semanticFormatterCallback(_state._lerp(value)); + config.increasedValue = semanticFormatterCallback(_state._lerp((value + _semanticActionUnit).clamp(0.0, 1.0) as double)); + config.decreasedValue = semanticFormatterCallback(_state._lerp((value - _semanticActionUnit).clamp(0.0, 1.0) as double)); + } else { + config.value = '${(value * 100).round()}%'; + config.increasedValue = '${((value + _semanticActionUnit).clamp(0.0, 1.0) * 100).round()}%'; + config.decreasedValue = '${((value - _semanticActionUnit).clamp(0.0, 1.0) * 100).round()}%'; } } diff --git a/packages/flutter/test/material/slider_test.dart b/packages/flutter/test/material/slider_test.dart index 6aa83ca4b8..285c815f11 100644 --- a/packages/flutter/test/material/slider_test.dart +++ b/packages/flutter/test/material/slider_test.dart @@ -1347,17 +1347,12 @@ void main() { children: [ TestSemantics( id: 3, - flags: [SemanticsFlag.isFocusable], - children: [ - TestSemantics( - id: 4, - value: '50%', - increasedValue: '55%', - decreasedValue: '45%', - textDirection: TextDirection.ltr, - actions: SemanticsAction.decrease.index | SemanticsAction.increase.index, - ), - ], + flags: [SemanticsFlag.hasEnabledState, SemanticsFlag.isEnabled, SemanticsFlag.isFocusable], + actions: [SemanticsAction.increase, SemanticsAction.decrease], + value: '50%', + increasedValue: '55%', + decreasedValue: '45%', + textDirection: TextDirection.ltr, ), ], ), @@ -1400,7 +1395,12 @@ void main() { flags: [SemanticsFlag.scopesRoute], children: [ TestSemantics( - id: 5, + id: 3, + flags: [SemanticsFlag.hasEnabledState], + value: '50%', + increasedValue: '55%', + decreasedValue: '45%', + textDirection: TextDirection.ltr, ), ], ), @@ -1455,17 +1455,60 @@ void main() { children: [ TestSemantics( id: 3, - flags: [SemanticsFlag.isFocusable], - children: [ - TestSemantics( - id: 4, - value: '50%', - increasedValue: '60%', - decreasedValue: '40%', - textDirection: TextDirection.ltr, - actions: SemanticsAction.decrease.index | SemanticsAction.increase.index, - ), - ], + flags: [SemanticsFlag.hasEnabledState, SemanticsFlag.isEnabled, SemanticsFlag.isFocusable], + actions: [SemanticsAction.increase, SemanticsAction.decrease], + value: '50%', + increasedValue: '60%', + decreasedValue: '40%', + textDirection: TextDirection.ltr, + ), + ], + ), + ], + ), + ], + ), + ignoreRect: true, + ignoreTransform: true, + ), + ); + + // Disable slider + await tester.pumpWidget(MaterialApp( + home: Directionality( + textDirection: TextDirection.ltr, + child: MediaQuery( + data: MediaQueryData.fromWindow(window), + child: const Material( + child: Slider( + value: 0.5, + onChanged: null, + ), + ), + ), + ), + )); + + expect( + semantics, + hasSemantics( + TestSemantics.root( + children: [ + TestSemantics( + id: 1, + textDirection: TextDirection.ltr, + children: [ + TestSemantics( + id: 2, + flags: [SemanticsFlag.scopesRoute], + children: [ + TestSemantics( + id: 4, + flags: [SemanticsFlag.hasEnabledState], + value: '50%', + increasedValue: '60%', + decreasedValue: '40%', + textDirection: TextDirection.ltr, ), ], ), @@ -1517,17 +1560,12 @@ void main() { children: [ TestSemantics( id: 3, - flags: [SemanticsFlag.isFocusable], - children: [ - TestSemantics( - id: 4, - value: '40', - increasedValue: '60', - decreasedValue: '20', - textDirection: TextDirection.ltr, - actions: SemanticsAction.decrease.index | SemanticsAction.increase.index, - ), - ], + flags: [SemanticsFlag.hasEnabledState, SemanticsFlag.isEnabled, SemanticsFlag.isFocusable], + actions: [SemanticsAction.increase, SemanticsAction.decrease], + value: '40', + increasedValue: '60', + decreasedValue: '20', + textDirection: TextDirection.ltr, ), ], ),