Skip to content

Commit e81cfde

Browse files
authored
[cupertino_ui] Dim disabled CupertinoSlider controls (#13419)
Dims `CupertinoSlider` to 50% opacity when `onChanged` is null, matching the disabled appearance in native UIKit Slider component on iOS. Adds light/dark regression tests for disabling and re-enabling the slider. Fixes flutter/flutter#106691. ### Screenshots Flutter before/after: iPhone 16 Pro simulator, iOS 18.6, light mode, value 0.67. The top slider is enabled; the bottom is disabled. The native reference shows stock `UISlider` controls in both themes. | Before | After | Native UIKit reference | | --- | --- | --- | | <img src="https://raw.githubusercontent.com/Wreos/packages/e755e4de3f50f2505fbd7ecf98240159f22d4f6e/before-ios18.6.jpg" width="240" alt="Before: enabled and disabled sliders look identical"> | <img src="https://raw.githubusercontent.com/Wreos/packages/e755e4de3f50f2505fbd7ecf98240159f22d4f6e/after-ios18.6.jpg" width="240" alt="After: disabled slider is dimmed"> | <img src="https://raw.githubusercontent.com/Wreos/packages/e755e4de3f50f2505fbd7ecf98240159f22d4f6e/native-ios18.6.jpg" width="240" alt="Native UIKit: disabled sliders are dimmed in light and dark mode"> | Also checked native UIKit on [iOS 26.5](https://raw.githubusercontent.com/Wreos/packages/e755e4de3f50f2505fbd7ecf98240159f22d4f6e/native-ios26.5.jpg), where the disabled visual element uses 0.5 opacity. This fixes disabled styling, not the slider's overall design. Validation: all 23 tests in `test/slider_test.dart` pass; targeted analysis and formatting pass. The full package suite has not been run locally. No public API changes. ## 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 5365902 commit e81cfde

3 files changed

Lines changed: 83 additions & 15 deletions

File tree

‎packages/cupertino_ui/lib/src/slider.dart‎

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ import 'thumb_painter.dart';
1717

1818
typedef _SliderValueChanged = void Function(double value, bool isFastDrag)?;
1919

20+
// Matches the disabled visual opacity of the native iOS UISlider.
21+
const double _kDisabledOpacity = 0.5;
22+
2023
/// Defines the threshold for determining a "fast" slider drag.
2124
///
2225
/// Measured in slider extent per second.
@@ -292,18 +295,21 @@ class _CupertinoSliderState extends State<CupertinoSlider> with TickerProviderSt
292295

293296
@override
294297
Widget build(BuildContext context) {
295-
return _CupertinoSliderRenderObjectWidget(
296-
value: (widget.value - widget.min) / (widget.max - widget.min),
297-
divisions: widget.divisions,
298-
activeColor: CupertinoDynamicColor.resolve(
299-
widget.activeColor ?? CupertinoTheme.of(context).primaryColor,
300-
context,
298+
return Opacity(
299+
opacity: widget.onChanged == null ? _kDisabledOpacity : 1.0,
300+
child: _CupertinoSliderRenderObjectWidget(
301+
value: (widget.value - widget.min) / (widget.max - widget.min),
302+
divisions: widget.divisions,
303+
activeColor: CupertinoDynamicColor.resolve(
304+
widget.activeColor ?? CupertinoTheme.of(context).primaryColor,
305+
context,
306+
),
307+
thumbColor: widget.thumbColor,
308+
onChanged: widget.onChanged != null ? _handleChanged : null,
309+
onChangeStart: widget.onChangeStart != null ? _handleDragStart : null,
310+
onChangeEnd: widget.onChangeEnd != null ? _handleDragEnd : null,
311+
vsync: this,
301312
),
302-
thumbColor: widget.thumbColor,
303-
onChanged: widget.onChanged != null ? _handleChanged : null,
304-
onChangeStart: widget.onChangeStart != null ? _handleDragStart : null,
305-
onChangeEnd: widget.onChangeEnd != null ? _handleDragEnd : null,
306-
vsync: this,
307313
);
308314
}
309315
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
changelog: |
2+
- Dims `CupertinoSlider` when disabled to match native iOS appearance.
3+
version: patch

‎packages/cupertino_ui/test/slider_test.dart‎

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,65 @@ const CupertinoDynamicColor _kSystemFill = CupertinoDynamicColor(
2222
);
2323

2424
void main() {
25+
final Finder sliderRenderWidget = find.descendant(
26+
of: find.byType(CupertinoSlider),
27+
matching: find.byWidgetPredicate((Widget widget) => widget is LeafRenderObjectWidget),
28+
);
29+
30+
for (final Brightness brightness in Brightness.values) {
31+
testWidgets('Disabled slider dims and restores its appearance in $brightness', (
32+
WidgetTester tester,
33+
) async {
34+
var enabled = true;
35+
var value = 0.5;
36+
late StateSetter updateState;
37+
await tester.pumpWidget(
38+
CupertinoApp(
39+
theme: CupertinoThemeData(brightness: brightness),
40+
home: StatefulBuilder(
41+
builder: (BuildContext context, StateSetter setState) {
42+
updateState = setState;
43+
return Center(
44+
child: CupertinoSlider(
45+
value: value,
46+
activeColor: CupertinoColors.systemPurple,
47+
thumbColor: CupertinoColors.systemOrange,
48+
onChanged: enabled ? (double newValue) => value = newValue : null,
49+
),
50+
);
51+
},
52+
),
53+
),
54+
);
55+
56+
final Finder opacity = find.descendant(
57+
of: find.byType(CupertinoSlider),
58+
matching: find.byType(Opacity),
59+
);
60+
expect(tester.widget<Opacity>(opacity).opacity, 1.0);
61+
final Size enabledSize = tester.getSize(find.byType(CupertinoSlider));
62+
final RenderObject sliderRenderObject = tester.renderObject(sliderRenderWidget);
63+
64+
updateState(() => enabled = false);
65+
await tester.pump();
66+
expect(tester.widget<Opacity>(opacity).opacity, 0.5);
67+
expect(tester.renderObject(sliderRenderWidget), same(sliderRenderObject));
68+
expect(tester.getSize(find.byType(CupertinoSlider)), enabledSize);
69+
final CupertinoSlider slider = tester.widget<CupertinoSlider>(find.byType(CupertinoSlider));
70+
expect(slider.activeColor, CupertinoColors.systemPurple);
71+
expect(slider.thumbColor, CupertinoColors.systemOrange);
72+
await tester.dragFrom(tester.getCenter(find.byType(CupertinoSlider)), const Offset(50, 0));
73+
expect(value, 0.5);
74+
75+
updateState(() => enabled = true);
76+
await tester.pump();
77+
expect(tester.widget<Opacity>(opacity).opacity, 1.0);
78+
expect(tester.renderObject(sliderRenderWidget), same(sliderRenderObject));
79+
await tester.dragFrom(tester.getCenter(find.byType(CupertinoSlider)), const Offset(50, 0));
80+
expect(value, greaterThan(0.5));
81+
});
82+
}
83+
2584
Future<void> dragSlider(WidgetTester tester, Key sliderKey) {
2685
final Offset topLeft = tester.getTopLeft(find.byKey(sliderKey));
2786
const double unit = CupertinoThumbPainter.radius;
@@ -439,7 +498,7 @@ void main() {
439498
);
440499

441500
expect(
442-
tester.getSemantics(find.byType(CupertinoSlider)),
501+
tester.getSemantics(sliderRenderWidget),
443502
matchesSemantics(
444503
isSlider: true,
445504
hasIncreaseAction: true,
@@ -462,7 +521,7 @@ void main() {
462521
),
463522
);
464523

465-
expect(tester.getSemantics(find.byType(CupertinoSlider)), matchesSemantics(isSlider: true));
524+
expect(tester.getSemantics(sliderRenderWidget), matchesSemantics(isSlider: true));
466525

467526
handle.dispose();
468527
});
@@ -480,7 +539,7 @@ void main() {
480539
);
481540

482541
expect(
483-
tester.getSemantics(find.byType(CupertinoSlider)),
542+
tester.getSemantics(sliderRenderWidget),
484543
matchesSemantics(
485544
isSlider: true,
486545
hasIncreaseAction: true,
@@ -503,7 +562,7 @@ void main() {
503562
);
504563

505564
expect(
506-
tester.getSemantics(find.byType(CupertinoSlider)),
565+
tester.getSemantics(sliderRenderWidget),
507566
matchesSemantics(
508567
isSlider: true,
509568
hasIncreaseAction: true,

0 commit comments

Comments
 (0)