Unverified Commit 457e7b23 authored by xster's avatar xster Committed by GitHub

Fix FixedExtentScrollController.animateToItem (#20618)

parent 4aaa5dfe
...@@ -250,15 +250,15 @@ class FixedExtentScrollController extends ScrollController { ...@@ -250,15 +250,15 @@ class FixedExtentScrollController extends ScrollController {
/// The returned [Future] resolves when the animation completes. /// The returned [Future] resolves when the animation completes.
/// ///
/// The `duration` and `curve` arguments must not be null. /// The `duration` and `curve` arguments must not be null.
Future<Null> animateToItem(int itemIndex, { Future<void> animateToItem(int itemIndex, {
@required Duration duration, @required Duration duration,
@required Curve curve, @required Curve curve,
}) { }) async {
if (!hasClients) { if (!hasClients) {
return new Future<Null>.value(); return new Future<void>.value();
} }
final List<Future<Null>> futures = <Future<Null>>[]; final List<Future<void>> futures = <Future<void>>[];
for (_FixedExtentScrollPosition position in positions) { for (_FixedExtentScrollPosition position in positions) {
futures.add(position.animateTo( futures.add(position.animateTo(
itemIndex * position.itemExtent, itemIndex * position.itemExtent,
...@@ -266,7 +266,7 @@ class FixedExtentScrollController extends ScrollController { ...@@ -266,7 +266,7 @@ class FixedExtentScrollController extends ScrollController {
curve: curve, curve: curve,
)); ));
} }
return Future.wait(futures); return await Future.wait(futures);
} }
/// Changes which item index is centered in the controlled scroll view. /// Changes which item index is centered in the controlled scroll view.
......
...@@ -922,6 +922,43 @@ void main() { ...@@ -922,6 +922,43 @@ void main() {
expect(controller.selectedItem, 0); expect(controller.selectedItem, 0);
}); });
testWidgets('controller animateToItem', (WidgetTester tester) async {
final FixedExtentScrollController controller = new FixedExtentScrollController(initialItem: 10);
final List<int> paintedChildren = <int>[];
await tester.pumpWidget(
new Directionality(
textDirection: TextDirection.ltr,
child: new ListWheelScrollView(
controller: controller,
itemExtent: 100.0,
children: new List<Widget>.generate(100, (int index) {
return new CustomPaint(
painter: new TestCallbackPainter(onPaint: () {
paintedChildren.add(index);
}),
);
}),
),
),
);
// Screen is 600px tall. Item 10 is in the center and each item is 100px tall.
expect(paintedChildren, <int>[7, 8, 9, 10, 11, 12, 13]);
paintedChildren.clear();
controller.animateToItem(
0,
duration: const Duration(seconds: 1),
curve: Curves.linear,
);
await tester.pump();
await tester.pump(const Duration(seconds: 1));
expect(paintedChildren, <int>[0, 1, 2, 3]);
expect(controller.selectedItem, 0);
});
testWidgets('onSelectedItemChanged and controller are in sync', (WidgetTester tester) async { testWidgets('onSelectedItemChanged and controller are in sync', (WidgetTester tester) async {
final List<int> selectedItems = <int>[]; final List<int> selectedItems = <int>[];
final FixedExtentScrollController controller = new FixedExtentScrollController(initialItem: 10); final FixedExtentScrollController controller = new FixedExtentScrollController(initialItem: 10);
......
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