mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
Fix FixedExtentScrollController.animateToItem (#20618)
This commit is contained in:
parent
4aaa5dfe31
commit
457e7b23df
@ -250,15 +250,15 @@ class FixedExtentScrollController extends ScrollController {
|
||||
/// The returned [Future] resolves when the animation completes.
|
||||
///
|
||||
/// The `duration` and `curve` arguments must not be null.
|
||||
Future<Null> animateToItem(int itemIndex, {
|
||||
Future<void> animateToItem(int itemIndex, {
|
||||
@required Duration duration,
|
||||
@required Curve curve,
|
||||
}) {
|
||||
}) async {
|
||||
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) {
|
||||
futures.add(position.animateTo(
|
||||
itemIndex * position.itemExtent,
|
||||
@ -266,7 +266,7 @@ class FixedExtentScrollController extends ScrollController {
|
||||
curve: curve,
|
||||
));
|
||||
}
|
||||
return Future.wait(futures);
|
||||
return await Future.wait(futures);
|
||||
}
|
||||
|
||||
/// Changes which item index is centered in the controlled scroll view.
|
||||
|
||||
@ -922,6 +922,43 @@ void main() {
|
||||
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 {
|
||||
final List<int> selectedItems = <int>[];
|
||||
final FixedExtentScrollController controller = new FixedExtentScrollController(initialItem: 10);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user