scrollByPageView method
void
scrollByPageView(
- Size tabCenterSize,
- List<
Size> ? sizeList, - ScrollController? scrollController,
- PageController pageController,
根据pageController来设置偏移量
Implementation
void scrollByPageView(Size tabCenterSize, List<Size>? sizeList,
ScrollController? scrollController, PageController pageController) {
if (scrollController == null) return;
var index = pageController.page!.ceil();
var preIndex = pageController.page!.floor();
var offsetPercent = pageController.page! % 1;
var currentIndexHalfSize = sizeList![index] / 2;
var preIndexHalfSize = sizeList[preIndex] / 2;
var totalSize = Size(currentIndexHalfSize.width + preIndexHalfSize.width,
currentIndexHalfSize.height + preIndexHalfSize.height);
var startOffset = getTargetItemScrollStartOffset(sizeList, preIndex);
var endSize = sizeList[preIndex] / 2 + startOffset;
var contentInsertSize = getTabBarSize(sizeList);
bool isVisible =
isItemVisible(scrollController, index, sizeList, tabCenterSize * 2);
var offset = 0.0;
if (direction == Axis.horizontal) {
if (isVisible) {
if (endSize.width + totalSize.width > tabCenterSize.width) {
if (endSize.width > tabCenterSize.width) {
offset = endSize.width -
tabCenterSize.width +
offsetPercent * totalSize.width;
} else {
offset = offsetPercent *
(totalSize.width + endSize.width - tabCenterSize.width);
}
if (contentInsertSize.width - offset - tabCenterSize.width >
tabCenterSize.width) {
scrollController.jumpTo(offset);
}
}
} else {
offset = startOffset.dx - tabCenterSize.width;
scrollController.jumpTo(offset < 0 ? 0 : offset);
}
} else {
//竖直方向
if (isVisible) {
if (endSize.height + totalSize.height > tabCenterSize.height) {
if (endSize.height > tabCenterSize.height) {
offset = endSize.height -
tabCenterSize.height +
offsetPercent * totalSize.height;
} else {
offset = offsetPercent *
(totalSize.height + endSize.height - tabCenterSize.height);
}
if (contentInsertSize.height - offset - tabCenterSize.height >
tabCenterSize.height) {
scrollController.jumpTo(offset);
}
}
} else {
offset = startOffset.dy - tabCenterSize.height;
scrollController.jumpTo(offset < 0 ? 0 : offset);
}
}
}