paint method

  1. @override
void paint(
  1. PaintingContext context,
  2. Offset offset, {
  3. required RenderBox parentBox,
  4. required SliderThemeData sliderTheme,
  5. required Animation<double> enableAnimation,
  6. required TextDirection textDirection,
  7. required Offset thumbCenter,
  8. Offset? secondaryOffset,
  9. bool isDiscrete = false,
  10. bool isEnabled = false,
  11. double additionalActiveTrackHeight = 2,
})
override

Paints the track shape based on the state passed to it.

The context argument is the same as the one that includes the Slider's render box.

The offset argument the offset of the origin of the parentBox to the origin of its context canvas. This shape must be painted relative to this offset. See PaintingContextCallback.

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 thumbCenter argument is the offset of the center of the thumb relative to the origin of the PaintingContext.canvas. It can be used as the point that divides the track into 2 segments.

The secondaryOffset argument is the offset of the secondary value relative to the origin of the PaintingContext.canvas.

If not null, the track is divided into 3 segments.

The isEnabled argument is false when Slider.onChanged is null and true otherwise. When true, the slider will respond to input.

The isDiscrete argument is true if Slider.divisions is non-null. When true, the slider will render tick marks on top of the track.

The textDirection argument can be used to determine how the track segments are painted depending on whether they are active 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 TextDirection.ltr, the start of the slider is on the left, and in TextDirection.rtl, the start of the slider is on the right.

Implementation

@override
void paint(
  PaintingContext context,
  Offset offset, {
  required RenderBox parentBox,
  required SliderThemeData sliderTheme,
  required Animation<double> enableAnimation,
  required TextDirection textDirection,
  required Offset thumbCenter,
  Offset? secondaryOffset,
  bool isDiscrete = false,
  bool isEnabled = false,
  double additionalActiveTrackHeight = 2,
}) {
  assert(sliderTheme.disabledActiveTrackColor != null);
  assert(sliderTheme.disabledInactiveTrackColor != null);
  assert(sliderTheme.activeTrackColor != null);
  assert(sliderTheme.inactiveTrackColor != null);
  assert(sliderTheme.thumbShape != null);
  // If the slider [SliderThemeData.trackHeight] is less than or equal to 0,
  // then it makes no difference whether the track is painted or not,
  // therefore the painting can be a no-op.
  if (sliderTheme.trackHeight == null || sliderTheme.trackHeight! <= 0) {
    return;
  }
  assert(sliderTheme is TDSliderThemeData);

  sliderTheme as TDSliderThemeData;
  // Assign the track segment paints, which are leading: active and
  // trailing: inactive.
  final activeTrackColorTween = ColorTween(
      begin: sliderTheme.disabledActiveTrackColor,
      end: sliderTheme.activeTrackColor);
  final inactiveTrackColorTween = ColorTween(
      begin: sliderTheme.disabledInactiveTrackColor,
      end: sliderTheme.inactiveTrackColor);
  final activePaint = Paint()
    ..color = activeTrackColorTween.evaluate(enableAnimation)!;
  final inactivePaint = Paint()
    ..color = inactiveTrackColorTween.evaluate(enableAnimation)!;
  final Paint leftTrackPaint;
  final Paint rightTrackPaint;
  switch (textDirection) {
    case TextDirection.ltr:
      leftTrackPaint = activePaint;
      rightTrackPaint = inactivePaint;
      break;
    case TextDirection.rtl:
      leftTrackPaint = inactivePaint;
      rightTrackPaint = activePaint;
      break;
  }

  final trackRect = getPreferredRect(
    parentBox: parentBox,
    offset: offset,
    sliderTheme: sliderTheme,
    isEnabled: isEnabled,
    isDiscrete: isDiscrete,
  );
  final trackRadius = Radius.circular(trackRect.height / 2);
  final activeTrackRadius =
      Radius.circular((trackRect.height + additionalActiveTrackHeight) / 2);

  context.canvas.drawRRect(
    RRect.fromLTRBAndCorners(
      trackRect.left,
      (textDirection == TextDirection.rtl)
          ? trackRect.top - (additionalActiveTrackHeight / 2)
          : trackRect.top,
      thumbCenter.dx,
      (textDirection == TextDirection.rtl)
          ? trackRect.bottom + (additionalActiveTrackHeight / 2)
          : trackRect.bottom,
      topLeft: (textDirection == TextDirection.ltr)
          ? activeTrackRadius
          : trackRadius,
      bottomLeft: (textDirection == TextDirection.ltr)
          ? activeTrackRadius
          : trackRadius,
    ),
    leftTrackPaint,
  );
  context.canvas.drawRRect(
    RRect.fromLTRBAndCorners(
      thumbCenter.dx,
      (textDirection == TextDirection.rtl)
          ? trackRect.top - (additionalActiveTrackHeight / 2)
          : trackRect.top,
      trackRect.right,
      (textDirection == TextDirection.rtl)
          ? trackRect.bottom + (additionalActiveTrackHeight / 2)
          : trackRect.bottom,
      topRight: (textDirection == TextDirection.rtl)
          ? activeTrackRadius
          : trackRadius,
      bottomRight: (textDirection == TextDirection.rtl)
          ? activeTrackRadius
          : trackRadius,
    ),
    rightTrackPaint,
  );
}