From 2a4209553f8b1c73a84ddb01197538127c3bd57d Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Wed, 5 Aug 2015 16:18:15 -0700 Subject: [PATCH] Support horizontal scrolling in ScrollableViewport --- packages/flutter/lib/rendering/box.dart | 6 ++-- packages/flutter/lib/widgets/basic.dart | 18 +++++++---- packages/flutter/lib/widgets/scrollable.dart | 34 +++++++++++++------- packages/flutter/lib/widgets/tabs.dart | 2 +- 4 files changed, 39 insertions(+), 21 deletions(-) diff --git a/packages/flutter/lib/rendering/box.dart b/packages/flutter/lib/rendering/box.dart index b24b1a03aa..abac4aeeb1 100644 --- a/packages/flutter/lib/rendering/box.dart +++ b/packages/flutter/lib/rendering/box.dart @@ -1128,10 +1128,10 @@ class RenderViewport extends RenderBox with RenderObjectWithChildMixin new RenderViewport(scrollOffset: new Offset(0.0, offset)); + RenderViewport createNode() => new RenderViewport(scrollOffset: scrollOffset, scrollDirection: scrollDirection); RenderViewport get root => super.root; void syncRenderObject(Viewport old) { super.syncRenderObject(old); - root.scrollOffset = new Offset(0.0, offset); + root.scrollOffset = scrollOffset; + root.scrollDirection = scrollDirection; } } diff --git a/packages/flutter/lib/widgets/scrollable.dart b/packages/flutter/lib/widgets/scrollable.dart index b8c9d1fe2d..2a8340ecaa 100644 --- a/packages/flutter/lib/widgets/scrollable.dart +++ b/packages/flutter/lib/widgets/scrollable.dart @@ -31,18 +31,19 @@ abstract class ScrollClient { bool ancestorScrolled(Scrollable ancestor); } -enum ScrollDirection { vertical, horizontal } - /// A base class for scrollable widgets that reacts to user input and generates /// a scrollOffset. abstract class Scrollable extends StatefulComponent { Scrollable({ Key key, - this.direction: ScrollDirection.vertical - }) : super(key: key); + this.scrollDirection: ViewportScrollDirection.vertical + }) : super(key: key) { + assert(scrollDirection == ViewportScrollDirection.vertical || + scrollDirection == ViewportScrollDirection.horizontal); + } - ScrollDirection direction; + ViewportScrollDirection scrollDirection; AnimatedSimulation _toEndAnimation; // See _startToEndAnimation() AnimationPerformance _toOffsetAnimation; // Started by scrollTo(offset, duration: d) @@ -57,12 +58,18 @@ abstract class Scrollable extends StatefulComponent { } void syncFields(Scrollable source) { - direction == source.direction; + scrollDirection == source.scrollDirection; } double _scrollOffset = 0.0; double get scrollOffset => _scrollOffset; + Offset get scrollOffsetVector { + if (scrollDirection == ViewportScrollDirection.horizontal) + return new Offset(scrollOffset, 0.0); + return new Offset(0.0, scrollOffset); + } + ScrollBehavior _scrollBehavior; ScrollBehavior createScrollBehavior(); ScrollBehavior get scrollBehavior { @@ -188,12 +195,12 @@ abstract class Scrollable extends StatefulComponent { } EventDisposition _handleScrollUpdate(sky.GestureEvent event) { - scrollBy(direction == ScrollDirection.horizontal ? event.dx : -event.dy); + scrollBy(scrollDirection == ViewportScrollDirection.horizontal ? event.dx : -event.dy); return EventDisposition.processed; } EventDisposition _handleFlingStart(sky.GestureEvent event) { - double eventVelocity = direction == ScrollDirection.horizontal + double eventVelocity = scrollDirection == ViewportScrollDirection.horizontal ? -event.velocityX : -event.velocityY; _startToEndAnimation(velocity: _velocityForFlingGesture(eventVelocity)); @@ -225,7 +232,11 @@ abstract class Scrollable extends StatefulComponent { /// A simple scrollable widget that has a single child. Use this component if /// you are not worried about offscreen widgets consuming resources. class ScrollableViewport extends Scrollable { - ScrollableViewport({ Key key, this.child }) : super(key: key); + ScrollableViewport({ + Key key, + this.child, + ViewportScrollDirection scrollDirection: ViewportScrollDirection.vertical + }) : super(key: key, scrollDirection: scrollDirection); Widget child; @@ -258,7 +269,8 @@ class ScrollableViewport extends Scrollable { return new SizeObserver( callback: _handleViewportSizeChanged, child: new Viewport( - offset: scrollOffset, + scrollOffset: scrollOffsetVector, + scrollDirection: scrollDirection, child: new SizeObserver( callback: _handleChildSizeChanged, child: child @@ -370,7 +382,7 @@ abstract class FixedHeightScrollable extends Scrollable { return new SizeObserver( callback: _handleSizeChanged, child: new Viewport( - offset: offsetY, + scrollOffset: new Offset(0.0, offsetY), child: new Container( padding: padding, child: new Block(items) diff --git a/packages/flutter/lib/widgets/tabs.dart b/packages/flutter/lib/widgets/tabs.dart index 6388343774..974723a861 100644 --- a/packages/flutter/lib/widgets/tabs.dart +++ b/packages/flutter/lib/widgets/tabs.dart @@ -396,7 +396,7 @@ class TabBar extends Scrollable { this.selectedIndex: 0, this.onChanged, this.isScrollable: false - }) : super(key: key, direction: ScrollDirection.horizontal); + }) : super(key: key, scrollDirection: ViewportScrollDirection.horizontal); Iterable labels; int selectedIndex;