From 406d8f7f1e4c795044e4e0a8ebddac9822183ed8 Mon Sep 17 00:00:00 2001 From: chunhtai <47866232+chunhtai@users.noreply.github.com> Date: Thu, 30 Jan 2020 09:35:59 -0800 Subject: [PATCH] Fix focus when popping pages (#49714) Fixes the focus handling when popping pages so that when multiple pages are popped, the focus doesn't try to focus the interstitial pages. --- packages/flutter/lib/src/widgets/routes.dart | 10 ++-- .../flutter/test/widgets/routes_test.dart | 46 +++++++++++++++++++ 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/packages/flutter/lib/src/widgets/routes.dart b/packages/flutter/lib/src/widgets/routes.dart index 5f46e74f84..d730dcedfb 100644 --- a/packages/flutter/lib/src/widgets/routes.dart +++ b/packages/flutter/lib/src/widgets/routes.dart @@ -677,10 +677,15 @@ class _ModalScopeState extends State<_ModalScope> { super.dispose(); } + bool get _shouldIgnoreFocusRequest { + return widget.route.animation?.status == AnimationStatus.reverse || + (widget.route.navigator?.userGestureInProgress ?? false); + } + // This should be called to wrap any changes to route.isCurrent, route.canPop, // and route.offstage. void _routeSetState(VoidCallback fn) { - if (widget.route.isCurrent) { + if (widget.route.isCurrent && !_shouldIgnoreFocusRequest) { widget.route.navigator.focusScopeNode.setFirstFocus(focusScopeNode); } setState(fn); @@ -713,8 +718,7 @@ class _ModalScopeState extends State<_ModalScope> { AnimatedBuilder( animation: widget.route.navigator?.userGestureInProgressNotifier ?? ValueNotifier(false), builder: (BuildContext context, Widget child) { - final bool ignoreEvents = widget.route.animation?.status == AnimationStatus.reverse || - (widget.route.navigator?.userGestureInProgress ?? false); + final bool ignoreEvents = _shouldIgnoreFocusRequest; focusScopeNode.canRequestFocus = !ignoreEvents; return IgnorePointer( ignoring: ignoreEvents, diff --git a/packages/flutter/test/widgets/routes_test.dart b/packages/flutter/test/widgets/routes_test.dart index 655190b45e..79baeca749 100644 --- a/packages/flutter/test/widgets/routes_test.dart +++ b/packages/flutter/test/widgets/routes_test.dart @@ -1159,6 +1159,52 @@ void main() { modalBarrierAnimation = tester.widget(animatedModalBarrier).color; expect(modalBarrierAnimation.value, Colors.black); }); + + testWidgets('focus traverse correct when pop mutiple page simultaneously', (WidgetTester tester) async { + // Regression test: https://github.com/flutter/flutter/issues/48903 + final GlobalKey navigatorKey = GlobalKey(); + await tester.pumpWidget(MaterialApp( + navigatorKey: navigatorKey, + home: const Text('dummy1'), + )); + final Element textOnPageOne = tester.element(find.text('dummy1')); + final FocusScopeNode focusNodeOnPageOne = FocusScope.of(textOnPageOne); + expect(focusNodeOnPageOne.hasFocus, isTrue); + + // Pushes one page. + navigatorKey.currentState.push( + MaterialPageRoute( + builder: (BuildContext context) => const Text('dummy2'), + ) + ); + await tester.pumpAndSettle(); + + final Element textOnPageTwo = tester.element(find.text('dummy2')); + final FocusScopeNode focusNodeOnPageTwo = FocusScope.of(textOnPageTwo); + // The focus should be on second page. + expect(focusNodeOnPageOne.hasFocus, isFalse); + expect(focusNodeOnPageTwo.hasFocus, isTrue); + + // Pushes another page. + navigatorKey.currentState.push( + MaterialPageRoute( + builder: (BuildContext context) => const Text('dummy3'), + ) + ); + await tester.pumpAndSettle(); + final Element textOnPageThree = tester.element(find.text('dummy3')); + final FocusScopeNode focusNodeOnPageThree = FocusScope.of(textOnPageThree); + // The focus should be on third page. + expect(focusNodeOnPageOne.hasFocus, isFalse); + expect(focusNodeOnPageTwo.hasFocus, isFalse); + expect(focusNodeOnPageThree.hasFocus, isTrue); + + // Pops two pages simultaneously. + navigatorKey.currentState.popUntil((Route route) => route.isFirst); + await tester.pumpAndSettle(); + // It should refocus page one after pops. + expect(focusNodeOnPageOne.hasFocus, isTrue); + }); }); }