Support horizontal scrolling in ScrollableViewport
This commit is contained in:
@@ -1128,10 +1128,10 @@ class RenderViewport extends RenderBox with RenderObjectWithChildMixin<RenderBox
|
||||
RenderViewport({
|
||||
RenderBox child,
|
||||
Offset scrollOffset,
|
||||
ViewportScrollDirection direction: ViewportScrollDirection.vertical
|
||||
ViewportScrollDirection scrollDirection: ViewportScrollDirection.vertical
|
||||
}) : _scrollOffset = scrollOffset,
|
||||
_scrollDirection = direction {
|
||||
assert(_offsetIsSane(scrollOffset, direction));
|
||||
_scrollDirection = scrollDirection {
|
||||
assert(_offsetIsSane(scrollOffset, scrollDirection));
|
||||
this.child = child;
|
||||
}
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ import 'package:sky/widgets/default_text_style.dart';
|
||||
import 'package:sky/widgets/framework.dart';
|
||||
|
||||
export 'package:sky/base/hit_test.dart' show EventDisposition, combineEventDispositions;
|
||||
export 'package:sky/rendering/box.dart' show BackgroundImage, BoxConstraints, BoxDecoration, Border, BorderSide, EdgeDims;
|
||||
export 'package:sky/rendering/box.dart' show BackgroundImage, BoxConstraints, BoxDecoration, Border, BorderSide, EdgeDims, ViewportScrollDirection;
|
||||
export 'package:sky/rendering/flex.dart' show FlexDirection, FlexJustifyContent, FlexAlignItems;
|
||||
export 'package:sky/rendering/object.dart' show Point, Offset, Size, Rect, Color, Paint, Path;
|
||||
export 'package:sky/rendering/toggleable.dart' show ValueChanged;
|
||||
@@ -268,17 +268,23 @@ class Baseline extends OneChildRenderObjectWrapper {
|
||||
}
|
||||
|
||||
class Viewport extends OneChildRenderObjectWrapper {
|
||||
Viewport({ Key key, this.offset: 0.0, Widget child })
|
||||
: super(key: key, child: child);
|
||||
Viewport({
|
||||
Key key,
|
||||
this.scrollOffset: Offset.zero,
|
||||
this.scrollDirection: ViewportScrollDirection.vertical,
|
||||
Widget child
|
||||
}) : super(key: key, child: child);
|
||||
|
||||
final double offset;
|
||||
final Offset scrollOffset;
|
||||
final ViewportScrollDirection scrollDirection;
|
||||
|
||||
RenderViewport createNode() => 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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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<TabLabel> labels;
|
||||
int selectedIndex;
|
||||
|
||||
Reference in New Issue
Block a user