From d95821c1d197f9892e210c3f5300cc95f0c294dd Mon Sep 17 00:00:00 2001 From: nick9822 Date: Wed, 25 Sep 2024 03:42:32 +0530 Subject: [PATCH] fixed keyboardDismissBehavior on scroll without a drag (#154675) fixes #154515, #150048 and other similar issues where user non-draggable scrolls (mouse wheel, two-fingers) should behave same as draggable ones. In this PR, scrollUpdateNotification.dragDetails check is removed and it has a supporting test which simulates a scroll which does not produce a drag. --- .../src/widgets/single_child_scroll_view.dart | 2 +- .../single_child_scroll_view_test.dart | 45 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/widgets/single_child_scroll_view.dart b/packages/flutter/lib/src/widgets/single_child_scroll_view.dart index 06b09d3ea8..2e17f43c24 100644 --- a/packages/flutter/lib/src/widgets/single_child_scroll_view.dart +++ b/packages/flutter/lib/src/widgets/single_child_scroll_view.dart @@ -276,7 +276,7 @@ class SingleChildScrollView extends StatelessWidget { child: scrollable, onNotification: (ScrollUpdateNotification notification) { final FocusScopeNode currentScope = FocusScope.of(context); - if (notification.dragDetails != null && !currentScope.hasPrimaryFocus && currentScope.hasFocus) { + if (!currentScope.hasPrimaryFocus && currentScope.hasFocus) { FocusManager.instance.primaryFocus?.unfocus(); } return false; diff --git a/packages/flutter/test/widgets/single_child_scroll_view_test.dart b/packages/flutter/test/widgets/single_child_scroll_view_test.dart index aeba59889b..d84f52fd6e 100644 --- a/packages/flutter/test/widgets/single_child_scroll_view_test.dart +++ b/packages/flutter/test/widgets/single_child_scroll_view_test.dart @@ -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'; + import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter_test/flutter_test.dart'; @@ -1118,4 +1120,47 @@ void main() { expect(tester.testTextInput.isVisible, isFalse); }); + + testWidgets('keyboardDismissBehavior on scroll without a drag test', (WidgetTester tester) async { + await tester.pumpWidget(MaterialApp( + home: Scaffold( + body: SizedBox( + height: 1000, + child: SingleChildScrollView( + keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag, + child: Column( + children: [ + Autocomplete( + optionsBuilder: (TextEditingValue textEditingValue) { + return['aardvark', 'bobcat', 'chameleon'] + .where((String option) { + return option.contains(textEditingValue.text.toLowerCase()); + }); + }, + ), + const SizedBox(height: 2000), + ], + ), + ), + ), + ), + )); + + await tester.tap(find.byType(Autocomplete)); + await tester.pump(); + + await tester.enterText(find.byType(RawAutocomplete),'aard'); + await tester.pump(); + + expect(find.text('aardvark'), findsOneWidget); + + final TestPointer testPointer = TestPointer(1, PointerDeviceKind.mouse); + final Offset scrollStart = tester.getCenter(find.byType(SingleChildScrollView)); + + testPointer.hover(scrollStart); + await tester.sendEventToBinding(testPointer.scroll(Offset(scrollStart.dx, scrollStart.dy - 100))); + await tester.pumpAndSettle(); + + expect(find.text('aardvark'), findsNothing); + }); }