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,
}) {
  final double thumbWidth = sliderTheme.thumbShape!.getPreferredSize(isEnabled, isDiscrete).width;
  final double overlayWidth = sliderTheme.overlayShape!.getPreferredSize(isEnabled, isDiscrete).width;
  final double trackHeight = sliderTheme.trackHeight!;
  assert(overlayWidth >= 0);
  assert(trackHeight >= 0);

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