Commit 1ea6a38f authored by Luiz França's avatar Luiz França Committed by LongCatIsLooong

Adding thumb color customisation functionality to CupertinoSlider (#42563)

parent b5424eba
...@@ -60,11 +60,13 @@ class CupertinoSlider extends StatefulWidget { ...@@ -60,11 +60,13 @@ class CupertinoSlider extends StatefulWidget {
this.max = 1.0, this.max = 1.0,
this.divisions, this.divisions,
this.activeColor, this.activeColor,
this.thumbColor = CupertinoColors.white,
}) : assert(value != null), }) : assert(value != null),
assert(min != null), assert(min != null),
assert(max != null), assert(max != null),
assert(value >= min && value <= max), assert(value >= min && value <= max),
assert(divisions == null || divisions > 0), assert(divisions == null || divisions > 0),
assert(thumbColor != null),
super(key: key); super(key: key);
/// The currently selected value for this slider. /// The currently selected value for this slider.
...@@ -193,6 +195,13 @@ class CupertinoSlider extends StatefulWidget { ...@@ -193,6 +195,13 @@ class CupertinoSlider extends StatefulWidget {
/// Defaults to the [CupertinoTheme]'s primary color if null. /// Defaults to the [CupertinoTheme]'s primary color if null.
final Color activeColor; final Color activeColor;
/// The color to use for the thumb of the slider.
///
/// Thumb color must not be null.
///
/// Defaults to [CupertinoColors.white].
final Color thumbColor;
@override @override
_CupertinoSliderState createState() => _CupertinoSliderState(); _CupertinoSliderState createState() => _CupertinoSliderState();
...@@ -233,6 +242,7 @@ class _CupertinoSliderState extends State<CupertinoSlider> with TickerProviderSt ...@@ -233,6 +242,7 @@ class _CupertinoSliderState extends State<CupertinoSlider> with TickerProviderSt
widget.activeColor ?? CupertinoTheme.of(context).primaryColor, widget.activeColor ?? CupertinoTheme.of(context).primaryColor,
context, context,
), ),
thumbColor: widget.thumbColor,
onChanged: widget.onChanged != null ? _handleChanged : null, onChanged: widget.onChanged != null ? _handleChanged : null,
onChangeStart: widget.onChangeStart != null ? _handleDragStart : null, onChangeStart: widget.onChangeStart != null ? _handleDragStart : null,
onChangeEnd: widget.onChangeEnd != null ? _handleDragEnd : null, onChangeEnd: widget.onChangeEnd != null ? _handleDragEnd : null,
...@@ -247,6 +257,7 @@ class _CupertinoSliderRenderObjectWidget extends LeafRenderObjectWidget { ...@@ -247,6 +257,7 @@ class _CupertinoSliderRenderObjectWidget extends LeafRenderObjectWidget {
this.value, this.value,
this.divisions, this.divisions,
this.activeColor, this.activeColor,
this.thumbColor,
this.onChanged, this.onChanged,
this.onChangeStart, this.onChangeStart,
this.onChangeEnd, this.onChangeEnd,
...@@ -256,18 +267,19 @@ class _CupertinoSliderRenderObjectWidget extends LeafRenderObjectWidget { ...@@ -256,18 +267,19 @@ class _CupertinoSliderRenderObjectWidget extends LeafRenderObjectWidget {
final double value; final double value;
final int divisions; final int divisions;
final Color activeColor; final Color activeColor;
final Color thumbColor;
final ValueChanged<double> onChanged; final ValueChanged<double> onChanged;
final ValueChanged<double> onChangeStart; final ValueChanged<double> onChangeStart;
final ValueChanged<double> onChangeEnd; final ValueChanged<double> onChangeEnd;
final TickerProvider vsync; final TickerProvider vsync;
@override @override
_RenderCupertinoSlider createRenderObject(BuildContext context) { _RenderCupertinoSlider createRenderObject(BuildContext context) {
return _RenderCupertinoSlider( return _RenderCupertinoSlider(
value: value, value: value,
divisions: divisions, divisions: divisions,
activeColor: activeColor, activeColor: activeColor,
thumbColor: CupertinoDynamicColor.resolve(thumbColor, context),
trackColor: CupertinoDynamicColor.resolve(CupertinoColors.systemFill, context), trackColor: CupertinoDynamicColor.resolve(CupertinoColors.systemFill, context),
onChanged: onChanged, onChanged: onChanged,
onChangeStart: onChangeStart, onChangeStart: onChangeStart,
...@@ -283,6 +295,7 @@ class _CupertinoSliderRenderObjectWidget extends LeafRenderObjectWidget { ...@@ -283,6 +295,7 @@ class _CupertinoSliderRenderObjectWidget extends LeafRenderObjectWidget {
..value = value ..value = value
..divisions = divisions ..divisions = divisions
..activeColor = activeColor ..activeColor = activeColor
..thumbColor = CupertinoDynamicColor.resolve(thumbColor, context)
..trackColor = CupertinoDynamicColor.resolve(CupertinoColors.systemFill, context) ..trackColor = CupertinoDynamicColor.resolve(CupertinoColors.systemFill, context)
..onChanged = onChanged ..onChanged = onChanged
..onChangeStart = onChangeStart ..onChangeStart = onChangeStart
...@@ -305,6 +318,7 @@ class _RenderCupertinoSlider extends RenderConstrainedBox { ...@@ -305,6 +318,7 @@ class _RenderCupertinoSlider extends RenderConstrainedBox {
@required double value, @required double value,
int divisions, int divisions,
Color activeColor, Color activeColor,
Color thumbColor,
Color trackColor, Color trackColor,
ValueChanged<double> onChanged, ValueChanged<double> onChanged,
this.onChangeStart, this.onChangeStart,
...@@ -316,6 +330,7 @@ class _RenderCupertinoSlider extends RenderConstrainedBox { ...@@ -316,6 +330,7 @@ class _RenderCupertinoSlider extends RenderConstrainedBox {
_value = value, _value = value,
_divisions = divisions, _divisions = divisions,
_activeColor = activeColor, _activeColor = activeColor,
_thumbColor = thumbColor,
_trackColor = trackColor, _trackColor = trackColor,
_onChanged = onChanged, _onChanged = onChanged,
_textDirection = textDirection, _textDirection = textDirection,
...@@ -363,6 +378,15 @@ class _RenderCupertinoSlider extends RenderConstrainedBox { ...@@ -363,6 +378,15 @@ class _RenderCupertinoSlider extends RenderConstrainedBox {
markNeedsPaint(); markNeedsPaint();
} }
Color get thumbColor => _thumbColor;
Color _thumbColor;
set thumbColor(Color value) {
if (value == _thumbColor)
return;
_thumbColor = value;
markNeedsPaint();
}
Color get trackColor => _trackColor; Color get trackColor => _trackColor;
Color _trackColor; Color _trackColor;
set trackColor(Color value) { set trackColor(Color value) {
...@@ -512,7 +536,7 @@ class _RenderCupertinoSlider extends RenderConstrainedBox { ...@@ -512,7 +536,7 @@ class _RenderCupertinoSlider extends RenderConstrainedBox {
} }
final Offset thumbCenter = Offset(trackActive, trackCenter); final Offset thumbCenter = Offset(trackActive, trackCenter);
const CupertinoThumbPainter().paint(canvas, Rect.fromCircle(center: thumbCenter, radius: CupertinoThumbPainter.radius)); CupertinoThumbPainter(color: thumbColor).paint(canvas, Rect.fromCircle(center: thumbCenter, radius: CupertinoThumbPainter.radius));
} }
@override @override
......
...@@ -556,4 +556,52 @@ void main() { ...@@ -556,4 +556,52 @@ void main() {
isNot(paints..rrect(color: _kSystemFill.color)), isNot(paints..rrect(color: _kSystemFill.color)),
); );
}); });
testWidgets('Thumb color can be overridden', (WidgetTester tester) async {
await tester.pumpWidget(
CupertinoApp(
home: Center(
child: CupertinoSlider(
thumbColor: CupertinoColors.systemPurple,
onChanged: (double value) { },
value: 0,
),
),
),
);
expect(
find.byType(CupertinoSlider),
paints
..rrect()
..rrect()
..rrect()
..rrect()
..rrect()
..rrect(color: CupertinoColors.systemPurple.color)
);
await tester.pumpWidget(
CupertinoApp(
home: Center(
child: CupertinoSlider(
thumbColor: CupertinoColors.activeOrange,
onChanged: (double value) { },
value: 0,
),
),
),
);
expect(
find.byType(CupertinoSlider),
paints
..rrect()
..rrect()
..rrect()
..rrect()
..rrect()
..rrect(color: CupertinoColors.activeOrange.color)
);
});
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment