背景
使用CupertinoPicker选择一些选项后,再点击预览内容用SinglescrollView查看,UI会变得很卡很卡。
解决
换成 listview 卡顿消失
SingleScrollView源码
Widget scrollable = Scrollable(
dragStartBehavior: dragStartBehavior,
axisDirection: axisDirection,
controller: scrollController,
physics: physics,
restorationId: restorationId,
viewportBuilder: (BuildContext context, ViewportOffset offset) {
return _SingleChildViewport(
axisDirection: axisDirection,
offset: offset,
clipBehavior: clipBehavior,
child: contents,
);
},
);
// _SingleChildViewport 最终继承至
class _RenderSingleChildViewport extends RenderBox with RenderObjectWithChildMixin<RenderBox> implements RenderAbstractViewport {
ListView源码
@protected
Widget buildViewport(
BuildContext context,
ViewportOffset offset,
AxisDirection axisDirection,
List<Widget> slivers,
) {
// assert...
if (shrinkWrap) {
return ShrinkWrappingViewport(
axisDirection: axisDirection,
offset: offset,
slivers: slivers,
clipBehavior: clipBehavior,
);
}
return Viewport(
axisDirection: axisDirection,
offset: offset,
slivers: slivers,
cacheExtent: cacheExtent,
center: center,
anchor: anchor,
clipBehavior: clipBehavior,
);
}
// Viewport 最终继承至
class Viewport extends MultiChildRenderObjectWidget {
CupertinoPicker源码
ListWheelViewport(
diameterRatio: widget.diameterRatio,
perspective: widget.perspective,
offAxisFraction: widget.offAxisFraction,
useMagnifier: widget.useMagnifier,
magnification: widget.magnification,
overAndUnderCenterOpacity: widget.overAndUnderCenterOpacity,
itemExtent: widget.itemExtent,
squeeze: widget.squeeze,
renderChildrenOutsideViewport: widget.renderChildrenOutsideViewport,
offset: offset,
childDelegate: widget.childDelegate,
clipBehavior: widget.clipBehavior,
);
// ListWheelViewport 最终继承
class RenderListWheelViewport extends RenderBox with ContainerRenderObjectMixin<RenderBox, ListWheelParentData> implements RenderAbstractViewport {