Skip to content

Commit 8f96e69

Browse files
authored
[material_ui] Add alternative input method for RangeSlider in NavigationMode.directional (flutter#12630)
Ports flutter#187816 to `material_ui`, following the instructions in flutter#188444. The commits were cherry-picked from the original PR, with only file paths and imports adjusted for this repo. In `NavigationMode.directional` (TV remotes, D-pads) the arrow keys are needed to move focus between views, so a focused `RangeSlider` thumb had no way to adjust its value. This adds the pattern discussed in flutter#181968: - Arrow keys only move focus. - Enter on a focused thumb enters a value adjustment mode, where Left and Right adjust the value. - Enter again, or losing focus, exits that mode. `NavigationMode.traditional` is unchanged: arrow keys adjust the value directly. This mirrors the keyboard handling `Slider` already has in this package. ## Tests New test group `RangeSlider keyboard with NavigationMode.directional` in `packages/material_ui/test/range_slider_test.dart`, covering each case above plus traditional navigation. Fixes flutter#181968 Supersedes flutter#187816, which will be closed with a pointer to this PR. ## Pre-Review Checklist **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent 44caf48 commit 8f96e69

3 files changed

Lines changed: 424 additions & 19 deletions

File tree

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

Lines changed: 163 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import 'package:flutter/foundation.dart';
2121
import 'package:flutter/gestures.dart';
2222
import 'package:flutter/rendering.dart';
2323
import 'package:flutter/scheduler.dart' show timeDilation;
24+
import 'package:flutter/services.dart';
2425
import 'package:flutter/widgets.dart';
2526

2627
import 'color_scheme.dart';
@@ -484,6 +485,54 @@ class _RangeSliderState extends State<RangeSlider> with TickerProviderStateMixin
484485
final FocusNode startFocusNode = FocusNode();
485486
final FocusNode endFocusNode = FocusNode();
486487

488+
final GlobalKey _renderObjectKey = GlobalKey();
489+
490+
// Whether the slider is currently in the value adjustment mode while using
491+
// directional navigation (e.g. a TV remote or D-pad). When false, arrow keys
492+
// are left unhandled so they can move the focus between views; when true,
493+
// arrow keys adjust the slider value. Toggled by the enter key. This has no
494+
// effect when using traditional navigation.
495+
bool _isEditingInDirectionalMode = false;
496+
497+
// The ambient navigation mode, cached so build() doesn't have to re-read the
498+
// MediaQuery (and re-pick a shortcut map) on every pass.
499+
NavigationMode _navigationMode = NavigationMode.traditional;
500+
501+
// Keyboard mapping for a focused range slider.
502+
static const Map<ShortcutActivator, Intent> _traditionalNavShortcutMap =
503+
<ShortcutActivator, Intent>{
504+
SingleActivator(LogicalKeyboardKey.arrowUp): _AdjustSliderIntent.up(),
505+
SingleActivator(LogicalKeyboardKey.arrowDown): _AdjustSliderIntent.down(),
506+
SingleActivator(LogicalKeyboardKey.arrowLeft): _AdjustSliderIntent.left(),
507+
SingleActivator(LogicalKeyboardKey.arrowRight): _AdjustSliderIntent.right(),
508+
};
509+
510+
// Keyboard mapping for a focused range slider when using directional
511+
// navigation and not in the value adjustment mode. Only the enter key is
512+
// handled, to enter the value adjustment mode. The arrow keys are left
513+
// unhandled so they can move the focus between views.
514+
static const Map<ShortcutActivator, Intent> _directionalNavShortcutMap =
515+
<ShortcutActivator, Intent>{
516+
SingleActivator(LogicalKeyboardKey.enter): _ToggleRangeSliderEditModeIntent(),
517+
};
518+
519+
// Keyboard mapping for a focused range slider when using directional
520+
// navigation and in the value adjustment mode. The horizontal inputs adjust
521+
// the value and the enter key exits the value adjustment mode. The vertical
522+
// inputs are not handled to allow navigating out of the slider.
523+
static const Map<ShortcutActivator, Intent> _directionalNavEditingShortcutMap =
524+
<ShortcutActivator, Intent>{
525+
SingleActivator(LogicalKeyboardKey.arrowLeft): _AdjustSliderIntent.left(),
526+
SingleActivator(LogicalKeyboardKey.arrowRight): _AdjustSliderIntent.right(),
527+
SingleActivator(LogicalKeyboardKey.enter): _ToggleRangeSliderEditModeIntent(),
528+
};
529+
530+
// Action mapping for the start thumb.
531+
late Map<Type, Action<Intent>> _startActionMap;
532+
533+
// Action mapping for the end thumb.
534+
late Map<Type, Action<Intent>> _endActionMap;
535+
487536
// Animation controller that is run when the overlay (a.k.a radial reaction)
488537
// changes visibility in response to user interaction.
489538
late AnimationController overlayController;
@@ -544,6 +593,22 @@ class _RangeSliderState extends State<RangeSlider> with TickerProviderStateMixin
544593
@override
545594
void initState() {
546595
super.initState();
596+
_startActionMap = <Type, Action<Intent>>{
597+
_AdjustSliderIntent: CallbackAction<_AdjustSliderIntent>(
598+
onInvoke: (_AdjustSliderIntent intent) => _actionHandler(intent, Thumb.start),
599+
),
600+
_ToggleRangeSliderEditModeIntent: CallbackAction<_ToggleRangeSliderEditModeIntent>(
601+
onInvoke: (_ToggleRangeSliderEditModeIntent intent) => _toggleEditMode(),
602+
),
603+
};
604+
_endActionMap = <Type, Action<Intent>>{
605+
_AdjustSliderIntent: CallbackAction<_AdjustSliderIntent>(
606+
onInvoke: (_AdjustSliderIntent intent) => _actionHandler(intent, Thumb.end),
607+
),
608+
_ToggleRangeSliderEditModeIntent: CallbackAction<_ToggleRangeSliderEditModeIntent>(
609+
onInvoke: (_ToggleRangeSliderEditModeIntent intent) => _toggleEditMode(),
610+
),
611+
};
547612
overlayController = AnimationController(duration: kRadialReactionDuration, vsync: this);
548613
valueIndicatorController = AnimationController(
549614
duration: valueIndicatorAnimationDuration,
@@ -566,6 +631,12 @@ class _RangeSliderState extends State<RangeSlider> with TickerProviderStateMixin
566631
);
567632
}
568633

634+
@override
635+
void didChangeDependencies() {
636+
super.didChangeDependencies();
637+
_navigationMode = MediaQuery.navigationModeOf(context);
638+
}
639+
569640
@override
570641
void didUpdateWidget(RangeSlider oldWidget) {
571642
super.didUpdateWidget(oldWidget);
@@ -605,6 +676,41 @@ class _RangeSliderState extends State<RangeSlider> with TickerProviderStateMixin
605676
}
606677
}
607678

679+
void _actionHandler(_AdjustSliderIntent intent, Thumb thumb) {
680+
final slider = _renderObjectKey.currentContext!.findRenderObject()! as _RenderRangeSlider;
681+
final TextDirection directionality = Directionality.of(_renderObjectKey.currentContext!);
682+
final bool increase = switch (intent.type) {
683+
_SliderAdjustmentType.up => true,
684+
_SliderAdjustmentType.down => false,
685+
_SliderAdjustmentType.left => directionality == TextDirection.rtl,
686+
_SliderAdjustmentType.right => directionality == TextDirection.ltr,
687+
};
688+
switch (thumb) {
689+
case Thumb.start:
690+
increase ? slider.increaseStartAction() : slider.decreaseStartAction();
691+
case Thumb.end:
692+
increase ? slider.increaseEndAction() : slider.decreaseEndAction();
693+
}
694+
}
695+
696+
// Toggles the value adjustment mode used by directional navigation. Entered
697+
// and exited by pressing the enter key on a focused thumb.
698+
void _toggleEditMode() {
699+
setState(() {
700+
_isEditingInDirectionalMode = !_isEditingInDirectionalMode;
701+
});
702+
}
703+
704+
// Exits the value adjustment mode when a thumb loses focus, so the slider
705+
// does not stay in the editing state after the user navigates away.
706+
void _handleFocusChanged(bool hasFocus) {
707+
if (!hasFocus && _isEditingInDirectionalMode) {
708+
setState(() {
709+
_isEditingInDirectionalMode = false;
710+
});
711+
}
712+
}
713+
608714
void _handleDragStart(RangeValues values) {
609715
setState(() {
610716
_dragging = true;
@@ -642,13 +748,16 @@ class _RangeSliderState extends State<RangeSlider> with TickerProviderStateMixin
642748

643749
Widget _buildThumbFocusDetector({
644750
required FocusNode focusNode,
751+
required Map<Type, Action<Intent>> actions,
645752
required ValueChanged<bool> onShowFocusHighlight,
646753
}) {
647754
return FocusableActionDetector(
648755
focusNode: focusNode,
649756
enabled: _enabled,
650757
includeFocusSemantics: false,
758+
actions: actions,
651759
onShowFocusHighlight: onShowFocusHighlight,
760+
onFocusChange: _handleFocusChanged,
652761
child: const SizedBox.shrink(),
653762
);
654763
}
@@ -812,6 +921,7 @@ class _RangeSliderState extends State<RangeSlider> with TickerProviderStateMixin
812921
return _buildValueIndicator(sliderTheme.showValueIndicator!);
813922
},
814923
child: _RangeSliderRenderObjectWidget(
924+
key: _renderObjectKey,
815925
values: _unlerpRangeValues(widget.values),
816926
divisions: widget.divisions,
817927
labels: widget.labels,
@@ -835,20 +945,33 @@ class _RangeSliderState extends State<RangeSlider> with TickerProviderStateMixin
835945
result = Padding(padding: padding, child: result);
836946
}
837947

948+
final Map<ShortcutActivator, Intent> shortcutMap = switch (_navigationMode) {
949+
NavigationMode.directional =>
950+
_isEditingInDirectionalMode
951+
? _directionalNavEditingShortcutMap
952+
: _directionalNavShortcutMap,
953+
NavigationMode.traditional => _traditionalNavShortcutMap,
954+
};
955+
838956
return Stack(
839957
children: <Widget>[
840958
// Adds two invisible focus nodes to the range slider for its two thumbs.
841-
Row(
842-
children: <Widget>[
843-
_buildThumbFocusDetector(
844-
focusNode: startFocusNode,
845-
onShowFocusHighlight: _handleStartFocusHighlightChanged,
846-
),
847-
_buildThumbFocusDetector(
848-
focusNode: endFocusNode,
849-
onShowFocusHighlight: _handleEndFocusHighlightChanged,
850-
),
851-
],
959+
Shortcuts(
960+
shortcuts: shortcutMap,
961+
child: Row(
962+
children: <Widget>[
963+
_buildThumbFocusDetector(
964+
focusNode: startFocusNode,
965+
actions: _startActionMap,
966+
onShowFocusHighlight: _handleStartFocusHighlightChanged,
967+
),
968+
_buildThumbFocusDetector(
969+
focusNode: endFocusNode,
970+
actions: _endActionMap,
971+
onShowFocusHighlight: _handleEndFocusHighlightChanged,
972+
),
973+
],
974+
),
852975
),
853976
MouseRegion(
854977
onEnter: (_) => _handleHoverChanged(true),
@@ -881,6 +1004,7 @@ class _RangeSliderState extends State<RangeSlider> with TickerProviderStateMixin
8811004

8821005
class _RangeSliderRenderObjectWidget extends LeafRenderObjectWidget {
8831006
const _RangeSliderRenderObjectWidget({
1007+
super.key,
8841008
required this.values,
8851009
required this.divisions,
8861010
required this.labels,
@@ -2014,16 +2138,16 @@ class _RenderRangeSlider extends RenderBox with RelayoutWhenSystemFontsChangeMix
20142138
values.start,
20152139
_increasedStartValue,
20162140
_decreasedStartValue,
2017-
_increaseStartAction,
2018-
_decreaseStartAction,
2141+
increaseStartAction,
2142+
decreaseStartAction,
20192143
focused: _state.startFocusNode.hasFocus,
20202144
);
20212145
final SemanticsConfiguration endSemanticsConfiguration = _createSemanticsConfiguration(
20222146
values.end,
20232147
_increasedEndValue,
20242148
_decreasedEndValue,
2025-
_increaseEndAction,
2026-
_decreaseEndAction,
2149+
increaseEndAction,
2150+
decreaseEndAction,
20272151
focused: _state.endFocusNode.hasFocus,
20282152
);
20292153

@@ -2074,25 +2198,25 @@ class _RenderRangeSlider extends RenderBox with RelayoutWhenSystemFontsChangeMix
20742198

20752199
double get _semanticActionUnit => divisions != null ? 1.0 / divisions! : _adjustmentUnit;
20762200

2077-
void _increaseStartAction() {
2201+
void increaseStartAction() {
20782202
if (isEnabled) {
20792203
onChanged!(RangeValues(_increasedStartValue, values.end));
20802204
}
20812205
}
20822206

2083-
void _decreaseStartAction() {
2207+
void decreaseStartAction() {
20842208
if (isEnabled) {
20852209
onChanged!(RangeValues(_decreasedStartValue, values.end));
20862210
}
20872211
}
20882212

2089-
void _increaseEndAction() {
2213+
void increaseEndAction() {
20902214
if (isEnabled) {
20912215
onChanged!(RangeValues(values.start, _increasedEndValue));
20922216
}
20932217
}
20942218

2095-
void _decreaseEndAction() {
2219+
void decreaseEndAction() {
20962220
if (isEnabled) {
20972221
onChanged!(RangeValues(values.start, _decreasedEndValue));
20982222
}
@@ -2263,6 +2387,26 @@ class _RangeSliderDefaultsM2 extends SliderThemeData {
22632387
double? get minThumbSeparation => 8;
22642388
}
22652389

2390+
class _AdjustSliderIntent extends Intent {
2391+
const _AdjustSliderIntent.right() : type = _SliderAdjustmentType.right;
2392+
2393+
const _AdjustSliderIntent.left() : type = _SliderAdjustmentType.left;
2394+
2395+
const _AdjustSliderIntent.up() : type = _SliderAdjustmentType.up;
2396+
2397+
const _AdjustSliderIntent.down() : type = _SliderAdjustmentType.down;
2398+
2399+
final _SliderAdjustmentType type;
2400+
}
2401+
2402+
enum _SliderAdjustmentType { right, left, up, down }
2403+
2404+
// Toggles the value adjustment mode used by directional navigation, so the
2405+
// thumb that has focus enters or exits the editing state.
2406+
class _ToggleRangeSliderEditModeIntent extends Intent {
2407+
const _ToggleRangeSliderEditModeIntent();
2408+
}
2409+
22662410
// BEGIN GENERATED TOKEN PROPERTIES - RangeSlider
22672411

22682412
// Do not edit by hand. The code between the "BEGIN GENERATED" and
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
changelog: |
2+
- Adds an alternative keyboard input method for RangeSlider in NavigationMode.directional.
3+
version: minor

0 commit comments

Comments
 (0)