RawScrollbar: don't listen for drag gestures when scrolling is not possible (#149925)

Fixes https://github.com/flutter/flutter/issues/149803
This commit is contained in:
Hans Muller
2024-06-13 10:20:58 -07:00
committed by GitHub
parent 034966b3cc
commit 1b8dc90590
2 changed files with 53 additions and 1 deletions

View File

@@ -1953,9 +1953,17 @@ class RawScrollbarState<T extends RawScrollbar> extends State<T> with TickerProv
instance.dragStartBehavior = DragStartBehavior.down;
}
bool _canHandleScrollGestures() {
return enableGestures
&& _effectiveScrollController != null
&& _effectiveScrollController!.positions.length == 1
&& _effectiveScrollController!.position.hasContentDimensions
&& _effectiveScrollController!.position.maxScrollExtent > 0.0;
}
Map<Type, GestureRecognizerFactory> get _gestures {
final Map<Type, GestureRecognizerFactory> gestures = <Type, GestureRecognizerFactory>{};
if (_effectiveScrollController == null || !enableGestures) {
if (!_canHandleScrollGestures()) {
return gestures;
}

View File

@@ -5,6 +5,7 @@
import 'dart:ui' as ui;
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/src/physics/utils.dart' show nearEqual;
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
@@ -3212,4 +3213,47 @@ The provided ScrollController cannot be shared by multiple ScrollView widgets.''
},
variant: TargetPlatformVariant.all(),
);
testWidgets('Safe to drag trackpad when maxScrollExtent is 0 (scrollbar is not painted)', (WidgetTester tester) async {
// Regression test for https://github.com/flutter/flutter/issues/149803
final ScrollController scrollController = ScrollController();
addTearDown(scrollController.dispose);
Widget buildFrame(double sizedBoxHeight) {
return Directionality(
textDirection: TextDirection.ltr,
child: MediaQuery(
data: const MediaQueryData(),
child: RawScrollbar(
controller: scrollController,
child: SingleChildScrollView(
controller: scrollController,
child: SizedBox(width: 100.0, height: sizedBoxHeight),
),
),
),
);
}
await tester.pumpWidget(buildFrame(100)); // Test viewport has height=600
await tester.pumpAndSettle();
expect(scrollController.offset, 0.0);
expect(scrollController.position.maxScrollExtent, 0.0);
await tester.drag(find.byType(SingleChildScrollView), const Offset(0, -100), kind: PointerDeviceKind.trackpad);
await tester.pumpAndSettle();
expect(scrollController.offset, 0.0);
expect(scrollController.position.maxScrollExtent, 0.0);
await tester.pumpWidget(buildFrame(700));
await tester.pumpAndSettle();
expect(scrollController.offset, 0.0);
expect(scrollController.position.maxScrollExtent, 100.0);
await tester.drag(find.byType(SingleChildScrollView), const Offset(0, -100), kind: PointerDeviceKind.trackpad);
await tester.pumpAndSettle();
expect(scrollController.offset, 100.0);
expect(scrollController.position.maxScrollExtent, 100.0);
});
}