Fix ScrollPosition.isScrollingNotifier.value for pointer scrolling (#113972)

This commit is contained in:
Kate Lovett
2022-10-24 18:03:50 -05:00
committed by GitHub
parent 694b25355d
commit 806fb93c21
3 changed files with 34 additions and 3 deletions

View File

@@ -810,8 +810,6 @@ abstract class ScrollPosition extends ViewportOffset with ScrollMetrics {
///
/// This method is very similar to [jumpTo], but [pointerScroll] will
/// update the [ScrollDirection].
///
// TODO(YeungKC): Support trackpad scroll, https://github.com/flutter/flutter/issues/23604.
void pointerScroll(double delta);
/// Calls [jumpTo] if duration is null or [Duration.zero], otherwise

View File

@@ -222,8 +222,10 @@ class ScrollPositionWithSingleContext extends ScrollPosition implements ScrollAc
-delta > 0.0 ? ScrollDirection.forward : ScrollDirection.reverse,
);
final double oldPixels = pixels;
forcePixels(targetPixels);
// Set the notifier before calling force pixels.
// This is set to false again after going ballistic below.
isScrollingNotifier.value = true;
forcePixels(targetPixels);
didStartScroll();
didUpdateScrollPositionBy(pixels - oldPixels);
didEndScroll();

View File

@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:ui' as ui;
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
@@ -362,4 +364,33 @@ void main() {
expect(tester.getTopLeft(find.widgetWithText(SizedBox, 'Item 1')), Offset.zero);
});
testWidgets('isScrollingNotifier works with pointer scroll', (WidgetTester tester) async {
Widget buildFrame(ScrollController controller) {
return Directionality(
textDirection: TextDirection.ltr,
child: ListView(
controller: controller,
children: List<Widget>.generate(50, (int index) {
return SizedBox(height: 100.0, child: Text('Item $index'));
}).toList(),
),
);
}
bool isScrolling = false;
final ScrollController controller = ScrollController();
controller.addListener((){
isScrolling = controller.position.isScrollingNotifier.value;
});
await tester.pumpWidget(buildFrame(controller));
final Offset scrollEventLocation = tester.getCenter(find.byType(ListView));
final TestPointer testPointer = TestPointer(1, ui.PointerDeviceKind.mouse);
// Create a hover event so that |testPointer| has a location when generating the scroll.
testPointer.hover(scrollEventLocation);
await tester.sendEventToBinding(testPointer.scroll(const Offset(0.0, 300.0)));
// When the listener was notified, the value of the isScrollingNotifier
// should have been true
expect(isScrolling, isTrue);
});
}