Commit 70d96ecb authored by Adam Barth's avatar Adam Barth

Improve unit conversions in Scrollable

parent c91ace82
......@@ -922,12 +922,12 @@ class _TabBarViewState extends PageableListState<TabBarView> implements TabBarSe
controller.value = scrollOffset / 2.0;
}
Future fling(Offset scrollVelocity) {
Future fling(double scrollVelocity) {
if (_selection == null || _selection.valueIsChanging)
return new Future.value();
if (scrollVelocity.dx.abs() > _kMinFlingVelocity) {
final int selectionDelta = scrollVelocity.dx > 0 ? -1 : 1;
if (scrollVelocity.abs() > _kMinFlingVelocity) {
final int selectionDelta = scrollVelocity.sign.truncate();
final int targetIndex = (_selection.index + selectionDelta).clamp(0, _tabCount - 1);
_selection.value = _selection.values[targetIndex];
return new Future.value();
......
......@@ -9,6 +9,11 @@ import 'package:vector_math/vector_math_64.dart';
import 'box.dart';
import 'object.dart';
enum ViewportAnchor {
start,
end,
}
abstract class HasScrollDirection {
Axis get scrollDirection;
}
......
......@@ -5,7 +5,6 @@
import 'dart:async';
import 'package:flutter/animation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/rendering.dart';
import 'basic.dart';
......@@ -65,12 +64,25 @@ class PageableListState<T extends PageableList> extends ScrollableState<T> {
int get itemCount => config.children?.length ?? 0;
int _previousItemCount;
double pixelToScrollOffset(double value) {
double get _pixelsPerScrollUnit {
final RenderBox box = context.findRenderObject();
if (box == null || !box.hasSize)
return 0.0;
final double pixelScrollExtent = config.scrollDirection == Axis.vertical ? box.size.height : box.size.width;
return pixelScrollExtent == 0.0 ? 0.0 : value / pixelScrollExtent;
switch (config.scrollDirection) {
case Axis.horizontal:
return box.size.width;
case Axis.vertical:
return box.size.height;
}
}
double pixelOffsetToScrollOffset(double pixelOffset) {
final double pixelsPerScrollUnit = _pixelsPerScrollUnit;
return super.pixelOffsetToScrollOffset(pixelsPerScrollUnit == 0.0 ? 0.0 : pixelOffset / pixelsPerScrollUnit);
}
double scrollOffsetToPixelOffset(double scrollOffset) {
return super.scrollOffsetToPixelOffset(scrollOffset * _pixelsPerScrollUnit);
}
void initState() {
......@@ -143,7 +155,7 @@ class PageableListState<T extends PageableList> extends ScrollableState<T> {
ScrollBehavior createScrollBehavior() => scrollBehavior;
bool get snapScrollOffsetChanges => config.itemsSnapAlignment == ItemsSnapAlignment.item;
bool get shouldSnapScrollOffset => config.itemsSnapAlignment == ItemsSnapAlignment.item;
double snapScrollOffset(double newScrollOffset) {
final double previousItemOffset = newScrollOffset.floorToDouble();
......@@ -152,20 +164,19 @@ class PageableListState<T extends PageableList> extends ScrollableState<T> {
.clamp(scrollBehavior.minScrollOffset, scrollBehavior.maxScrollOffset);
}
Future _flingToAdjacentItem(Offset velocity) {
final double scrollVelocity = scrollDirectionVelocity(velocity);
Future _flingToAdjacentItem(double scrollVelocity) {
final double newScrollOffset = snapScrollOffset(scrollOffset + scrollVelocity.sign)
.clamp(snapScrollOffset(scrollOffset - 0.5), snapScrollOffset(scrollOffset + 0.5));
return scrollTo(newScrollOffset, duration: config.duration, curve: config.curve)
.then(_notifyPageChanged);
}
Future fling(Offset velocity) {
Future fling(double scrollVelocity) {
switch(config.itemsSnapAlignment) {
case ItemsSnapAlignment.adjacentItem:
return _flingToAdjacentItem(velocity);
return _flingToAdjacentItem(scrollVelocity);
default:
return super.fling(velocity).then(_notifyPageChanged);
return super.fling(scrollVelocity).then(_notifyPageChanged);
}
}
......
......@@ -48,14 +48,11 @@ void set scrollOffset(double value) {
}
Future fling(double velocity) {
Offset velocityOffset = scrollDirection == Axis.vertical
? new Offset(0.0, velocity)
: new Offset(velocity, 0.0);
return scrollableState.fling(velocityOffset);
return scrollableState.fling(velocity);
}
void main() {
test('ScrollableList snap scrolling, fling(-0.8)', () {
test('ScrollableList snap scrolling, fling(0.8)', () {
testWidgets((WidgetTester tester) {
tester.pumpWidget(buildFrame());
......@@ -65,7 +62,7 @@ void main() {
Duration dt = const Duration(seconds: 2);
fling(-0.8);
fling(0.8);
tester.pump(); // Start the scheduler at 0.0
tester.pump(dt);
expect(scrollOffset, closeTo(200.0, 1.0));
......@@ -74,7 +71,7 @@ void main() {
tester.pump();
expect(scrollOffset, 0.0);
fling(-2.0);
fling(2.0);
tester.pump();
tester.pump(dt);
expect(scrollOffset, closeTo(400.0, 1.0));
......@@ -83,7 +80,7 @@ void main() {
tester.pump();
expect(scrollOffset, 400.0);
fling(0.8);
fling(-0.8);
tester.pump();
tester.pump(dt);
expect(scrollOffset, closeTo(0.0, 1.0));
......@@ -92,7 +89,7 @@ void main() {
tester.pump();
expect(scrollOffset, 800.0);
fling(2.0);
fling(-2.0);
tester.pump();
tester.pump(dt);
expect(scrollOffset, closeTo(200.0, 1.0));
......@@ -102,7 +99,7 @@ void main() {
expect(scrollOffset, 800.0);
bool completed = false;
fling(2.0).then((_) {
fling(-2.0).then((_) {
completed = true;
expect(scrollOffset, closeTo(200.0, 1.0));
});
......
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