diff --git a/packages/flutter/lib/src/widgets/homogeneous_viewport.dart b/packages/flutter/lib/src/widgets/homogeneous_viewport.dart index 3d9052af0e8..ac4caa4bf6e 100644 --- a/packages/flutter/lib/src/widgets/homogeneous_viewport.dart +++ b/packages/flutter/lib/src/widgets/homogeneous_viewport.dart @@ -256,7 +256,6 @@ class _HomogeneousPageViewportElement extends _ViewportBaseElement> extends ScrollableSta bool get snapScrollOffsetChanges => config.itemsSnapAlignment == ItemsSnapAlignment.item; double snapScrollOffset(double newScrollOffset) { - double previousItemOffset = newScrollOffset.floorToDouble(); - double nextItemOffset = newScrollOffset.ceilToDouble(); + final double previousItemOffset = newScrollOffset.floorToDouble(); + final double nextItemOffset = newScrollOffset.ceilToDouble(); return (newScrollOffset - previousItemOffset < 0.5 ? previousItemOffset : nextItemOffset) .clamp(scrollBehavior.minScrollOffset, scrollBehavior.maxScrollOffset); } Future _flingToAdjacentItem(Offset velocity) { - double scrollVelocity = scrollDirectionVelocity(velocity); - double newScrollOffset = snapScrollOffset(scrollOffset + scrollVelocity.sign) + final double scrollVelocity = scrollDirectionVelocity(velocity); + 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); @@ -177,9 +177,9 @@ class PageableListState> extends ScrollableSta } List buildItems(BuildContext context, int start, int count) { - List result = new List(); - int begin = config.itemsWrap ? start : math.max(0, start); - int end = config.itemsWrap ? begin + count : math.min(begin + count, config.items.length); + final List result = new List(); + final int begin = config.itemsWrap ? start : math.max(0, start); + final int end = config.itemsWrap ? begin + count : math.min(begin + count, itemCount); for (int i = begin; i < end; ++i) result.add(config.itemBuilder(context, config.items[i % itemCount], i)); assert(result.every((Widget item) => item.key != null)); diff --git a/packages/unit/test/widget/pageable_list_test.dart b/packages/unit/test/widget/pageable_list_test.dart index dff21ccc1a0..fc3bd713dc1 100644 --- a/packages/unit/test/widget/pageable_list_test.dart +++ b/packages/unit/test/widget/pageable_list_test.dart @@ -7,7 +7,7 @@ import 'package:flutter/widgets.dart'; import 'package:test/test.dart'; const Size pageSize = const Size(800.0, 600.0); -const List pages = const [0, 1, 2, 3, 4, 5]; +const List defaultPages = const [0, 1, 2, 3, 4, 5]; int currentPage = null; bool itemsWrap = false; @@ -20,7 +20,7 @@ Widget buildPage(BuildContext context, int page, int index) { ); } -Widget buildFrame() { +Widget buildFrame({ List pages: defaultPages }) { // The test framework forces the frame (and so the PageableList) // to be 800x600. The pageSize constant reflects this. return new PageableList( @@ -66,7 +66,6 @@ void main() { test('PageableList with itemsWrap: true', () { testWidgets((WidgetTester tester) { - tester.pumpWidget(new Container()); currentPage = null; itemsWrap = true; tester.pumpWidget(buildFrame()); @@ -79,4 +78,44 @@ void main() { expect(currentPage, equals(5)); }); }); + + test('PageableList with two items', () { + testWidgets((WidgetTester tester) { + currentPage = null; + itemsWrap = true; + tester.pumpWidget(buildFrame(pages: [0, 1])); + expect(currentPage, isNull); + pageLeft(tester); + expect(currentPage, equals(1)); + pageRight(tester); + expect(currentPage, equals(0)); + pageRight(tester); + expect(currentPage, equals(1)); + }); + }); + + test('PageableList with one item', () { + testWidgets((WidgetTester tester) { + currentPage = null; + itemsWrap = true; + tester.pumpWidget(buildFrame(pages: [0])); + expect(currentPage, isNull); + pageLeft(tester); + expect(currentPage, equals(0)); + pageRight(tester); + expect(currentPage, equals(0)); + pageRight(tester); + expect(currentPage, equals(0)); + }); + }); + + test('PageableList with no items', () { + testWidgets((WidgetTester tester) { + currentPage = null; + itemsWrap = true; + tester.pumpWidget(buildFrame(pages: null)); + expect(currentPage, isNull); + }); + }); + }