Skip to content

Commit 994fff1

Browse files
Fix RangeSlider thumb overlay remains visible after touch interaction ends (flutter#12560)
Ports flutter#189610 to material_ui, per the porting instructions in flutter#188444. Fixes flutter#189205 After a RangeSlider thumb was touched or dragged, the selected thumb could retain focus and continue painting the focused overlay after the touch gesture ended. This happened because the focused thumb requested keyboard focus during pointer interaction, and RangeSlider painted its focused overlay directly from FocusNode.hasFocus. This PR separates actual thumb focus from the visual focus highlight. Touch interaction can still move focus to the selected thumb for semantics and keyboard continuation, but the focused overlay is only painted when Flutter's focus highlight mode says it should be shown, such as keyboard/traditional focus. This PR also fixes the active drag overlay disappearing while a touch gesture is still in progress. Previously, if the finger moved outside the thumb's overlay bounds during a drag, hover tracking could reverse the shared overlay animation even though the slider was still active. The overlay now remains visible for the active drag and dismisses when the interaction ends. Original PR: flutter#189610 ## Pre-Review Checklist
1 parent 5db6716 commit 994fff1

3 files changed

Lines changed: 284 additions & 8 deletions

File tree

‎packages/material_ui/lib/src/range_slider.dart‎

Lines changed: 79 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,24 @@ class _RangeSliderState extends State<RangeSlider> with TickerProviderStateMixin
507507

508508
bool _dragging = false;
509509

510+
bool _showStartFocusHighlight = false;
511+
void _handleStartFocusHighlightChanged(bool showFocusHighlight) {
512+
if (showFocusHighlight != _showStartFocusHighlight) {
513+
setState(() {
514+
_showStartFocusHighlight = showFocusHighlight;
515+
});
516+
}
517+
}
518+
519+
bool _showEndFocusHighlight = false;
520+
void _handleEndFocusHighlightChanged(bool showFocusHighlight) {
521+
if (showFocusHighlight != _showEndFocusHighlight) {
522+
setState(() {
523+
_showEndFocusHighlight = showFocusHighlight;
524+
});
525+
}
526+
}
527+
510528
bool _hovering = false;
511529
bool _showHoverHighlight = false;
512530
void _handleHoverChanged(bool hovering) {
@@ -622,6 +640,19 @@ class _RangeSliderState extends State<RangeSlider> with TickerProviderStateMixin
622640
return RangeValues(_unlerp(values.start), _unlerp(values.end));
623641
}
624642

643+
Widget _buildThumbFocusDetector({
644+
required FocusNode focusNode,
645+
required ValueChanged<bool> onShowFocusHighlight,
646+
}) {
647+
return FocusableActionDetector(
648+
focusNode: focusNode,
649+
enabled: _enabled,
650+
includeFocusSemantics: false,
651+
onShowFocusHighlight: onShowFocusHighlight,
652+
child: const SizedBox.shrink(),
653+
);
654+
}
655+
625656
// Finds the closest thumb. If both thumbs are close to each other and within
626657
// the touch radius, neither is selected immediately while the drag
627658
// displacement is zero. The first non-zero displacement determines which
@@ -690,6 +721,7 @@ class _RangeSliderState extends State<RangeSlider> with TickerProviderStateMixin
690721
final states = <WidgetState>{
691722
if (!_enabled) WidgetState.disabled,
692723
if (_hovering) WidgetState.hovered,
724+
if (_showStartFocusHighlight || _showEndFocusHighlight) WidgetState.focused,
693725
if (_dragging) WidgetState.dragged,
694726
};
695727

@@ -792,6 +824,8 @@ class _RangeSliderState extends State<RangeSlider> with TickerProviderStateMixin
792824
state: this,
793825
semanticFormatterCallback: widget.semanticFormatterCallback,
794826
hovering: _showHoverHighlight,
827+
startThumbShowFocusHighlight: _showStartFocusHighlight,
828+
endThumbShowFocusHighlight: _showEndFocusHighlight,
795829
),
796830
),
797831
);
@@ -806,12 +840,14 @@ class _RangeSliderState extends State<RangeSlider> with TickerProviderStateMixin
806840
// Adds two invisible focus nodes to the range slider for its two thumbs.
807841
Row(
808842
children: <Widget>[
809-
Focus(
843+
_buildThumbFocusDetector(
810844
focusNode: startFocusNode,
811-
includeSemantics: false,
812-
child: const SizedBox.shrink(),
845+
onShowFocusHighlight: _handleStartFocusHighlightChanged,
846+
),
847+
_buildThumbFocusDetector(
848+
focusNode: endFocusNode,
849+
onShowFocusHighlight: _handleEndFocusHighlightChanged,
813850
),
814-
Focus(focusNode: endFocusNode, includeSemantics: false, child: const SizedBox.shrink()),
815851
],
816852
),
817853
MouseRegion(
@@ -857,6 +893,8 @@ class _RangeSliderRenderObjectWidget extends LeafRenderObjectWidget {
857893
required this.state,
858894
required this.semanticFormatterCallback,
859895
required this.hovering,
896+
required this.startThumbShowFocusHighlight,
897+
required this.endThumbShowFocusHighlight,
860898
});
861899

862900
final RangeValues values;
@@ -871,6 +909,8 @@ class _RangeSliderRenderObjectWidget extends LeafRenderObjectWidget {
871909
final SemanticFormatterCallback? semanticFormatterCallback;
872910
final _RangeSliderState state;
873911
final bool hovering;
912+
final bool startThumbShowFocusHighlight;
913+
final bool endThumbShowFocusHighlight;
874914

875915
@override
876916
_RenderRangeSlider createRenderObject(BuildContext context) {
@@ -890,6 +930,8 @@ class _RangeSliderRenderObjectWidget extends LeafRenderObjectWidget {
890930
semanticFormatterCallback: semanticFormatterCallback,
891931
platform: Theme.of(context).platform,
892932
hovering: hovering,
933+
startThumbShowFocusHighlight: startThumbShowFocusHighlight,
934+
endThumbShowFocusHighlight: endThumbShowFocusHighlight,
893935
gestureSettings: MediaQuery.gestureSettingsOf(context),
894936
);
895937
}
@@ -913,6 +955,8 @@ class _RangeSliderRenderObjectWidget extends LeafRenderObjectWidget {
913955
..semanticFormatterCallback = semanticFormatterCallback
914956
..platform = Theme.of(context).platform
915957
..hovering = hovering
958+
..startThumbShowFocusHighlight = startThumbShowFocusHighlight
959+
..endThumbShowFocusHighlight = endThumbShowFocusHighlight
916960
..gestureSettings = MediaQuery.gestureSettingsOf(context);
917961
}
918962
}
@@ -934,6 +978,8 @@ class _RenderRangeSlider extends RenderBox with RelayoutWhenSystemFontsChangeMix
934978
required this._state,
935979
required this._textDirection,
936980
required this._hovering,
981+
required this._startThumbShowFocusHighlight,
982+
required this._endThumbShowFocusHighlight,
937983
required DeviceGestureSettings gestureSettings,
938984
}) : assert(_values.start >= 0.0 && _values.start <= 1.0),
939985
assert(_values.end >= 0.0 && _values.end <= 1.0) {
@@ -1192,6 +1238,28 @@ class _RenderRangeSlider extends RenderBox with RelayoutWhenSystemFontsChangeMix
11921238
_updateForHover(_hovering);
11931239
}
11941240

1241+
/// True if the start thumb should show the focus highlight.
1242+
bool get startThumbShowFocusHighlight => _startThumbShowFocusHighlight;
1243+
bool _startThumbShowFocusHighlight;
1244+
set startThumbShowFocusHighlight(bool value) {
1245+
if (value == _startThumbShowFocusHighlight) {
1246+
return;
1247+
}
1248+
_startThumbShowFocusHighlight = value;
1249+
markNeedsPaint();
1250+
}
1251+
1252+
/// True if the end thumb should show the focus highlight.
1253+
bool get endThumbShowFocusHighlight => _endThumbShowFocusHighlight;
1254+
bool _endThumbShowFocusHighlight;
1255+
set endThumbShowFocusHighlight(bool value) {
1256+
if (value == _endThumbShowFocusHighlight) {
1257+
return;
1258+
}
1259+
_endThumbShowFocusHighlight = value;
1260+
markNeedsPaint();
1261+
}
1262+
11951263
/// True if the slider is interactive and the start thumb is being
11961264
/// hovered over by a pointer.
11971265
bool _hoveringStartThumb = false;
@@ -1221,7 +1289,11 @@ class _RenderRangeSlider extends RenderBox with RelayoutWhenSystemFontsChangeMix
12211289
if (hovered && (hoveringStartThumb || hoveringEndThumb)) {
12221290
_state.overlayController.forward();
12231291
} else {
1224-
_state.overlayController.reverse();
1292+
// Keep the active drag overlay visible even if the pointer moves outside
1293+
// the thumb bounds during an in-progress drag.
1294+
if (!_active) {
1295+
_state.overlayController.reverse();
1296+
}
12251297
}
12261298
}
12271299

@@ -1641,7 +1713,7 @@ class _RenderRangeSlider extends RenderBox with RelayoutWhenSystemFontsChangeMix
16411713
final bool endThumbSelected = _lastThumbSelection == Thumb.end && !hoveringStartThumb;
16421714
final Size resolvedscreenSize = screenSize.isEmpty ? size : screenSize;
16431715

1644-
if (_state.startFocusNode.hasFocus) {
1716+
if (startThumbShowFocusHighlight) {
16451717
_sliderTheme.overlayShape!.paint(
16461718
context,
16471719
_startThumbCenter,
@@ -1658,7 +1730,7 @@ class _RenderRangeSlider extends RenderBox with RelayoutWhenSystemFontsChangeMix
16581730
);
16591731
}
16601732

1661-
if (_state.endFocusNode.hasFocus) {
1733+
if (endThumbShowFocusHighlight) {
16621734
_sliderTheme.overlayShape!.paint(
16631735
context,
16641736
_endThumbCenter,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
changelog: |
2+
- Fix RangeSlider thumb overlay remaining visible after touch interaction
3+
version: patch

0 commit comments

Comments
 (0)