Skip to content

Instantly share code, notes, and snippets.

@slightfoot
Last active April 16, 2020 17:30
Show Gist options
  • Save slightfoot/a5395e793fd8d3f0676cdc8e91929904 to your computer and use it in GitHub Desktop.
Save slightfoot/a5395e793fd8d3f0676cdc8e91929904 to your computer and use it in GitHub Desktop.
PageView with CacheExtent - by Simon Lightfoot - 15/01/2020
// MIT License
//
// Copyright (c) 2020 Simon Lightfoot
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart' show ViewportOffset;
class PageViewWithCacheExtent extends StatelessWidget {
const PageViewWithCacheExtent({
Key key,
@required this.controller,
@required this.children,
this.onPageChanged,
this.cachedPages = 0,
this.physics,
}) : assert(controller != null),
assert(children != null),
super(key: key);
final PageController controller;
final ValueChanged<int> onPageChanged;
final List<Widget> children;
final int cachedPages;
final ScrollPhysics physics;
@override
Widget build(BuildContext context) {
return NotificationListener<ScrollNotification>(
onNotification: (ScrollNotification notification) {
if (notification.depth == 0 && notification is ScrollUpdateNotification) {
final PageMetrics metrics = notification.metrics as PageMetrics;
onPageChanged?.call(metrics.page.round());
}
return false;
},
child: LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
return Scrollable(
axisDirection: AxisDirection.right,
controller: controller,
physics: const PageScrollPhysics().applyTo(physics),
viewportBuilder: (BuildContext context, ViewportOffset position) {
final cacheExtent = constraints.maxWidth * controller.viewportFraction * cachedPages;
return Viewport(
axisDirection: AxisDirection.right,
offset: position,
cacheExtent: cacheExtent,
slivers: <Widget>[
SliverFillViewport(
viewportFraction: controller.viewportFraction,
delegate: SliverChildListDelegate(children),
),
],
);
},
);
},
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment