getPreferredRect method Null safety

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

Returns the preferred bounds of the shape.

It is used to provide horizontal boundaries for the position of the thumbs, and to help position the slider thumbs and tick marks relative to the track.

The parentBox argument can be used to help determine the preferredRect relative to attributes of the render box of the slider itself, such as size.

The offset argument is relative to the caller's bounding box. It can be used to convert gesture coordinates from global to slider-relative coordinates.

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

The isEnabled argument is false when RangeSlider.onChanged is null and true otherwise. When true, the slider will respond to input.

The isDiscrete argument is true if RangeSlider.divisions is non-null. When true, the slider will render tick marks on top of the track.

Implementation

@override
Rect getPreferredRect({
  required RenderBox parentBox,
  Offset offset = Offset.zero,
  required SliderThemeData sliderTheme,
  bool isEnabled = false,
  bool isDiscrete = false,
}) {
  assert(parentBox != null);
  assert(offset != null);
  assert(sliderTheme != null);
  assert(sliderTheme.overlayShape != null);
  assert(isEnabled != null);
  assert(isDiscrete != null);
  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 + overlayWidth / 2;
  final double trackTop = offset.dy + (parentBox.size.height - trackHeight) / 2;
  final double trackRight = trackLeft + parentBox.size.width - 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);
}