diff --git a/packages/flutter/lib/src/material/slider.dart b/packages/flutter/lib/src/material/slider.dart index 653e9e4c42..50ddee5f8e 100644 --- a/packages/flutter/lib/src/material/slider.dart +++ b/packages/flutter/lib/src/material/slider.dart @@ -153,6 +153,7 @@ class Slider extends StatefulWidget { this.label, this.activeColor, this.inactiveColor, + this.thumbColor, this.mouseCursor, this.semanticFormatterCallback, this.focusNode, @@ -190,6 +191,7 @@ class Slider extends StatefulWidget { this.mouseCursor, this.activeColor, this.inactiveColor, + this.thumbColor, this.semanticFormatterCallback, this.focusNode, this.autofocus = false, @@ -382,6 +384,14 @@ class Slider extends StatefulWidget { /// Ignored if this slider is created with [Slider.adaptive]. final Color? inactiveColor; + /// The color of the thumb. + /// + /// If this color is null: + /// * [Slider] will use [activeColor]. + /// * [CupertinoSlider] will have a white thumb + /// (like the native default iOS slider). + final Color? thumbColor; + /// The cursor for a mouse pointer when it enters or is hovering over the /// widget. /// @@ -679,7 +689,7 @@ class _SliderState extends State with TickerProviderStateMixin { inactiveTickMarkColor: widget.activeColor ?? sliderTheme.inactiveTickMarkColor ?? theme.colorScheme.primary.withOpacity(0.54), disabledActiveTickMarkColor: sliderTheme.disabledActiveTickMarkColor ?? theme.colorScheme.onPrimary.withOpacity(0.12), disabledInactiveTickMarkColor: sliderTheme.disabledInactiveTickMarkColor ?? theme.colorScheme.onSurface.withOpacity(0.12), - thumbColor: widget.activeColor ?? sliderTheme.thumbColor ?? theme.colorScheme.primary, + thumbColor: widget.thumbColor ?? widget.activeColor ?? sliderTheme.thumbColor ?? theme.colorScheme.primary, disabledThumbColor: sliderTheme.disabledThumbColor ?? Color.alphaBlend(theme.colorScheme.onSurface.withOpacity(.38), theme.colorScheme.surface), overlayColor: widget.activeColor?.withOpacity(0.12) ?? sliderTheme.overlayColor ?? theme.colorScheme.primary.withOpacity(0.12), valueIndicatorColor: valueIndicatorColor, @@ -757,6 +767,7 @@ class _SliderState extends State with TickerProviderStateMixin { max: widget.max, divisions: widget.divisions, activeColor: widget.activeColor, + thumbColor: widget.thumbColor ?? CupertinoColors.white, ), ); } diff --git a/packages/flutter/test/material/slider_test.dart b/packages/flutter/test/material/slider_test.dart index 03ada68d0e..26ef3d60ee 100644 --- a/packages/flutter/test/material/slider_test.dart +++ b/packages/flutter/test/material/slider_test.dart @@ -2513,4 +2513,105 @@ void main() { // 24.0 is the default margin, (800.0 - 24.0 - 24.0) is the slider's width. expect(nearEqual(activeTrackRRect.right, (800.0 - 24.0 - 24.0) * (5 / 15) + 24.0, 0.01), true); }); + + + testWidgets('Slider paints thumbColor', (WidgetTester tester) async { + const Color color = Color(0xffffc107); + + final Widget sliderAdaptive = MaterialApp( + theme: ThemeData(platform: TargetPlatform.iOS), + home: Material( + child: Slider( + value: 0, + onChanged: (double newValue) {}, + thumbColor: color, + ), + ), + ); + + await tester.pumpWidget(sliderAdaptive); + await tester.pumpAndSettle(); + + final MaterialInkController material = + Material.of(tester.element(find.byType(Slider)))!; + expect(material, paints..circle(color: color)); + }); + + testWidgets('Slider.adaptive paints thumbColor on Android', + (WidgetTester tester) async { + const Color color = Color(0xffffc107); + + final Widget sliderAdaptive = MaterialApp( + theme: ThemeData(platform: TargetPlatform.android), + home: Material( + child: Slider.adaptive( + value: 0, + onChanged: (double newValue) {}, + thumbColor: color, + ), + ), + ); + + await tester.pumpWidget(sliderAdaptive); + await tester.pumpAndSettle(); + + final MaterialInkController material = + Material.of(tester.element(find.byType(Slider)))!; + expect(material, paints..circle(color: color)); + }); + + testWidgets('If thumbColor is null, it defaults to CupertinoColors.white', + (WidgetTester tester) async { + final Widget sliderAdaptive = MaterialApp( + theme: ThemeData(platform: TargetPlatform.iOS), + home: Material( + child: Slider.adaptive( + value: 0, + onChanged: (double newValue) {}, + ), + ), + ); + + await tester.pumpWidget(sliderAdaptive); + await tester.pumpAndSettle(); + + final MaterialInkController material = + Material.of(tester.element(find.byType(CupertinoSlider)))!; + expect( + material, + paints + ..rrect() + ..rrect() + ..rrect() + ..rrect() + ..rrect() + ..rrect(color: CupertinoColors.white), + ); + }); + + testWidgets('Slider.adaptive passes thumbColor to CupertinoSlider', + (WidgetTester tester) async { + const Color color = Color(0xffffc107); + + final Widget sliderAdaptive = MaterialApp( + theme: ThemeData(platform: TargetPlatform.iOS), + home: Material( + child: Slider.adaptive( + value: 0, + onChanged: (double newValue) {}, + thumbColor: color, + ), + ), + ); + + await tester.pumpWidget(sliderAdaptive); + await tester.pumpAndSettle(); + + final MaterialInkController material = + Material.of(tester.element(find.byType(CupertinoSlider)))!; + expect( + material, + paints..rrect()..rrect()..rrect()..rrect()..rrect()..rrect(color: color), + ); + }); }