paint method
- PaintingContext context,
- Offset center, {
- required RenderBox parentBox,
- required SliderThemeData sliderTheme,
- required Animation<
double> enableAnimation, - required TextDirection textDirection,
- required Offset thumbCenter,
- required bool isEnabled,
Paints the slider track.
The context
argument is the same as the one that includes the Slider's
render box.
The center
argument is the offset for where this shape's center should be
painted. This offset is relative to the origin of the context
canvas.
The parentBox
argument is the RenderBox of the Slider. Its attributes,
such as size, can be used to assist in painting this shape.
the sliderTheme
argument is the theme assigned to the Slider that this
shape belongs to.
The enableAnimation
argument is an animation triggered when the Slider
is enabled, and it reverses when the slider is disabled. The Slider is
enabled when Slider.onChanged is not null.Use this to paint intermediate
frames for this shape when the slider changes enabled state.
The isEnabled
argument is false when Slider.onChanged is null and true
otherwise. When true, the slider will respond to input.
The textDirection
argument can be used to determine how the tick marks
are painting depending on whether they are on an active track segment or
not. The track segment between the start of the slider and the thumb is
the active track segment. The track segment between the thumb and the end
of the slider is the inactive track segment. In LTR text direction, the
start of the slider is on the left, and in RTL text direction, the start
of the slider is on the right.
Implementation
@override
void paint(
PaintingContext context,
Offset center, {
required RenderBox parentBox,
required SliderThemeData sliderTheme,
required Animation<double> enableAnimation,
required TextDirection textDirection,
required Offset thumbCenter,
required bool isEnabled,
}) {
assert(sliderTheme.disabledActiveTickMarkColor != null);
assert(sliderTheme.disabledInactiveTickMarkColor != null);
assert(sliderTheme.activeTickMarkColor != null);
assert(sliderTheme.inactiveTickMarkColor != null);
// The paint color of the tick mark depends on its position relative
// to the thumb and the text direction.
final double xOffset = center.dx - thumbCenter.dx;
final (Color? begin, Color? end) = switch (textDirection) {
TextDirection.ltr when xOffset > 0 => (sliderTheme.disabledInactiveTickMarkColor, sliderTheme.inactiveTickMarkColor),
TextDirection.rtl when xOffset < 0 => (sliderTheme.disabledInactiveTickMarkColor, sliderTheme.inactiveTickMarkColor),
TextDirection.ltr || TextDirection.rtl => (sliderTheme.disabledActiveTickMarkColor, sliderTheme.activeTickMarkColor),
};
final Paint paint = Paint()..color = ColorTween(begin: begin, end: end).evaluate(enableAnimation)!;
// The tick marks are tiny circles that are the same height as the track.
final double tickMarkRadius = getPreferredSize(
isEnabled: isEnabled,
sliderTheme: sliderTheme,
).width / 2;
if (tickMarkRadius > 0) {
context.canvas.drawCircle(center, tickMarkRadius, paint);
}
}