getPreferredRect method

Rect getPreferredRect({
  1. required RenderBox parentBox,
  2. Offset offset = Offset.zero,
  3. required SliderThemeData sliderTheme,
  4. bool isEnabled = false,
  5. bool isDiscrete = false,
})

Returns a rect that represents the track bounds that fits within the Slider.

The width is the width of the Slider or RangeSlider, but padded by the max of the overlay and thumb radius. The height is defined by the SliderThemeData.trackHeight.

The Rect is centered both horizontally and vertically within the slider bounds.

Implementation

Rect getPreferredRect({
  required RenderBox parentBox,
  Offset offset = Offset.zero,
  required SliderThemeData sliderTheme,
  bool isEnabled = false,
  bool isDiscrete = false,
}) {
  assert(sliderTheme.rangeThumbShape != null);
  assert(sliderTheme.overlayShape != null);
  final thumbWidth = sliderTheme.rangeThumbShape!
      .getPreferredSize(isEnabled, isDiscrete)
      .width;
  final overlayWidth =
      sliderTheme.overlayShape!.getPreferredSize(isEnabled, isDiscrete).width;
  final trackHeight = sliderTheme.trackHeight!;
  assert(overlayWidth >= 0);
  assert(trackHeight >= 0);

  final trackLeft = offset.dx + math.max(overlayWidth / 2, thumbWidth / 2);
  final trackTop = offset.dy + (parentBox.size.height - trackHeight) / 2;
  final trackRight =
      trackLeft + parentBox.size.width - math.max(thumbWidth, overlayWidth);
  final trackBottom = trackTop + trackHeight;
  final rect = Rect.fromLTRB(math.min(trackLeft, trackRight), trackTop,
      math.max(trackLeft, trackRight), trackBottom);
  // If the parentBox'size less than slider's size the trackRight will be less than trackLeft, so switch them.
  (sliderTheme as TDSliderThemeData).sliderMeasureData.trackerRect = rect;
  return rect;
}