confirmPreview method

CalendarState confirmPreview({
  1. bool confirmRange = false,
  2. required bool movingStartMaintainsLength,
})

Updates one endpoint of the "current" selection to match the preview confirmRange - If true, this the last endpoint to be set for the current range. This information is not used by CalendarState directly, but clients of CalendarState may need it.

Implementation

CalendarState confirmPreview(
    {bool confirmRange = false, required bool movingStartMaintainsLength}) {
  var current = selection(currentSelection);
  var anchor = previewAnchoredAtStart ? current.start : current.end;
  assert(preview != null && anchor != null);

  var cause = confirmRange ? CausedBy.rangeConfirm : CausedBy.endpointConfirm;

  if (previewAnchoredAtStart) {
    // Selecting end date.

    if (preview! <= anchor) {
      // End date was moved before (or onto) the start date. Collapse to a
      // single-day range and keep the end date active.
      return setSelection(
          CalendarSelection(currentSelection, preview, preview),
          cause: cause,
          previewAnchoredAtStart: true);
    } else {
      // Modify end date.
      return setSelection(
          CalendarSelection(currentSelection, anchor, preview),
          cause: cause,
          previewAnchoredAtStart: false);
    }
  } else {
    // Selecting start date.

    if (movingStartMaintainsLength) {
      // Preserve the length of the range, and make the end date active.
      var rangeLengthInDays =
          daysSpanned(current.start!, current.end!, inclusive: false);
      return setSelection(
          CalendarSelection(currentSelection, preview,
              preview!.add(days: rangeLengthInDays)),
          cause: cause,
          previewAnchoredAtStart: true);
    } else if (preview! >= anchor) {
      // Move only the start date.
      //
      // Start date was moved after the end date. Collapse to a single-day
      // range and activate the end date.
      return setSelection(
          CalendarSelection(currentSelection, preview, preview),
          cause: cause,
          previewAnchoredAtStart: true);
    } else {
      // Move only the start date.
      return setSelection(
          CalendarSelection(currentSelection, preview, anchor),
          cause: cause,
          previewAnchoredAtStart: true);
    }
  }
}