Flutter的滾動以及sliver約束

Flutter框架中有很多滾動的Widget,ListView、GridView等,這些Widget都是使用Scrollable配合Viewport來完成滾動的。我們來分析一下這個滾動效果是怎樣實現的。

Scrollable在滾動中的作用

Scrollable繼承自StatefulWidget,我們看一下他的State的build方法來看一下他的構成

@override
Widget build(BuildContext context) {assert(position != null);Widget result = _ScrollableScope(scrollable: this,position: position,child: RawGestureDetector(key: _gestureDetectorKey,gestures: _gestureRecognizers,behavior: HitTestBehavior.opaque,excludeFromSemantics: widget.excludeFromSemantics,child: Semantics(explicitChildNodes: !widget.excludeFromSemantics,child: IgnorePointer(key: _ignorePointerKey,ignoring: _shouldIgnorePointer,ignoringSemantics: false,child: widget.viewportBuilder(context, position),),),),);...省略不重要的	return _configuration.buildViewportChrome(context, result, widget.axisDirection);
}
復制代碼

可以看到最主要的兩點就是:RawGestureDetector來監聽用戶手勢,viewportBuilder來創建Viewport

Scrollable中有一個重要的字段就是ScrollPosition(繼承自ViewportOffset,ViewportOffset又繼承自ChangeNotifier),ViewportOffset是viewportBuilder中的一個重要參數,用來描述Viewport的偏移量。ScrollPosition是在_updatePosition方法中進行更新和創建的。

void _updatePosition() {_configuration = ScrollConfiguration.of(context);_physics = _configuration.getScrollPhysics(context);if (widget.physics != null)_physics = widget.physics.applyTo(_physics);final ScrollController controller = widget.controller;final ScrollPosition oldPosition = position;if (oldPosition != null) {controller?.detach(oldPosition);scheduleMicrotask(oldPosition.dispose);}//更新_position_position = controller?.createScrollPosition(_physics, this, oldPosition)?? ScrollPositionWithSingleContext(physics: _physics, context: this, oldPosition: oldPosition);assert(position != null);controller?.attach(position);
}
復制代碼

可以看到ScrollPosition的實例是ScrollPositionWithSingleContext,而且_updatePosition是在didChangeDependencies以及didUpdateWidget方法中調用的(在Element更新的情況下都會去更新position)。

我們繼續看Scrollable中的手勢監聽_handleDragDown、_handleDragStart、_handleDragUpdate、_handleDragEnd、_handleDragCancel這五個方法來處理用戶的手勢。

void _handleDragDown(DragDownDetails details) {assert(_drag == null);assert(_hold == null);_hold = position.hold(_disposeHold);
}@override
ScrollHoldController hold(VoidCallback holdCancelCallback) {final double previousVelocity = activity.velocity;final HoldScrollActivity holdActivity = HoldScrollActivity(delegate: this,onHoldCanceled: holdCancelCallback,);beginActivity(holdActivity);//開始HoldScrollActivity活動_heldPreviousVelocity = previousVelocity;return holdActivity;
}
復制代碼

可以看到_handleDragDown中就是調用ScrollPosition的hold方法返回一個holdActivity。我們繼續看一下_handleDragStart

void _handleDragStart(DragStartDetails details) {assert(_drag == null);_drag = position.drag(details, _disposeDrag);assert(_drag != null);assert(_hold == null);
}@override
Drag drag(DragStartDetails details, VoidCallback dragCancelCallback) {final ScrollDragController drag = ScrollDragController(delegate: this,details: details,onDragCanceled: dragCancelCallback,carriedVelocity: physics.carriedMomentum(_heldPreviousVelocity),motionStartDistanceThreshold: physics.dragStartDistanceMotionThreshold,);beginActivity(DragScrollActivity(this, drag));//開始DragScrollActivity活動assert(_currentDrag == null);_currentDrag = drag;return drag;//返回ScrollDragController
}
復制代碼

_handleDragStart中調用ScrollPosition的drag方法但是返回的ScrollDragController對象,并沒有返回DragScrollActivity。我們繼續看一下_handleDragUpdate、_handleDragEnd、_handleDragCancel方法

void _handleDragUpdate(DragUpdateDetails details) {assert(_hold == null || _drag == null);_drag?.update(details);
}void _handleDragEnd(DragEndDetails details) {assert(_hold == null || _drag == null);_drag?.end(details);assert(_drag == null);
}void _handleDragCancel() {assert(_hold == null || _drag == null);_hold?.cancel();_drag?.cancel();assert(_hold == null);assert(_drag == null);
}
復制代碼

_handleDragUpdate、_handleDragEnd、_handleDragCancel基本就是調用_hold,_drag的對應的方法。我們先看一下ScrollPositionWithSingleContext中的beginActivity方法

@override
void beginActivity(ScrollActivity newActivity) {_heldPreviousVelocity = 0.0;if (newActivity == null)return;assert(newActivity.delegate == this);super.beginActivity(newActivity);_currentDrag?.dispose();_currentDrag = null;if (!activity.isScrolling)updateUserScrollDirection(ScrollDirection.idle);
}///ScrollPosition的beginActivity方法
void beginActivity(ScrollActivity newActivity) {if (newActivity == null)return;bool wasScrolling, oldIgnorePointer;if (_activity != null) {oldIgnorePointer = _activity.shouldIgnorePointer;wasScrolling = _activity.isScrolling;if (wasScrolling && !newActivity.isScrolling)didEndScroll();_activity.dispose();} else {oldIgnorePointer = false;wasScrolling = false;}_activity = newActivity;if (oldIgnorePointer != activity.shouldIgnorePointer)context.setIgnorePointer(activity.shouldIgnorePointer);isScrollingNotifier.value = activity.isScrolling;if (!wasScrolling && _activity.isScrolling)didStartScroll();
}
復制代碼

ScrollPosition的beginActivity總結下來就是發送相關的ScrollNotification(我們用NotificationListener可以監聽)以及dispose上一個activity,ScrollPositionWithSingleContext的beginActivity方法后續會調用updateUserScrollDirection方法來更新以及發送UserScrollDirection。

看到這里我們可以發現Scrollable的第一個作用就是發送ScrollNotification。我們繼續看一下update時的情況,_handleDragUpdate就是調用Drag的update方法,我們直接看update方法,它的具體實現是ScrollDragController

@override
void update(DragUpdateDetails details) {assert(details.primaryDelta != null);_lastDetails = details;double offset = details.primaryDelta;if (offset != 0.0) {_lastNonStationaryTimestamp = details.sourceTimeStamp;}_maybeLoseMomentum(offset, details.sourceTimeStamp);offset = _adjustForScrollStartThreshold(offset, details.sourceTimeStamp);//根據ios的彈性滑動調整offsetif (offset == 0.0) {return;}if (_reversed)offset = -offset;delegate.applyUserOffset(offset);//調用ScrollPositionWithSingleContext的applyUserOffset方法
}
復制代碼

主要看最后applyUserOffset方法

@override
void applyUserOffset(double delta) {updateUserScrollDirection(delta > 0.0 ? ScrollDirection.forward : ScrollDirection.reverse);//發送UserScrollNotificationsetPixels(pixels - physics.applyPhysicsToUserOffset(this, delta));//setPixels直接調用了super.setPixels
}double setPixels(double newPixels) {assert(_pixels != null);assert(SchedulerBinding.instance.schedulerPhase.index <= SchedulerPhase.transientCallbacks.index);if (newPixels != pixels) {final double overscroll = applyBoundaryConditions(newPixels);//計算出overscrollassert(() {final double delta = newPixels - pixels;if (overscroll.abs() > delta.abs()) {throw FlutterError();}return true;}());final double oldPixels = _pixels;_pixels = newPixels - overscroll;//計算出滾動距離if (_pixels != oldPixels) {notifyListeners();//通知Listeners,因為ScrollPosition繼承自ChangeNotifier,可以設置Listeners,這里也是直接調用了ChangeNotifier中的notifyListeners方法didUpdateScrollPositionBy(_pixels - oldPixels);//調用activity發送ScrollUpdateNotification}if (overscroll != 0.0) {didOverscrollBy(overscroll);//調用activity發送OverscrollNotificationreturn overscroll;}}return 0.0;
}
復制代碼

applyUserOffset方法中調用了一個非常重要的notifyListeners方法,那么這些Listeners是在哪設置的呢?在RenderViewport中找到了它的設置地方

@override
void attach(PipelineOwner owner) {super.attach(owner);_offset.addListener(markNeedsLayout);//直接標記重新layout
}@override
void detach() {_offset.removeListener(markNeedsLayout);super.detach();
}
復制代碼

可以看到在RenderObject attach的時候添加監聽,在detach的時候移除監聽,至于監聽中的實現,在_RenderSingleChildViewport中有不同的實現。

到此我們可以總結出Scrollable的主要作用了

  1. 監聽用戶手勢,計算轉換出各種滾動情況,并進行通知
  2. 計算滾動的pixels,然后通知Listeners

Viewport在滾動中的作用

我們先看只包含一個Child的Viewport

_RenderSingleChildViewport單一child的Viewport

@override
void attach(PipelineOwner owner) {super.attach(owner);_offset.addListener(_hasScrolled);
}@override
void detach() {_offset.removeListener(_hasScrolled);super.detach();
}void _hasScrolled() {markNeedsPaint();markNeedsSemanticsUpdate();
}
復制代碼

在_RenderSingleChildViewport中當發生滾動的時候時只需要重繪的,我們先看一下他怎樣進行布局的

@override
void performLayout() {if (child == null) {size = constraints.smallest;} else {child.layout(_getInnerConstraints(constraints), parentUsesSize: true);//計算child的約束去布局childsize = constraints.constrain(child.size);//自己的size最大不能超過自身的Box約束}offset.applyViewportDimension(_viewportExtent);offset.applyContentDimensions(_minScrollExtent, _maxScrollExtent);
}BoxConstraints _getInnerConstraints(BoxConstraints constraints) {switch (axis) {case Axis.horizontal:return constraints.heightConstraints();//橫向滾動,就返回高度按parent傳進來的約束,寬度約束就是0到無窮大case Axis.vertical:return constraints.widthConstraints();//縱向滾動,就返回寬度按parent傳進來的約束,高度約束就是0到無窮大}return null;
}
復制代碼

看一下offset.applyViewportDimension方法,offset是傳入的ViewportOffset,_viewportExtent(視窗范圍),看一下其get方法

double get _viewportExtent {assert(hasSize);switch (axis) {case Axis.horizontal:return size.width;//橫向滾動,就返回自身size的寬度case Axis.vertical:return size.height;//縱向滾動,就返回自身size的高度}return null;
}@override
bool applyViewportDimension(double viewportDimension) {if (_viewportDimension != viewportDimension) {_viewportDimension = viewportDimension;//簡單的賦值_didChangeViewportDimensionOrReceiveCorrection = true;}return true;
}
復制代碼

offset.applyViewportDimension就是簡單的計算viewportExtent的值并賦值給ScrollPosition。我們在看一下offset.applyContentDimensions(_minScrollExtent, _maxScrollExtent)方法

double get _minScrollExtent {assert(hasSize);return 0.0;
}double get _maxScrollExtent {assert(hasSize);if (child == null)return 0.0;switch (axis) {case Axis.horizontal:return math.max(0.0, child.size.width - size.width);case Axis.vertical:return math.max(0.0, child.size.height - size.height);}return null;
}@override
bool applyContentDimensions(double minScrollExtent, double maxScrollExtent) {if (!nearEqual(_minScrollExtent, minScrollExtent, Tolerance.defaultTolerance.distance) ||!nearEqual(_maxScrollExtent, maxScrollExtent, Tolerance.defaultTolerance.distance) ||_didChangeViewportDimensionOrReceiveCorrection) {_minScrollExtent = minScrollExtent;//簡單的賦值_maxScrollExtent = maxScrollExtent;//簡單的賦值_haveDimensions = true;applyNewDimensions();//通知活動viewport的尺寸或者內容發生了改變_didChangeViewportDimensionOrReceiveCorrection = false;}return true;
}
復制代碼

offset.applyContentDimensions(_minScrollExtent, _maxScrollExtent)方法也基本上就是計算minScrollExtent、maxScrollExtent然后進行賦值。

我們在看paint方法

@override
void paint(PaintingContext context, Offset offset) {if (child != null) {final Offset paintOffset = _paintOffset;//計算出繪制偏移void paintContents(PaintingContext context, Offset offset) {context.paintChild(child, offset + paintOffset);//加上繪制偏移去繪制child}if (_shouldClipAtPaintOffset(paintOffset)) {//看是否需要裁剪context.pushClipRect(needsCompositing, offset, Offset.zero & size, paintContents);} else {paintContents(context, offset);}}
}Offset get _paintOffset => _paintOffsetForPosition(offset.pixels);Offset _paintOffsetForPosition(double position) {assert(axisDirection != null);switch (axisDirection) {case AxisDirection.up://往上滾動,把內容網上偏移繪制return Offset(0.0, position - child.size.height + size.height);case AxisDirection.down:return Offset(0.0, -position);//往下滾動,把內容網上偏移繪制case AxisDirection.left:return Offset(position - child.size.width + size.width, 0.0);case AxisDirection.right:return Offset(-position, 0.0);}return null;
}bool _shouldClipAtPaintOffset(Offset paintOffset) {assert(child != null);//這句話的意思可以翻譯成這樣:繪制內容的左上坐標以及右下坐標是否在Viewport的size里面,否則就需要裁剪return paintOffset < Offset.zero || !(Offset.zero & size).contains((paintOffset & child.size).bottomRight);
}
復制代碼

可以看到單個child的viewport還是使用的盒約束去布局child,而且它的滾動效果實現就是通過繪制偏移來實現的。

RenderViewport多個child的Viewport

我們上面知道RenderViewport在offset改變時會重新去布局繪制,因為在RenderViewport重寫了sizedByParent,那么它自身的size是在performResize中確定的,我們先看performResize

@override
void performResize() {size = constraints.biggest;//確定自己的size為約束的最大范圍switch (axis) {case Axis.vertical:offset.applyViewportDimension(size.height);//賦值ViewportDimensionbreak;case Axis.horizontal:offset.applyViewportDimension(size.width);break;}
}
復制代碼

然后我們繼續看performLayout

@override
void performLayout() {if (center == null) {assert(firstChild == null);_minScrollExtent = 0.0;_maxScrollExtent = 0.0;_hasVisualOverflow = false;offset.applyContentDimensions(0.0, 0.0);return;}assert(center.parent == this);double mainAxisExtent;double crossAxisExtent;switch (axis) {case Axis.vertical:mainAxisExtent = size.height;crossAxisExtent = size.width;break;case Axis.horizontal:mainAxisExtent = size.width;crossAxisExtent = size.height;break;}final double centerOffsetAdjustment = center.centerOffsetAdjustment;double correction;int count = 0;do {assert(offset.pixels != null);correction = _attemptLayout(mainAxisExtent, crossAxisExtent, offset.pixels + centerOffsetAdjustment);if (correction != 0.0) {offset.correctBy(correction);} else {if (offset.applyContentDimensions(math.min(0.0, _minScrollExtent + mainAxisExtent * anchor),math.max(0.0, _maxScrollExtent - mainAxisExtent * (1.0 - anchor)),))break;}count += 1;} while (count < _maxLayoutCycles);
}
復制代碼

performLayout里面存在一個循環,只要哪個元素布局的過程中需要調整滾動的偏移量,就會更新滾動偏移量之后再重新布局,但是重新布局的次數不能超過_kMaxLayoutCycles也就是10次,這里也是明顯從性能考慮;看一下_attemptLayout方法

double _attemptLayout(double mainAxisExtent, double crossAxisExtent, double correctedOffset) {_minScrollExtent = 0.0;_maxScrollExtent = 0.0;_hasVisualOverflow = false;//第一個sliver布局開始點的偏移final double centerOffset = mainAxisExtent * anchor - correctedOffset;//反向余留的繪制范圍final double reverseDirectionRemainingPaintExtent = centerOffset.clamp(0.0, mainAxisExtent);//正向余留的繪制范圍final double forwardDirectionRemainingPaintExtent = (mainAxisExtent - centerOffset).clamp(0.0, mainAxisExtent);//總共的緩存范圍final double fullCacheExtent = mainAxisExtent + 2 * cacheExtent;final double centerCacheOffset = centerOffset + cacheExtent;//反向余留的緩存范圍final double reverseDirectionRemainingCacheExtent = centerCacheOffset.clamp(0.0, fullCacheExtent);//正向余留的緩存范圍final double forwardDirectionRemainingCacheExtent = (fullCacheExtent - centerCacheOffset).clamp(0.0, fullCacheExtent);final RenderSliver leadingNegativeChild = childBefore(center);if (leadingNegativeChild != null) {//反向滾動final double result = layoutChildSequence(child: leadingNegativeChild,scrollOffset: math.max(mainAxisExtent, centerOffset) - mainAxisExtent,overlap: 0.0,layoutOffset: forwardDirectionRemainingPaintExtent,remainingPaintExtent: reverseDirectionRemainingPaintExtent,mainAxisExtent: mainAxisExtent,crossAxisExtent: crossAxisExtent,growthDirection: GrowthDirection.reverse,advance: childBefore,remainingCacheExtent: reverseDirectionRemainingCacheExtent,cacheOrigin: (mainAxisExtent - centerOffset).clamp(-cacheExtent, 0.0),);if (result != 0.0)return -result;}//正向滾動return layoutChildSequence(child: center,scrollOffset: math.max(0.0, -centerOffset),overlap: leadingNegativeChild == null ? math.min(0.0, -centerOffset) : 0.0,layoutOffset: centerOffset >= mainAxisExtent ? centerOffset: reverseDirectionRemainingPaintExtent,remainingPaintExtent: forwardDirectionRemainingPaintExtent,mainAxisExtent: mainAxisExtent,crossAxisExtent: crossAxisExtent,growthDirection: GrowthDirection.forward,advance: childAfter,remainingCacheExtent: forwardDirectionRemainingCacheExtent,cacheOrigin: centerOffset.clamp(-cacheExtent, 0.0),);
}
復制代碼

這里面可以看到就是一些變量的賦值,然后根據正向反向來進行布局,這里我們先要說明一下這幾個變量的意思

我們繼續看layoutChildSequence方法

@protected
double layoutChildSequence({@required RenderSliver child,//布局的起始child,類型必須是RenderSliver@required double scrollOffset,//centerSliver的偏移量@required double overlap,@required double layoutOffset,//布局的偏移量@required double remainingPaintExtent,//剩余需要繪制的范圍@required double mainAxisExtent,//viewport的主軸范圍@required double crossAxisExtent,//viewport的縱軸范圍@required GrowthDirection growthDirection,//增長方向@required RenderSliver advance(RenderSliver child),@required double remainingCacheExtent,//剩余需要緩存的范圍@required double cacheOrigin,//緩存的起點
}) {//將傳進來的layoutOffset記錄為初始布局偏移final double initialLayoutOffset = layoutOffset;final ScrollDirection adjustedUserScrollDirection =applyGrowthDirectionToScrollDirection(offset.userScrollDirection, growthDirection);assert(adjustedUserScrollDirection != null);//初始最大繪制偏移double maxPaintOffset = layoutOffset + overlap;double precedingScrollExtent = 0.0;while (child != null) {//計算sliver的滾動偏移,scrollOffset <= 0.0表示當前sliver的偏移量還沒越過viewport頂部,還沒有輪到該sliver滾動,所以sliver的滾動偏移為0final double sliverScrollOffset = scrollOffset <= 0.0 ? 0.0 : scrollOffset;final double correctedCacheOrigin = math.max(cacheOrigin, -sliverScrollOffset);final double cacheExtentCorrection = cacheOrigin - correctedCacheOrigin;//創建SliverConstraints去布局childchild.layout(SliverConstraints(axisDirection: axisDirection,//主軸方向growthDirection: growthDirection,//sliver的排列方向userScrollDirection: adjustedUserScrollDirection,//用戶滾動方向scrollOffset: sliverScrollOffset,//sliver的滾動偏移量precedingScrollExtent: precedingScrollExtent,//被前面sliver消費的滾動距離overlap: maxPaintOffset - layoutOffset,remainingPaintExtent: math.max(0.0, remainingPaintExtent - layoutOffset + initialLayoutOffset),//sliver仍然需要繪制的范圍crossAxisExtent: crossAxisExtent,//縱軸的范圍crossAxisDirection: crossAxisDirection,viewportMainAxisExtent: mainAxisExtent,//viewport主軸的范圍remainingCacheExtent: math.max(0.0, remainingCacheExtent + cacheExtentCorrection),//sliver仍然需要緩存的范圍cacheOrigin: correctedCacheOrigin,), parentUsesSize: true);final SliverGeometry childLayoutGeometry = child.geometry;assert(childLayoutGeometry.debugAssertIsValid());//scrollOffsetCorrection如果不為空,就要重新開始布局if (childLayoutGeometry.scrollOffsetCorrection != null)return childLayoutGeometry.scrollOffsetCorrection;//計算sliver的layout偏移final double effectiveLayoutOffset = layoutOffset + childLayoutGeometry.paintOrigin;//記錄sliver的layout偏移if (childLayoutGeometry.visible || scrollOffset > 0) {updateChildLayoutOffset(child, effectiveLayoutOffset, growthDirection);} else {updateChildLayoutOffset(child, -scrollOffset + initialLayoutOffset, growthDirection);}//更新最大繪制偏移maxPaintOffset = math.max(effectiveLayoutOffset + childLayoutGeometry.paintExtent, maxPaintOffset);//計算下一個sliver的scrollOffset(center的sliver的scrollOffset是centerOffset)scrollOffset -= childLayoutGeometry.scrollExtent;//統計前面的sliver總共消耗的滾動范圍precedingScrollExtent += childLayoutGeometry.scrollExtent;//計算下一個sliver的布局偏移layoutOffset += childLayoutGeometry.layoutExtent;if (childLayoutGeometry.cacheExtent != 0.0) {//計算余下的緩存范圍,remainingCacheExtent需要減去當前sliver所用掉的cacheExtentremainingCacheExtent -= childLayoutGeometry.cacheExtent - cacheExtentCorrection;//計算下一個sliver的緩存起始cacheOrigin = math.min(correctedCacheOrigin + childLayoutGeometry.cacheExtent, 0.0);}updateOutOfBandData(growthDirection, childLayoutGeometry);布局下一個sliverchild = advance(child);}//正確完成布局直接返回0return 0.0;
}
復制代碼

從layout的過程我們可以看到,viewport布局每一個child的時候是計算一個sliver約束去布局,讓后更新每個sliver的layoutOffset。那我們再看一下viewport的繪制過程

@override
void paint(PaintingContext context, Offset offset) {if (firstChild == null)return;if (hasVisualOverflow) {//viewport有內容溢出就使用clip繪制context.pushClipRect(needsCompositing, offset, Offset.zero & size, _paintContents);} else {_paintContents(context, offset);}
}void _paintContents(PaintingContext context, Offset offset) {for (RenderSliver child in childrenInPaintOrder) {//sliver是否顯示,否則不繪制if (child.geometry.visible)//將layoutOffset運用的繪制偏移中,來定位每一個slivercontext.paintChild(child, offset + paintOffsetOf(child));}
}@override
Offset paintOffsetOf(RenderSliver child) {final SliverPhysicalParentData childParentData = child.parentData;return childParentData.paintOffset;
}
復制代碼

從viewport的size、layout、paint過程我們可以知道,viewport只確定sliver的layoutExtent、paintExtent(大小)以及layoutOffset(位置),然后對每個sliver進行繪制。 我們有一張圖大致可以表示viewport的布局繪制過程,只確定每個sliver的大小以及位置,不顯示的sliver不進行繪制;至于sliver內的內容滾動了多少,該怎樣去布局繪制,viewport只傳入了sliver約束,讓sliver自行去處理。

SliverConstraints以及SliverGeometry

這兩個是相對出現了,跟BoxConstraints與Size的關系一樣,一個作為輸入(SliverConstraints),一個作為輸出(SliverGeometry)

SliverConstraints({@required this.axisDirection,//scrollOffset、remainingPaintExtent增長的方向@required this.growthDirection,//sliver排列的方向@required this.userScrollDirection,//用戶滾動的方向,viewport的scrollOffset為正直是為forward,負值為reverse,沒有滾動則為idle@required this.scrollOffset,//在sliver坐標系中的滾動偏移量@required this.precedingScrollExtent,//前面sliver已經消耗的滾動距離,等于前面sliver的scrollExtent的累加結果@required this.overlap,//指前一個Sliver組件的layoutExtent(布局區域)和paintExtent(繪制區域)重疊了的區域大小@required this.remainingPaintExtent,//viewport仍剩余的繪制范圍@required this.crossAxisExtent,//viewport滾動軸縱向的范圍@required this.crossAxisDirection,//viewport滾動軸縱向的方向@required this.viewportMainAxisExtent,//viewport滾動軸的范圍@required this.remainingCacheExtent,//viewport仍剩余的緩存范圍@required this.cacheOrigin,//緩存起始
})SliverGeometry({this.scrollExtent = 0.0,//sliver可以滾動內容的總范圍this.paintExtent = 0.0,//sliver允許繪制的范圍this.paintOrigin = 0.0,//sliver的繪制起始double layoutExtent,//sliver的layout范圍this.maxPaintExtent = 0.0,//最大的繪制范圍,this.maxScrollObstructionExtent = 0.0,//當sliver被固定住,sliver可以減少內容滾動的區域的最大范圍double hitTestExtent,//命中測試的范圍bool visible,//是否可見,sliver是否應該被繪制this.hasVisualOverflow = false,//sliver是否有視覺溢出this.scrollOffsetCorrection,//滾動偏移修正,當部位null或zero時,viewport會開始新一輪layoutdouble cacheExtent,//緩存范圍
})
復制代碼

上面介紹了一下兩者屬性的意思,那如何根據輸入得到產出,我們需要看一個具體的實現(RenderSliverToBoxAdapter),我們看他的performLayout方法

@override
void performLayout() {if (child == null) {geometry = SliverGeometry.zero;return;}//布局child獲取child的size,將SliverConstraint轉換成BoxConstraints,在滾動的方向范圍沒有限制child.layout(constraints.asBoxConstraints(), parentUsesSize: true);double childExtent;switch (constraints.axis) {case Axis.horizontal:childExtent = child.size.width;break;case Axis.vertical:childExtent = child.size.height;break;}assert(childExtent != null);//計算它的繪制范圍final double paintedChildSize = calculatePaintOffset(constraints, from: 0.0, to: childExtent);//計算它的緩存范圍final double cacheExtent = calculateCacheOffset(constraints, from: 0.0, to: childExtent);assert(paintedChildSize.isFinite);assert(paintedChildSize >= 0.0);//得到SliverGeometry輸出geometry = SliverGeometry(scrollExtent: childExtent,//就是child的滾動內容大小paintExtent: paintedChildSize,//child需要繪制的范圍cacheExtent: cacheExtent,//緩存范圍maxPaintExtent: childExtent,//最大繪制范圍,child的滾動內容大小hitTestExtent: paintedChildSize,//命中測試范圍就是child繪制的范圍hasVisualOverflow: childExtent > constraints.remainingPaintExtent || constraints.scrollOffset > 0.0,//是否有視覺溢出);//設置ChildParentData就是設置繪制偏移setChildParentData(child, constraints, geometry);
}double calculatePaintOffset(SliverConstraints constraints, { @required double from, @required double to }) {assert(from <= to);final double a = constraints.scrollOffset;final double b = constraints.scrollOffset + constraints.remainingPaintExtent;return (to.clamp(a, b) - from.clamp(a, b)).clamp(0.0, constraints.remainingPaintExtent);
}void setChildParentData(RenderObject child, SliverConstraints constraints, SliverGeometry geometry) {final SliverPhysicalParentData childParentData = child.parentData;assert(constraints.axisDirection != null);assert(constraints.growthDirection != null);switch (applyGrowthDirectionToAxisDirection(constraints.axisDirection, constraints.growthDirection)) {case AxisDirection.up:childParentData.paintOffset = Offset(0.0, -(geometry.scrollExtent - (geometry.paintExtent + constraints.scrollOffset)));break;case AxisDirection.right:childParentData.paintOffset = Offset(-constraints.scrollOffset, 0.0);break;case AxisDirection.down:childParentData.paintOffset = Offset(0.0, -constraints.scrollOffset);break;case AxisDirection.left:childParentData.paintOffset = Offset(-(geometry.scrollExtent - (geometry.paintExtent + constraints.scrollOffset)), 0.0);break;}assert(childParentData.paintOffset != null);
}
復制代碼

child的SliverGeometry和繪制偏移都確定了,那么接下來就是繪制了,我們看一下繪制。

void paint(PaintingContext context, Offset offset) {if (child != null && geometry.visible) {final SliverPhysicalParentData childParentData = child.parentData;context.paintChild(child, offset + childParentData.paintOffset);}
}
復制代碼

就是簡單的加上偏移量再進行繪制。

總結

從以上分析來看,整個滾動形成由一下步驟來實現

  1. Scrollable監聽用戶手勢,通知viewport內容已經發生偏移
  2. viewport通過偏移值,去計算每個SliverConstraints來得到每個sliver的SliverGeometry,然后根據SliverGeometry對sliver進行大小、位置的確定并繪制
  3. 最后sliver根據布局階段計算出來的自己的滾動偏移量來對child進行繪制

轉載于:https://juejin.im/post/5caec613f265da03a00fbcde

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/536544.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/536544.shtml
英文地址,請注明出處:http://en.pswp.cn/news/536544.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

頁面增加html,為靜態頁面HTML增加session功能

一般來說&#xff0c;只有服務器端的CGI程序(ASP、PHP、JSP)具有session會話功能&#xff0c;用來保存用戶在網站期間(會話)的活動數據信息&#xff0c;而對于數量眾多的靜態頁面(HTML)來說&#xff0c;只能使用客戶端的cookies來保存臨時活動數據&#xff0c;但對于cookies的操…

關于Istio 1.1,你所不知道的細節

本文整理自Istio社區成員Star在 Cloud Native Days China 2019 北京站的現場分享 第1則 主角 Istio Istio作為service mesh領域的明星項目&#xff0c;從2016年發布到現在熱度不斷攀升。 Istio & Envoy Github Star Growth 官網中Istio1.1的架構圖除了數據面的Envoy和控制面…

html調用父頁面的函數,js調用父框架函數與彈窗調用父頁面函數的方法

調用父級中的 aaa的函數子頁面中:οnclick"window.parent.frames.aaa()"父頁面中:function aaa(){alert(‘bbbbb’);}----------------------------------------------frame框架里的頁面要改其他同框架下的頁面或父框架的頁面就用parentwindow.opener引用的是window.…

讀卡距離和信號強度兩方面來考慮

選擇物聯宇手持終端機的時候&#xff0c;你可以參考以下幾個原則&#xff1a;選擇行業需要應用功能&#xff0c;能有效控制好預算。屏幕界面需要高清晰的&#xff0c;選用分辨率較高的能更好的支持展現。按照項目所需求的來分析&#xff0c;需要從讀卡距離和信號強度兩方面來考…

html script 放置位置,script標簽應該放在HTML哪里,總結分享

幾年前&#xff0c;有經驗的程序員總是讓我們將很明顯&#xff0c;現在瀏覽器有了更加酷的兼容方式&#xff0c;這篇文章&#xff0c;俺將跟大家一起來學習script標簽的async和defer新特性&#xff0c;探討script應該放在哪里更好。頁面加載方式在我們討論當瀏覽器加載帶有獲取…

2021吉林高考26日幾點可以查詢成績,2021吉林高考成績查分時間及入口

2021吉林高考成績查分時間及入口2021吉林高考成績查分時間及入口&#xff0c;有一些高考生真的很積極&#xff0c;考完試當天就將答案給對好了&#xff0c;考試嘛&#xff0c;站在旁觀者的角度來看總是有人歡喜有人憂。估出來分數不咋地的&#xff0c;整個六月就毀了。2021吉林…

easyui,layui和 vuejs 有什么區別

2019獨角獸企業重金招聘Python工程師標準>>> easyui是功能強大但是有很多的組件使用功能是十分強大的&#xff0c;而layui是2016年才出來的前端框架&#xff0c;現在才更新到2.x版本還有很多的功能沒有完善&#xff0c;也還存在一些不穩定的情況&#xff0c;但是lay…

廣東2021高考成績位次查詢,廣東一分一段表查詢2021-廣東省2021年一分一段統計表...

廣東省高考一分一段表是同學們在填報高考志愿時的重要參考資料之一。根據一分一段表&#xff0c;大家不僅可以清楚地了解自己的高考成績在全省的排名&#xff0c;還可以結合心儀的大學近3年在廣東省的錄取位次變化&#xff0c;判斷出自己被錄取的概率大概是多少。根據考試院公布…

bootstrap-select動態生成數據,設置默認選項(默認值)

bootstrap-select設置選中的屬性是selected"selected"&#xff0c;只要找出哪一項要設置為默認選項&#xff0c;再設置其屬性selected"selected"即可&#xff0c;親測有效。代碼如下&#xff1a; var currentId $(_this).attr(data_id);//目標id&#xff…

無法顯示驗證碼去掉html,如何去除驗證碼-模版風格-易通免費企業網站系統 - Powered by CmsEasy...

去除前臺用戶登錄驗證碼1.修改lib/default/user_act.php 注釋掉或刪除55-58行修改為//if(!session::get(verify) || front::post(verify)<>session::get(verify)) {//front::flash(驗證碼錯誤&#xff01;);//return;//} 復制代碼2.修改template/default/user/login.html…

webpack4打包工具

什么是webpack webpack 是一個現代 JavaScript 應用程序的靜態模塊打包器(module bundler)。當 webpack 處理應用程序時&#xff0c;它會遞歸地構建一個依賴關系圖(dependency graph)&#xff0c;其中包含應用程序需要的每個模塊&#xff0c;然后將所有這些模塊打包成一個或多個…

通過計算機網絡進行的商務活動包括,電子商務練習題及答案

“電子商務”練習題一、填空題1&#xff0e;EDI系統構成三要素包括數據標準化、(EDI軟件及硬件)和(通信網絡)。2.B2C電子商務模式主要有&#xff1a;門戶網站、(電子零售商)、(內容提供商)、(交易經紀人)和社區服務商。3. 影響消費者網上購物的因素&#xff1a;商品特性、(商品…

PAKDD 2019 都有哪些重要看點?看這篇文章就夠了!...

雷鋒網 AI 科技評論按&#xff1a;亞太地區知識發現與數據挖掘國際會議&#xff08;Pacific Asia Knowledge Discovery and Data Mining&#xff0c;PAKDD&#xff09;是亞太地區數據挖掘領域的頂級國際會議&#xff0c;旨在為數據挖掘相關領域的研究者和從業者提供一個可自由 …

「javaScript-每三位插入一個逗號實現方式」

一道火了很久的面試題&#xff0c;//將以下數字從小數點前開始每三位數加一個逗號var num 1234567890.12345;復制代碼相信大家寫了這么久的前端代碼&#xff0c;不論是培訓也好&#xff0c;面試也好&#xff0c;這種題出現的頻率挺高的&#xff0c;網上方法很多&#xff0c;但…

計算機網絡df例題,計算機網絡期末試題北交.doc

計算機網絡期末試題北交北京交通大學 2007-2008學年 第學期考試試題課程名稱&#xff1a;計算機通信與網絡技術 出題人&#xff1a;網絡課程組題 號一二三五總分得 分簽 字選擇題(每題分&#xff0c;共0分)PING命令使用協議的報文A、TCP ?? ?B、UDP ??????????C、…

java B2B2C 仿淘寶電子商城系統-Spring Cloud Feign的文件上傳實現

在Spring Cloud封裝的Feign中并不直接支持傳文件&#xff0c;但可以通過引入Feign的擴展包來實現&#xff0c;本文就來具體說說如何實現。需要JAVA Spring Cloud大型企業分布式微服務云構建的B2B2C電子商務平臺源碼 一零三八七七四六二六 服務提供方&#xff08;接收文件&#…

2021計算機三級網絡技術教程,全國計算機等級考試三級教程——網絡技術(2021年版)...

前輔文第一單元 網絡規劃與設計第1章 網絡系統結構與設計的基本原則1.1 基礎知識1.2 實訓任務習題第2章 中小型網絡系統總體規劃與設計方法2.1 基礎知識2.2 實訓任務習題第3章 IP地址規劃設計技術3.1 基礎知識3.2 實訓任務習題第4章 路由設計基礎4.1 基礎知識4.2 實訓任務習題第…

subline Text3 插件安裝

--沒有解決&#xff0c;換了vscode 安裝Package Control 這是必須的步驟&#xff0c;安裝任何插件之前需要安裝這個 自動安裝的方法最方便&#xff0c;只需要在控制臺&#xff08;不是win的控制臺&#xff0c;而是subline 的&#xff09;里粘貼一段代碼就好&#xff0c;但是由…

大學計算機基礎書本里的畢業論文源稿,計算機基礎畢業論文范文

計算機基礎畢業論文范文導語&#xff1a;關于大學計算機基礎的教學&#xff0c;需要不斷探索與實踐&#xff0c;實現更好的教學。下面是小編帶來的計算機基礎畢業論文&#xff0c;歡迎閱讀與參考。論文&#xff1a;大學計算機基礎教學的探索與實踐摘要&#xff1a;大學計算機基…

p批處理替換目錄下文本中的字符串

echo offrem 進入批處理文件所在的路徑 cd C:\Users\zxh\Desktop\123echo ***** Replace "123" as "abc" ***** rem 定義要替換的新舊字符串 set strOld123 set strNewabcrem 定義變量修改本地化延期 setlocal enabledelayedexpansionrem 循環取出要處理的…