Commit 3c8cbef9 authored by Adam Barth's avatar Adam Barth

Rationalize RenderViewport and RenderVirtualViewport

These classes now share more code and have feature parity.
parent 70d96ecb
...@@ -771,7 +771,7 @@ class _TabBarState<T> extends ScrollableState<TabBar<T>> implements TabBarSelect ...@@ -771,7 +771,7 @@ class _TabBarState<T> extends ScrollableState<TabBar<T>> implements TabBarSelect
onSizeChanged: _handleViewportSizeChanged, onSizeChanged: _handleViewportSizeChanged,
child: new Viewport( child: new Viewport(
scrollDirection: Axis.horizontal, scrollDirection: Axis.horizontal,
scrollOffset: new Offset(scrollOffset, 0.0), paintOffset: scrollOffsetToPixelDelta(scrollOffset),
child: contents child: contents
) )
); );
......
...@@ -314,10 +314,12 @@ class RenderGrid extends RenderVirtualViewport<GridParentData> { ...@@ -314,10 +314,12 @@ class RenderGrid extends RenderVirtualViewport<GridParentData> {
int virtualChildBase: 0, int virtualChildBase: 0,
int virtualChildCount, int virtualChildCount,
Offset paintOffset: Offset.zero, Offset paintOffset: Offset.zero,
Painter overlayPainter,
LayoutCallback callback LayoutCallback callback
}) : _delegate = delegate, _virtualChildBase = virtualChildBase, super( }) : _delegate = delegate, _virtualChildBase = virtualChildBase, super(
virtualChildCount: virtualChildCount, virtualChildCount: virtualChildCount,
paintOffset: paintOffset, paintOffset: paintOffset,
overlayPainter: overlayPainter,
callback: callback callback: callback
) { ) {
assert(delegate != null); assert(delegate != null);
...@@ -338,6 +340,15 @@ class RenderGrid extends RenderVirtualViewport<GridParentData> { ...@@ -338,6 +340,15 @@ class RenderGrid extends RenderVirtualViewport<GridParentData> {
_delegate = newDelegate; _delegate = newDelegate;
} }
void set scrollDirection(Axis value) {
assert(() {
if (value != Axis.vertical)
throw new RenderingError('RenderGrid doesn\'t yet support horizontal scrolling.');
return true;
});
super.scrollDirection = value;
}
int get virtualChildCount => super.virtualChildCount ?? childCount; int get virtualChildCount => super.virtualChildCount ?? childCount;
/// The virtual index of the first child. /// The virtual index of the first child.
......
...@@ -11,7 +11,7 @@ import 'viewport.dart'; ...@@ -11,7 +11,7 @@ import 'viewport.dart';
/// Parent data for use with [RenderList]. /// Parent data for use with [RenderList].
class ListParentData extends ContainerBoxParentDataMixin<RenderBox> { } class ListParentData extends ContainerBoxParentDataMixin<RenderBox> { }
class RenderList extends RenderVirtualViewport<ListParentData> implements HasScrollDirection { class RenderList extends RenderVirtualViewport<ListParentData> {
RenderList({ RenderList({
List<RenderBox> children, List<RenderBox> children,
double itemExtent, double itemExtent,
...@@ -19,13 +19,15 @@ class RenderList extends RenderVirtualViewport<ListParentData> implements HasScr ...@@ -19,13 +19,15 @@ class RenderList extends RenderVirtualViewport<ListParentData> implements HasScr
int virtualChildCount, int virtualChildCount,
Offset paintOffset: Offset.zero, Offset paintOffset: Offset.zero,
Axis scrollDirection: Axis.vertical, Axis scrollDirection: Axis.vertical,
Painter overlayPainter,
LayoutCallback callback LayoutCallback callback
}) : _itemExtent = itemExtent, }) : _itemExtent = itemExtent,
_padding = padding, _padding = padding,
_scrollDirection = scrollDirection,
super( super(
virtualChildCount: virtualChildCount, virtualChildCount: virtualChildCount,
paintOffset: paintOffset, paintOffset: paintOffset,
scrollDirection: scrollDirection,
overlayPainter: overlayPainter,
callback: callback callback: callback
) { ) {
addAll(children); addAll(children);
...@@ -50,15 +52,6 @@ class RenderList extends RenderVirtualViewport<ListParentData> implements HasScr ...@@ -50,15 +52,6 @@ class RenderList extends RenderVirtualViewport<ListParentData> implements HasScr
markNeedsLayout(); markNeedsLayout();
} }
Axis get scrollDirection => _scrollDirection;
Axis _scrollDirection;
void set scrollDirection (Axis newValue) {
if (_scrollDirection == newValue)
return;
_scrollDirection = newValue;
markNeedsLayout();
}
void setupParentData(RenderBox child) { void setupParentData(RenderBox child) {
if (child.parentData is! ListParentData) if (child.parentData is! ListParentData)
child.parentData = new ListParentData(); child.parentData = new ListParentData();
......
...@@ -797,11 +797,12 @@ class Viewport extends OneChildRenderObjectWidget { ...@@ -797,11 +797,12 @@ class Viewport extends OneChildRenderObjectWidget {
Viewport({ Viewport({
Key key, Key key,
this.scrollDirection: Axis.vertical, this.scrollDirection: Axis.vertical,
this.scrollOffset: Offset.zero, this.paintOffset: Offset.zero,
this.overlayPainter,
Widget child Widget child
}) : super(key: key, child: child) { }) : super(key: key, child: child) {
assert(scrollDirection != null); assert(scrollDirection != null);
assert(scrollOffset != null); assert(paintOffset != null);
} }
/// The direction in which the child is permitted to be larger than the viewport /// The direction in which the child is permitted to be larger than the viewport
...@@ -814,14 +815,25 @@ class Viewport extends OneChildRenderObjectWidget { ...@@ -814,14 +815,25 @@ class Viewport extends OneChildRenderObjectWidget {
/// The offset at which to paint the child. /// The offset at which to paint the child.
/// ///
/// The offset can be non-zero only in the [scrollDirection]. /// The offset can be non-zero only in the [scrollDirection].
final Offset scrollOffset; final Offset paintOffset;
RenderViewport createRenderObject() => new RenderViewport(scrollDirection: scrollDirection, scrollOffset: scrollOffset); /// Paints an overlay over the viewport.
///
/// Often used to paint scroll bars.
final Painter overlayPainter;
RenderViewport createRenderObject() => new RenderViewport(
scrollDirection: scrollDirection,
paintOffset: paintOffset,
overlayPainter: overlayPainter
);
void updateRenderObject(RenderViewport renderObject, Viewport oldWidget) { void updateRenderObject(RenderViewport renderObject, Viewport oldWidget) {
// Order dependency: RenderViewport validates scrollOffset based on scrollDirection. // Order dependency: RenderViewport validates scrollOffset based on scrollDirection.
renderObject.scrollDirection = scrollDirection; renderObject
renderObject.scrollOffset = scrollOffset; ..scrollDirection = scrollDirection
..paintOffset = paintOffset
..overlayPainter = overlayPainter;
} }
} }
......
...@@ -467,15 +467,17 @@ class ScrollNotification extends Notification { ...@@ -467,15 +467,17 @@ class ScrollNotification extends Notification {
class ScrollableViewport extends Scrollable { class ScrollableViewport extends Scrollable {
ScrollableViewport({ ScrollableViewport({
Key key, Key key,
this.child,
double initialScrollOffset, double initialScrollOffset,
Axis scrollDirection: Axis.vertical, Axis scrollDirection: Axis.vertical,
ViewportAnchor scrollAnchor: ViewportAnchor.start,
ScrollListener onScrollStart, ScrollListener onScrollStart,
ScrollListener onScroll, ScrollListener onScroll,
ScrollListener onScrollEnd ScrollListener onScrollEnd,
this.child
}) : super( }) : super(
key: key, key: key,
scrollDirection: scrollDirection, scrollDirection: scrollDirection,
scrollAnchor: scrollAnchor,
initialScrollOffset: initialScrollOffset, initialScrollOffset: initialScrollOffset,
onScrollStart: onScrollStart, onScrollStart: onScrollStart,
onScroll: onScroll, onScroll: onScroll,
...@@ -514,18 +516,12 @@ class _ScrollableViewportState extends ScrollableState<ScrollableViewport> { ...@@ -514,18 +516,12 @@ class _ScrollableViewportState extends ScrollableState<ScrollableViewport> {
)); ));
} }
Offset get _scrollOffsetVector {
if (config.scrollDirection == Axis.horizontal)
return new Offset(scrollOffset, 0.0);
return new Offset(0.0, scrollOffset);
}
Widget buildContent(BuildContext context) { Widget buildContent(BuildContext context) {
return new SizeObserver( return new SizeObserver(
onSizeChanged: _handleViewportSizeChanged, onSizeChanged: _handleViewportSizeChanged,
child: new Viewport( child: new Viewport(
scrollOffset: _scrollOffsetVector,
scrollDirection: config.scrollDirection, scrollDirection: config.scrollDirection,
paintOffset: scrollOffsetToPixelDelta(scrollOffset),
child: new SizeObserver( child: new SizeObserver(
onSizeChanged: _handleChildSizeChanged, onSizeChanged: _handleChildSizeChanged,
child: config.child child: config.child
......
...@@ -24,7 +24,7 @@ void main() { ...@@ -24,7 +24,7 @@ void main() {
), ),
child: size); child: size);
RenderViewport viewport = new RenderViewport(child: red, scrollOffset: new Offset(0.0, -10.0)); RenderViewport viewport = new RenderViewport(child: red, paintOffset: new Offset(0.0, 10.0));
layout(viewport); layout(viewport);
HitTestResult result; HitTestResult result;
......
...@@ -298,6 +298,7 @@ class AnalyzeCommand extends FlutterCommand { ...@@ -298,6 +298,7 @@ class AnalyzeCommand extends FlutterCommand {
new RegExp(r'\[lint\] Prefer using lowerCamelCase for constant names.'), // sometimes we have no choice (e.g. when matching other platforms) new RegExp(r'\[lint\] Prefer using lowerCamelCase for constant names.'), // sometimes we have no choice (e.g. when matching other platforms)
new RegExp(r'\[lint\] Avoid defining a one-member abstract class when a simple function will do.'), // too many false-positives; code review should catch real instances new RegExp(r'\[lint\] Avoid defining a one-member abstract class when a simple function will do.'), // too many false-positives; code review should catch real instances
new RegExp(r'\[info\] TODO.+'), new RegExp(r'\[info\] TODO.+'),
new RegExp('\\[warning\\] Missing concrete implementation of \'RenderObject\\.applyPaintTransform\''), // https://github.com/dart-lang/sdk/issues/25232
new RegExp(r'[0-9]+ (error|warning|hint|lint).+found\.'), new RegExp(r'[0-9]+ (error|warning|hint|lint).+found\.'),
new RegExp(r'^$'), new RegExp(r'^$'),
]; ];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment