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 TextDirection textDirection,
  7. required Offset thumbCenter,
  8. required bool isEnabled}
)
override

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(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(textDirection != null);
  assert(thumbCenter != null);
  assert(isEnabled != null);
  // The paint color of the tick mark depends on its position relative
  // to the thumb and the text direction.
  Color? begin;
  Color? end;
  switch (textDirection) {
    case TextDirection.ltr:
      final bool isTickMarkRightOfThumb = center.dx > thumbCenter.dx;
      begin = isTickMarkRightOfThumb ? sliderTheme.disabledInactiveTickMarkColor : sliderTheme.disabledActiveTickMarkColor;
      end = isTickMarkRightOfThumb ? sliderTheme.inactiveTickMarkColor : sliderTheme.activeTickMarkColor;
      break;
    case TextDirection.rtl:
      final bool isTickMarkLeftOfThumb = center.dx < thumbCenter.dx;
      begin = isTickMarkLeftOfThumb ? sliderTheme.disabledInactiveTickMarkColor : sliderTheme.disabledActiveTickMarkColor;
      end = isTickMarkLeftOfThumb ? sliderTheme.inactiveTickMarkColor : sliderTheme.activeTickMarkColor;
      break;
  }
  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);
  }
}