Skip to content

Commit 5451ea6

Browse files
authored
Slider.onChangeStart & Slider.onChangeEnd are not called on keyboard shortcuts (#126896)
fixes flutter/flutter#123315 -------- This PR makes changes to the _actionHandler function used on the Slider.Dart Widget for Key Events. It ensures onChangeStart is called at the beginning of a Key Event and onChangeEnd at the end of one. This PR includes a test for the changes made. I ran all existing tests after my changes were made and they passed.
1 parent 982e005 commit 5451ea6

2 files changed

Lines changed: 129 additions & 30 deletions

File tree

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,7 @@ class _SliderState extends State<Slider> with TickerProviderStateMixin {
684684
void _actionHandler(_AdjustSliderIntent intent) {
685685
final _RenderSlider renderSlider = _renderObjectKey.currentContext!.findRenderObject()! as _RenderSlider;
686686
final TextDirection textDirection = Directionality.of(_renderObjectKey.currentContext!);
687+
687688
switch (intent.type) {
688689
case _SliderAdjustmentType.right:
689690
switch (textDirection) {
@@ -1802,15 +1803,33 @@ class _RenderSlider extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
18021803

18031804
void increaseAction() {
18041805
if (isInteractive) {
1805-
onChanged!(clampDouble(value + _semanticActionUnit, 0.0, 1.0));
1806+
onChangeStart!(currentValue);
1807+
final double increase = increaseValue();
1808+
onChanged!(increase);
1809+
onChangeEnd!(increase);
18061810
}
18071811
}
18081812

18091813
void decreaseAction() {
18101814
if (isInteractive) {
1811-
onChanged!(clampDouble(value - _semanticActionUnit, 0.0, 1.0));
1815+
onChangeStart!(currentValue);
1816+
final double decrease = decreaseValue();
1817+
onChanged!(decrease);
1818+
onChangeEnd!(decrease);
18121819
}
18131820
}
1821+
1822+
double get currentValue {
1823+
return clampDouble(value, 0.0, 1.0);
1824+
}
1825+
1826+
double increaseValue() {
1827+
return clampDouble(value + _semanticActionUnit, 0.0, 1.0);
1828+
}
1829+
1830+
double decreaseValue() {
1831+
return clampDouble(value - _semanticActionUnit, 0.0, 1.0);
1832+
}
18141833
}
18151834

18161835
class _AdjustSliderIntent extends Intent {

‎packages/flutter/test/material/slider_test.dart‎

Lines changed: 108 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2125,17 +2125,29 @@ void main() {
21252125

21262126
testWidgets('Slider can be incremented and decremented by keyboard shortcuts - LTR', (WidgetTester tester) async {
21272127
tester.binding.focusManager.highlightStrategy = FocusHighlightStrategy.alwaysTraditional;
2128-
double value = 0.5;
2128+
double startValue = 0.0;
2129+
double currentValue = 0.5;
2130+
double endValue = 0.0;
21292131
await tester.pumpWidget(
21302132
MaterialApp(
21312133
home: Material(
21322134
child: Center(
21332135
child: StatefulBuilder(builder: (BuildContext context, StateSetter setState) {
21342136
return Slider(
2135-
value: value,
2137+
value: currentValue,
2138+
onChangeStart: (double newValue) {
2139+
setState(() {
2140+
startValue = newValue;
2141+
});
2142+
},
21362143
onChanged: (double newValue) {
21372144
setState(() {
2138-
value = newValue;
2145+
currentValue = newValue;
2146+
});
2147+
},
2148+
onChangeEnd: (double newValue) {
2149+
setState(() {
2150+
endValue = newValue;
21392151
});
21402152
},
21412153
autofocus: true,
@@ -2149,34 +2161,54 @@ void main() {
21492161

21502162
await tester.sendKeyEvent(LogicalKeyboardKey.arrowRight);
21512163
await tester.pumpAndSettle();
2152-
expect(value, 0.55);
2164+
expect(startValue, 0.5);
2165+
expect(currentValue, 0.55);
2166+
expect(endValue, 0.55);
21532167

21542168
await tester.sendKeyEvent(LogicalKeyboardKey.arrowLeft);
21552169
await tester.pumpAndSettle();
2156-
expect(value, 0.5);
2170+
expect(startValue, 0.55);
2171+
expect(currentValue, 0.5);
2172+
expect(endValue, 0.5);
21572173

21582174
await tester.sendKeyEvent(LogicalKeyboardKey.arrowUp);
21592175
await tester.pumpAndSettle();
2160-
expect(value, 0.55);
2176+
expect(startValue, 0.5);
2177+
expect(currentValue, 0.55);
2178+
expect(endValue, 0.55);
21612179

21622180
await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown);
21632181
await tester.pumpAndSettle();
2164-
expect(value, 0.5);
2182+
expect(startValue, 0.55);
2183+
expect(currentValue, 0.5);
2184+
expect(endValue, 0.5);
21652185
}, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.android, TargetPlatform.fuchsia, TargetPlatform.linux, TargetPlatform.windows }));
21662186

21672187
testWidgets('Slider can be incremented and decremented by keyboard shortcuts - LTR', (WidgetTester tester) async {
21682188
tester.binding.focusManager.highlightStrategy = FocusHighlightStrategy.alwaysTraditional;
2169-
double value = 0.5;
2189+
double startValue = 0.0;
2190+
double currentValue = 0.5;
2191+
double endValue = 0.0;
21702192
await tester.pumpWidget(
21712193
MaterialApp(
21722194
home: Material(
21732195
child: Center(
21742196
child: StatefulBuilder(builder: (BuildContext context, StateSetter setState) {
21752197
return Slider(
2176-
value: value,
2198+
value: currentValue,
2199+
onChangeStart: (double newValue) {
2200+
setState(() {
2201+
startValue = newValue;
2202+
});
2203+
},
21772204
onChanged: (double newValue) {
21782205
setState(() {
2179-
value = newValue;
2206+
currentValue = newValue;
2207+
});
2208+
},
2209+
onChangeEnd: (double newValue) {
2210+
setState(() {
2211+
endValue = newValue;
21802212
});
21812213
},
21822214
autofocus: true,
@@ -2190,24 +2222,34 @@ void main() {
21902222

21912223
await tester.sendKeyEvent(LogicalKeyboardKey.arrowRight);
21922224
await tester.pumpAndSettle();
2193-
expect(value, 0.6);
2225+
expect(startValue, 0.5);
2226+
expect(currentValue, 0.6);
2227+
expect(endValue, 0.6);
21942228

21952229
await tester.sendKeyEvent(LogicalKeyboardKey.arrowLeft);
21962230
await tester.pumpAndSettle();
2197-
expect(value, 0.5);
2231+
expect(startValue, 0.6);
2232+
expect(currentValue, 0.5);
2233+
expect(endValue, 0.5);
21982234

21992235
await tester.sendKeyEvent(LogicalKeyboardKey.arrowUp);
22002236
await tester.pumpAndSettle();
2201-
expect(value, 0.6);
2237+
expect(startValue, 0.5);
2238+
expect(currentValue, 0.6);
2239+
expect(endValue, 0.6);
22022240

22032241
await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown);
22042242
await tester.pumpAndSettle();
2205-
expect(value, 0.5);
2243+
expect(startValue, 0.6);
2244+
expect(currentValue, 0.5);
2245+
expect(endValue, 0.5);
22062246
}, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.iOS, TargetPlatform.macOS }));
22072247

22082248
testWidgets('Slider can be incremented and decremented by keyboard shortcuts - RTL', (WidgetTester tester) async {
22092249
tester.binding.focusManager.highlightStrategy = FocusHighlightStrategy.alwaysTraditional;
2210-
double value = 0.5;
2250+
double startValue = 0.0;
2251+
double currentValue = 0.5;
2252+
double endValue = 0.0;
22112253
await tester.pumpWidget(
22122254
MaterialApp(
22132255
home: Material(
@@ -2216,10 +2258,20 @@ void main() {
22162258
return Directionality(
22172259
textDirection: TextDirection.rtl,
22182260
child: Slider(
2219-
value: value,
2261+
value: currentValue,
2262+
onChangeStart: (double newValue) {
2263+
setState(() {
2264+
startValue = newValue;
2265+
});
2266+
},
22202267
onChanged: (double newValue) {
22212268
setState(() {
2222-
value = newValue;
2269+
currentValue = newValue;
2270+
});
2271+
},
2272+
onChangeEnd: (double newValue) {
2273+
setState(() {
2274+
endValue = newValue;
22232275
});
22242276
},
22252277
autofocus: true,
@@ -2234,24 +2286,34 @@ void main() {
22342286

22352287
await tester.sendKeyEvent(LogicalKeyboardKey.arrowRight);
22362288
await tester.pumpAndSettle();
2237-
expect(value, 0.45);
2289+
expect(startValue, 0.5);
2290+
expect(currentValue, 0.45);
2291+
expect(endValue, 0.45);
22382292

22392293
await tester.sendKeyEvent(LogicalKeyboardKey.arrowLeft);
22402294
await tester.pumpAndSettle();
2241-
expect(value, 0.5);
2295+
expect(startValue, 0.45);
2296+
expect(currentValue, 0.5);
2297+
expect(endValue, 0.5);
22422298

22432299
await tester.sendKeyEvent(LogicalKeyboardKey.arrowUp);
22442300
await tester.pumpAndSettle();
2245-
expect(value, 0.55);
2301+
expect(startValue, 0.5);
2302+
expect(currentValue, 0.55);
2303+
expect(endValue, 0.55);
22462304

22472305
await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown);
22482306
await tester.pumpAndSettle();
2249-
expect(value, 0.5);
2307+
expect(startValue, 0.55);
2308+
expect(currentValue, 0.5);
2309+
expect(endValue, 0.5);
22502310
}, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.android, TargetPlatform.fuchsia, TargetPlatform.linux, TargetPlatform.windows }));
22512311

22522312
testWidgets('Slider can be incremented and decremented by keyboard shortcuts - RTL', (WidgetTester tester) async {
22532313
tester.binding.focusManager.highlightStrategy = FocusHighlightStrategy.alwaysTraditional;
2254-
double value = 0.5;
2314+
double startValue = 0.0;
2315+
double currentValue = 0.5;
2316+
double endValue = 0.0;
22552317
await tester.pumpWidget(
22562318
MaterialApp(
22572319
home: Material(
@@ -2260,10 +2322,20 @@ void main() {
22602322
return Directionality(
22612323
textDirection: TextDirection.rtl,
22622324
child: Slider(
2263-
value: value,
2325+
value: currentValue,
2326+
onChangeStart: (double newValue) {
2327+
setState(() {
2328+
startValue = newValue;
2329+
});
2330+
},
22642331
onChanged: (double newValue) {
22652332
setState(() {
2266-
value = newValue;
2333+
currentValue = newValue;
2334+
});
2335+
},
2336+
onChangeEnd: (double newValue) {
2337+
setState(() {
2338+
endValue = newValue;
22672339
});
22682340
},
22692341
autofocus: true,
@@ -2278,19 +2350,27 @@ void main() {
22782350

22792351
await tester.sendKeyEvent(LogicalKeyboardKey.arrowRight);
22802352
await tester.pumpAndSettle();
2281-
expect(value, 0.4);
2353+
expect(startValue, 0.5);
2354+
expect(currentValue, 0.4);
2355+
expect(endValue, 0.4);
22822356

22832357
await tester.sendKeyEvent(LogicalKeyboardKey.arrowLeft);
22842358
await tester.pumpAndSettle();
2285-
expect(value, 0.5);
2359+
expect(startValue, 0.4);
2360+
expect(currentValue, 0.5);
2361+
expect(endValue, 0.5);
22862362

22872363
await tester.sendKeyEvent(LogicalKeyboardKey.arrowUp);
22882364
await tester.pumpAndSettle();
2289-
expect(value, 0.6);
2365+
expect(startValue, 0.5);
2366+
expect(currentValue, 0.6);
2367+
expect(endValue, 0.6);
22902368

22912369
await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown);
22922370
await tester.pumpAndSettle();
2293-
expect(value, 0.5);
2371+
expect(startValue, 0.6);
2372+
expect(currentValue, 0.5);
2373+
expect(endValue, 0.5);
22942374
}, variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.iOS, TargetPlatform.macOS }));
22952375

22962376
testWidgets('In directional nav, Slider can be navigated out of by using up and down arrows', (WidgetTester tester) async {

0 commit comments

Comments
 (0)