|
2 | 2 | // Use of this source code is governed by a BSD-style license that can be
|
3 | 3 | // found in the LICENSE file.
|
4 | 4 |
|
| 5 | +import 'package:flutter/gestures.dart'; |
5 | 6 | import 'package:flutter/scheduler.dart';
|
6 | 7 | import 'package:flutter/widgets.dart';
|
7 | 8 | import 'package:flutter_test/flutter_test.dart';
|
@@ -103,6 +104,58 @@ void main() {
|
103 | 104 | reason: 'Expected an animation.',
|
104 | 105 | );
|
105 | 106 | });
|
| 107 | + |
| 108 | + testWidgets('HoldActivity can interrupt ScrollPosition.animateTo', (WidgetTester tester) async { |
| 109 | + const double animationExtent = 100.0; |
| 110 | + const double dragExtent = 30.0; |
| 111 | + final ScrollController controller = ScrollController(); |
| 112 | + addTearDown(controller.dispose); |
| 113 | + await tester.pumpWidget( |
| 114 | + Directionality( |
| 115 | + textDirection: TextDirection.ltr, |
| 116 | + child: NotificationListener<ScrollNotification>( |
| 117 | + onNotification: (ScrollNotification notification) { |
| 118 | + controller.position.animateTo( |
| 119 | + animationExtent, |
| 120 | + duration: const Duration(seconds: 1), |
| 121 | + curve: Curves.linear, |
| 122 | + ); |
| 123 | + return true; |
| 124 | + }, |
| 125 | + child: ListView( |
| 126 | + controller: controller, |
| 127 | + dragStartBehavior: DragStartBehavior.down, |
| 128 | + children: List<Widget>.generate( |
| 129 | + 80, |
| 130 | + (int i) => Text('$i', textDirection: TextDirection.ltr), |
| 131 | + ), |
| 132 | + ), |
| 133 | + ), |
| 134 | + ), |
| 135 | + ); |
| 136 | + |
| 137 | + expectNoAnimation(); |
| 138 | + |
| 139 | + // Drag to initiate the scroll animation. |
| 140 | + await tester.drag(find.byType(Scrollable), const Offset(0.0, 1.0)); |
| 141 | + await tester.pump(); |
| 142 | + |
| 143 | + // Pump to halfway through the animation. |
| 144 | + await tester.pump(const Duration(milliseconds: 500)); |
| 145 | + expect(controller.position.pixels, animationExtent / 2); |
| 146 | + |
| 147 | + // Interrupt the scroll animation. |
| 148 | + final TestGesture gesture = await tester.startGesture( |
| 149 | + tester.getCenter(find.byType(Scrollable)), |
| 150 | + ); |
| 151 | + await gesture.moveBy(const Offset(0.0, dragExtent)); |
| 152 | + await gesture.up(); |
| 153 | + |
| 154 | + await tester.pump(const Duration(milliseconds: 500)); |
| 155 | + |
| 156 | + // The drag stops the animation, and the drag extent is respected. |
| 157 | + expect(controller.position.pixels, (animationExtent / 2) - dragExtent); |
| 158 | + }); |
106 | 159 | }
|
107 | 160 |
|
108 | 161 | void expectNoAnimation() {
|
|
0 commit comments