paint method Null safety

  1. @override
void paint(
  1. PaintingContext context,
  2. Offset center,
  3. {required RenderBox parentBox,
  4. required SliderThemeData sliderTheme,
  5. required Animation<double> enableAnimation,
  6. required Offset startThumbCenter,
  7. required Offset endThumbCenter,
  8. bool isEnabled = false,
  9. required TextDirection textDirection}
)
override

Paints the slider track.

The context argument represents the RangeSlider'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 RangeSlider. Its attributes, such as size, can be used to assist in painting this shape.

The sliderTheme argument is the theme assigned to the RangeSlider that this shape belongs to.

The enableAnimation argument is an animation triggered when the RangeSlider is enabled, and it reverses when the slider is disabled. The RangeSlider is enabled when RangeSlider.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 RangeSlider.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 painted depending on whether they are on an active track segment or not.

The track segment between the two thumbs is the active track segment. The track segments between the thumb and each end of the slider are the inactive track segments. 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 center, {
  required RenderBox parentBox,
  required SliderThemeData sliderTheme,
  required Animation<double> enableAnimation,
  required Offset startThumbCenter,
  required Offset endThumbCenter,
  bool isEnabled = false,
  required TextDirection textDirection,
}) {
  assert(context != null);
  assert(center != null);
  assert(parentBox != null);
  assert(sliderTheme != null);
  assert(sliderTheme.disabledActiveTickMarkColor != null);
  assert(sliderTheme.disabledInactiveTickMarkColor != null);
  assert(sliderTheme.activeTickMarkColor != null);
  assert(sliderTheme.inactiveTickMarkColor != null);
  assert(enableAnimation != null);
  assert(startThumbCenter != null);
  assert(endThumbCenter != null);
  assert(isEnabled != null);
  assert(textDirection != null);

  final bool isBetweenThumbs;
  switch (textDirection) {
    case TextDirection.ltr:
      isBetweenThumbs = startThumbCenter.dx < center.dx && center.dx < endThumbCenter.dx;
      break;
    case TextDirection.rtl:
      isBetweenThumbs = endThumbCenter.dx < center.dx && center.dx < startThumbCenter.dx;
      break;
  }
  final Color? begin = isBetweenThumbs ? sliderTheme.disabledActiveTickMarkColor : sliderTheme.disabledInactiveTickMarkColor;
  final Color? end = isBetweenThumbs ? sliderTheme.activeTickMarkColor : sliderTheme.inactiveTickMarkColor;
  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);
  }
}