paint method

  1. @override
void paint (PaintingContext context, Offset center, { @required RenderBox parentBox, @required SliderThemeData sliderTheme, @required Animation<double> enableAnimation, @required TextDirection textDirection, @required Offset thumbCenter, bool isEnabled: false })
override

Paints the slider track.

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

center is the offset for where this shape's center should be painted. This offset is relative to the origin of the context canvas.

parentBox is the RenderBox of the Slider. Its attributes, such as size, can be used to assist in painting this shape.

sliderTheme is the theme assigned to the Slider that this shape belongs to.

enableAnimation is an animation triggered when the Slider is enabled, and it reverses when the slider is disabled. Enabled is the Slider.isInteractive state. Use this to paint intermediate frames for this shape when the slider changes enabled state.

isEnabled has the same value as Slider.isInteractive. If true, the slider will respond to input.

textDirection 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,
  bool isEnabled = false,
}) {
  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);
  }
}