Skip to content

Commit 5530c10

Browse files
authored
Optimize the display of the Overlay on the Slider (#139021)
## Description Automatic focus request on Slider value change prevents Overlay from correctly handling hover events. Fixes #139281 ### Before ![slider3](https://github.com/flutter/flutter/assets/38378650/815e12a4-ccaf-4b99-8480-6cbdc97a91bd) ### After ![slider4](https://github.com/flutter/flutter/assets/38378650/473eca22-0308-4964-8368-b0c158ae30eb)
1 parent 3f1f0aa commit 5530c10

2 files changed

Lines changed: 95 additions & 3 deletions

File tree

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,6 @@ class _SliderState extends State<Slider> with TickerProviderStateMixin {
666666
final double lerpValue = _lerp(value);
667667
if (lerpValue != widget.value) {
668668
widget.onChanged!(lerpValue);
669-
_focusNode?.requestFocus();
670669
}
671670
}
672671

@@ -1376,8 +1375,8 @@ class _RenderSlider extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
13761375
if (hovered && hoveringThumb) {
13771376
_state.overlayController.forward();
13781377
} else {
1379-
// Only remove overlay when Slider is unfocused.
1380-
if (!hasFocus) {
1378+
// Only remove overlay when Slider is inactive and unfocused.
1379+
if (!_active && !hasFocus) {
13811380
_state.overlayController.reverse();
13821381
}
13831382
}

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

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3396,6 +3396,99 @@ void main() {
33963396
expect(dragStarted, false);
33973397
});
33983398

3399+
// Regression test for https://github.com/flutter/flutter/issues/139281
3400+
testWidgetsWithLeakTracking('Slider does not request focus when the value is changed', (WidgetTester tester) async {
3401+
double value = 0.5;
3402+
await tester.pumpWidget(MaterialApp(
3403+
home: Material(
3404+
child: Center(
3405+
child: StatefulBuilder(builder: (BuildContext context, StateSetter setState) {
3406+
return Slider(
3407+
value: value,
3408+
onChanged: (double newValue) {
3409+
setState(() {
3410+
value = newValue;
3411+
});
3412+
}
3413+
);
3414+
}),
3415+
),
3416+
),
3417+
));
3418+
// Initially, the slider does not have focus whe enabled and not tapped.
3419+
await tester.pumpAndSettle();
3420+
expect(value, equals(0.5));
3421+
// Get FocusNode from the state of the slider to include auto-generated FocusNode.
3422+
// ignore: avoid_dynamic_calls, invalid_assignment
3423+
final FocusNode focusNode = (tester.firstState(find.byType(Slider)) as dynamic).focusNode;
3424+
// The slider does not have focus.
3425+
expect(focusNode.hasFocus, false);
3426+
final Offset sliderCenter = tester.getCenter(find.byType(Slider));
3427+
final Offset tapLocation = Offset(sliderCenter.dx + 50, sliderCenter.dy);
3428+
// Tap on the slider to change the value.
3429+
final TestGesture gesture = await tester.createGesture();
3430+
await gesture.addPointer();
3431+
await gesture.down(tapLocation);
3432+
await gesture.up();
3433+
await tester.pumpAndSettle();
3434+
expect(value, isNot(equals(0.5)));
3435+
// The slider does not have have focus after the value is changed.
3436+
expect(focusNode.hasFocus, false);
3437+
});
3438+
3439+
// Regression test for https://github.com/flutter/flutter/issues/139281
3440+
testWidgetsWithLeakTracking('Overlay remains when Slider thumb is interacted', (WidgetTester tester) async {
3441+
double value = 0.5;
3442+
const Color overlayColor = Color(0xffff0000);
3443+
await tester.pumpWidget(MaterialApp(
3444+
home: Material(
3445+
child: Center(
3446+
child: StatefulBuilder(builder: (BuildContext context, StateSetter setState) {
3447+
return Slider(
3448+
value: value,
3449+
overlayColor: const MaterialStatePropertyAll<Color?>(overlayColor),
3450+
onChanged: (double newValue) {
3451+
setState(() {
3452+
value = newValue;
3453+
});
3454+
}
3455+
);
3456+
}),
3457+
),
3458+
),
3459+
));
3460+
// Slider does not have overlay when enabled and not tapped.
3461+
await tester.pumpAndSettle();
3462+
expect(
3463+
Material.of(tester.element(find.byType(Slider))),
3464+
isNot(paints..circle(color: overlayColor)),
3465+
);
3466+
final Offset sliderCenter = tester.getCenter(find.byType(Slider));
3467+
// Tap and hold down on the thumb to keep it active.
3468+
final TestGesture gesture = await tester.createGesture();
3469+
await gesture.addPointer();
3470+
await gesture.down(sliderCenter);
3471+
await tester.pumpAndSettle();
3472+
expect(
3473+
Material.of(tester.element(find.byType(Slider))),
3474+
paints..circle(color: overlayColor),
3475+
);
3476+
// Hover on the slider but outside the thumb.
3477+
await gesture.moveTo(tester.getTopLeft(find.byType(Slider)));
3478+
await tester.pumpAndSettle();
3479+
expect(
3480+
Material.of(tester.element(find.byType(Slider))),
3481+
paints..circle(color: overlayColor),
3482+
);
3483+
// Tap up on the slider.
3484+
await gesture.up();
3485+
await tester.pumpAndSettle();
3486+
expect(
3487+
Material.of(tester.element(find.byType(Slider))),
3488+
isNot(paints..circle(color: overlayColor)),
3489+
);
3490+
});
3491+
33993492
testWidgetsWithLeakTracking('Overlay appear only when hovered on the thumb on desktop', (WidgetTester tester) async {
34003493
double value = 0.5;
34013494
const Color overlayColor = Color(0xffff0000);

0 commit comments

Comments
 (0)