diff --git a/packages/flutter/lib/src/widgets/scrollable_helpers.dart b/packages/flutter/lib/src/widgets/scrollable_helpers.dart index 287536fe40..56cb64d55e 100644 --- a/packages/flutter/lib/src/widgets/scrollable_helpers.dart +++ b/packages/flutter/lib/src/widgets/scrollable_helpers.dart @@ -460,17 +460,18 @@ class ScrollAction extends ContextAction { return true; }()); - if (primaryScrollController.position.context.notificationContext == null - && Scrollable.maybeOf(primaryScrollController.position.context.notificationContext!) == null) { + final BuildContext? notificationContext = primaryScrollController.position.context.notificationContext; + if (notificationContext != null) { + state = Scrollable.maybeOf(notificationContext); + } + if (state == null) { return; } - state = Scrollable.maybeOf(primaryScrollController.position.context.notificationContext!); } - assert(state != null, '$ScrollAction was invoked on a context that has no scrollable parent'); - assert(state!.position.hasPixels, 'Scrollable must be laid out before it can be scrolled via a ScrollAction'); + assert(state.position.hasPixels, 'Scrollable must be laid out before it can be scrolled via a ScrollAction'); // Don't do anything if the user isn't allowed to scroll. - if (state!.resolvedPhysics != null && !state.resolvedPhysics!.shouldAcceptUserOffset(state.position)) { + if (state.resolvedPhysics != null && !state.resolvedPhysics!.shouldAcceptUserOffset(state.position)) { return; } final double increment = getDirectionalIncrement(state, intent); diff --git a/packages/flutter/test/widgets/scrollable_helpers_test.dart b/packages/flutter/test/widgets/scrollable_helpers_test.dart index e057a4292b..b110e44929 100644 --- a/packages/flutter/test/widgets/scrollable_helpers_test.dart +++ b/packages/flutter/test/widgets/scrollable_helpers_test.dart @@ -4,6 +4,7 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; +import 'package:flutter/rendering.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; @@ -11,6 +12,21 @@ final LogicalKeyboardKey modifierKey = defaultTargetPlatform == TargetPlatform.m ? LogicalKeyboardKey.metaLeft : LogicalKeyboardKey.controlLeft; +class _NoNotificationContextScrollable extends Scrollable { + const _NoNotificationContextScrollable({ + super.controller, + required super.viewportBuilder, + }); + + @override + ScrollableState createState() => _NoNotificationContextScrollableState(); +} + +class _NoNotificationContextScrollableState extends ScrollableState { + @override + BuildContext? get notificationContext => null; +} + void main() { group('ScrollableDetails', (){ test('copyWith / == / hashCode', () { @@ -590,4 +606,73 @@ void main() { expect(find.text('The cape as red as blood'), findsOneWidget); expect(find.text('The hair as yellow as corn'), findsOneWidget); }); + + // Regression test for https://github.com/flutter/flutter/issues/158063. + testWidgets('Invoking a ScrollAction when notificationContext is null does not cause an exception.', (WidgetTester tester) async { + const List keysWithModifier = [ + LogicalKeyboardKey.arrowDown, LogicalKeyboardKey.arrowUp, + ]; + const List keys = [ + ...keysWithModifier, + LogicalKeyboardKey.pageDown, LogicalKeyboardKey.pageUp, + ]; + final ScrollController controller = ScrollController(); + addTearDown(controller.dispose); + await tester.pumpWidget( + MaterialApp( + theme: ThemeData(platform: TargetPlatform.fuchsia), + home: PrimaryScrollController( + controller: controller, + child: Focus( + autofocus: true, + child: _NoNotificationContextScrollable( + controller: controller, + viewportBuilder: (BuildContext context, ViewportOffset offset) => Viewport( + offset: offset, + slivers: List.generate( + 20, + (int index) => SliverToBoxAdapter( + child: SizedBox( + key: ValueKey('Box $index'), + height: 50.0, + ), + ), + ), + ), + ), + ), + ), + ), + ); + + // Verify the initial scroll offset. + await tester.pumpAndSettle(); + expect(controller.position.pixels, equals(0.0)); + expect( + tester.getRect(find.byKey(const ValueKey('Box 0'), skipOffstage: false)), + equals(const Rect.fromLTRB(0.0, 0.0, 800.0, 50.0)), + ); + + for (final LogicalKeyboardKey key in keys) { + // The default web shortcuts do not use a modifier key for ScrollActions. + if (!kIsWeb && keysWithModifier.contains(key)) { + await tester.sendKeyDownEvent(modifierKey); + } + + await tester.sendKeyEvent(key); + expect(tester.takeException(), isNull); + + if (!kIsWeb && keysWithModifier.contains(key)) { + await tester.sendKeyUpEvent(modifierKey); + } + + // No scrollable is found, so the scroll position should not change. + await tester.pumpAndSettle(); + expect(controller.position.pixels, equals(0.0)); + expect( + tester.getRect(find.byKey(const ValueKey('Box 0'), skipOffstage: false)), + equals(const Rect.fromLTRB(0.0, 0.0, 800.0, 50.0)), + ); + } + }, variant: KeySimulatorTransitModeVariant.all()); }