Skip to content

Commit 93b55ed

Browse files
authored
Slider does not show changed label value for keyboard users fix (#152886)
Fix issue to where keyboard users could not see visual indicator label of changed value in Slider Before: [Screen recording 2024-08-05 12.16.24 PM.webm](https://github.com/user-attachments/assets/89b99423-7856-4b25-86de-b211b2dbe178) After: [Screen recording 2024-08-05 12.38.20 PM.webm](https://github.com/user-attachments/assets/641f9065-8279-4b79-89b1-b5bcd3d691a8) Fixes flutter/flutter#152884 Fixes b/340638215
1 parent ad268e2 commit 93b55ed

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1818,6 +1818,10 @@ class _RenderSlider extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
18181818
final double increase = increaseValue();
18191819
onChanged!(increase);
18201820
onChangeEnd!(increase);
1821+
if (!_state.mounted) {
1822+
return;
1823+
}
1824+
_state.showValueIndicator();
18211825
}
18221826
}
18231827

@@ -1827,6 +1831,10 @@ class _RenderSlider extends RenderBox with RelayoutWhenSystemFontsChangeMixin {
18271831
final double decrease = decreaseValue();
18281832
onChanged!(decrease);
18291833
onChangeEnd!(decrease);
1834+
if (!_state.mounted) {
1835+
return;
1836+
}
1837+
_state.showValueIndicator();
18301838
}
18311839
}
18321840

packages/flutter/test/material/slider_test.dart

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4443,4 +4443,109 @@ void main() {
44434443
await tester.pumpAndSettle();
44444444
expect(valueIndicatorBox, paintsExactlyCountTimes(#drawPath, 1));
44454445
});
4446+
4447+
testWidgets('Slider value indicator is shown when using arrow keys', (WidgetTester tester) async {
4448+
tester.binding.focusManager.highlightStrategy = FocusHighlightStrategy.alwaysTraditional;
4449+
final ThemeData theme = ThemeData();
4450+
double startValue = 0.0;
4451+
double currentValue = 0.5;
4452+
double endValue = 0.0;
4453+
4454+
await tester.pumpWidget(
4455+
MaterialApp(
4456+
theme: theme,
4457+
home: Material(
4458+
child: Center(
4459+
child: StatefulBuilder(builder: (BuildContext context, StateSetter setState) {
4460+
return Slider(
4461+
value: currentValue,
4462+
divisions: 5,
4463+
label: currentValue.toStringAsFixed(1),
4464+
onChangeStart: (double newValue) {
4465+
setState(() {
4466+
startValue = newValue;
4467+
});
4468+
},
4469+
onChanged: (double newValue) {
4470+
setState(() {
4471+
currentValue = newValue;
4472+
});
4473+
},
4474+
onChangeEnd: (double newValue) {
4475+
setState(() {
4476+
endValue = newValue;
4477+
});
4478+
},
4479+
autofocus: true,
4480+
);
4481+
}),
4482+
),
4483+
),
4484+
),
4485+
);
4486+
4487+
// Slider does not show value indicator initially.
4488+
await tester.pumpAndSettle();
4489+
RenderBox valueIndicatorBox = tester.renderObject(find.byType(Overlay));
4490+
expect(
4491+
valueIndicatorBox,
4492+
isNot(paints..scale()..path(color: theme.colorScheme.primary)),
4493+
);
4494+
4495+
// Right arrow (increase)
4496+
await tester.sendKeyEvent(LogicalKeyboardKey.arrowRight);
4497+
await tester.pumpAndSettle();
4498+
expect(startValue, 0.6);
4499+
expect(currentValue.toStringAsFixed(1), '0.8');
4500+
expect(endValue.toStringAsFixed(1), '0.8');
4501+
4502+
// Value indicator is visible.
4503+
valueIndicatorBox = tester.renderObject(find.byType(Overlay));
4504+
expect(
4505+
valueIndicatorBox,
4506+
paints..scale()..path(color: theme.colorScheme.primary),
4507+
);
4508+
4509+
// Left arrow (decrease)
4510+
await tester.sendKeyEvent(LogicalKeyboardKey.arrowLeft);
4511+
await tester.pumpAndSettle();
4512+
expect(startValue, 0.8);
4513+
expect(currentValue.toStringAsFixed(1), '0.6');
4514+
expect(endValue.toStringAsFixed(1), '0.6');
4515+
4516+
// Value indicator is still visible.
4517+
valueIndicatorBox = tester.renderObject(find.byType(Overlay));
4518+
expect(
4519+
valueIndicatorBox,
4520+
paints..scale()..path(color: theme.colorScheme.primary),
4521+
);
4522+
4523+
// Up arrow (increase)
4524+
await tester.sendKeyEvent(LogicalKeyboardKey.arrowUp);
4525+
await tester.pumpAndSettle();
4526+
expect(startValue, 0.6);
4527+
expect(currentValue.toStringAsFixed(1), '0.8');
4528+
expect(endValue.toStringAsFixed(1), '0.8');
4529+
4530+
// Value indicator is still visible.
4531+
valueIndicatorBox = tester.renderObject(find.byType(Overlay));
4532+
expect(
4533+
valueIndicatorBox,
4534+
paints..scale()..path(color: theme.colorScheme.primary),
4535+
);
4536+
4537+
// Down arrow (decrease)
4538+
await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown);
4539+
await tester.pumpAndSettle();
4540+
expect(startValue, 0.8);
4541+
expect(currentValue.toStringAsFixed(1), '0.6');
4542+
expect(endValue.toStringAsFixed(1), '0.6');
4543+
4544+
// Value indicator is still visible.
4545+
valueIndicatorBox = tester.renderObject(find.byType(Overlay));
4546+
expect(
4547+
valueIndicatorBox,
4548+
paints..scale()..path(color: theme.colorScheme.primary),
4549+
);
4550+
}, variant: TargetPlatformVariant.desktop());
44464551
}

0 commit comments

Comments
 (0)