scrollToMonth method

Future<void> scrollToMonth({
  1. required DateTime date,
  2. double alignment = 0,
  3. required Duration duration,
  4. Curve curve = Curves.linear,
  5. List<double> opacityAnimationWeights = const [40, 20, 40],
})

Scroll to date.month.

Animate the list over duration using the given curve such that the item at index ends up with its leading edge at the given alignment. See jumpTo for an explanation of alignment.

The duration must be greater than 0; otherwise, use jumpTo.

When item position is not available, because it's too far, the scroll is composed into three phases:

  1. The currently displayed list view starts scrolling.
  2. Another list view, which scrolls with the same speed, fades over the first one and shows items that are close to the scroll target.
  3. The second list view scrolls and stops on the target.

The opacityAnimationWeights can be used to apply custom weights to these three stages of this animation. The default weights, [40, 20, 40], are good with default Curves.linear. Different weights might be better for other cases. For example, if you use Curves.easeOut, consider setting opacityAnimationWeights to [20, 20, 60].

Implementation

Future<void> scrollToMonth({
  required DateTime date,
  double alignment = 0,
  required Duration duration,
  Curve curve = Curves.linear,
  List<double> opacityAnimationWeights = const [40, 20, 40],
}) async {
  if (!(date.year >= minDate.year &&
      (date.year > minDate.year || date.month >= minDate.month) &&
      date.year <= maxDate.year &&
      (date.year < maxDate.year || date.month <= maxDate.month))) {
    return;
  }
  final month =
      ((date.year - minDate.year) * 12) - minDate.month + date.month;
  await itemScrollController.scrollTo(
      index: month,
      alignment: alignment,
      duration: duration,
      curve: curve,
      opacityAnimationWeights: opacityAnimationWeights);
}