diff --git a/packages/flutter/lib/src/widgets/routes.dart b/packages/flutter/lib/src/widgets/routes.dart index d2fbaa7168..e7dcd7f2a2 100644 --- a/packages/flutter/lib/src/widgets/routes.dart +++ b/packages/flutter/lib/src/widgets/routes.dart @@ -1086,6 +1086,17 @@ abstract class ModalRoute extends TransitionRoute with LocalHistoryRoute nextRoute) { + super.didPopNext(nextRoute); + // When the route on top of this route is popped, resume focus onto the + // widget that received the last focus. + final _ModalScopeState modalScopeState = _scopeKey.currentState; + if (modalScopeState != null) { + modalScopeState.focusScopeNode.requestFocus(); + } + } + /// Enables this route to veto attempts by the user to dismiss it. /// /// This callback is typically added using a [WillPopScope] widget. That diff --git a/packages/flutter/test/material/text_field_focus_test.dart b/packages/flutter/test/material/text_field_focus_test.dart index 39a6a02a88..b2711abcec 100644 --- a/packages/flutter/test/material/text_field_focus_test.dart +++ b/packages/flutter/test/material/text_field_focus_test.dart @@ -40,11 +40,6 @@ void main() { Navigator.of(tester.element(find.text('Dialog'))).pop(); await tester.pump(); - expect(tester.testTextInput.isVisible, isFalse); - - await tester.tap(find.byType(TextField)); - await tester.idle(); - expect(tester.testTextInput.isVisible, isTrue); await tester.pumpWidget(Container()); diff --git a/packages/flutter/test/widgets/routes_test.dart b/packages/flutter/test/widgets/routes_test.dart index 0ffbbf720d..4d5492ec85 100644 --- a/packages/flutter/test/widgets/routes_test.dart +++ b/packages/flutter/test/widgets/routes_test.dart @@ -8,6 +8,7 @@ import 'package:flutter/foundation.dart'; import 'package:mockito/mockito.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:flutter/widgets.dart'; +import 'package:flutter/material.dart'; final List results = []; @@ -503,6 +504,52 @@ void main() { verifyNoMoreInteractions(pageRouteAware); }); }); + + group('ModalRoute', () { + testWidgets('should resume focus when the next route is popped', (WidgetTester tester) async { + // TODO(tongmu): It's currently using PageRoute (from MaterialApp) + // instead of ModalRoute, which might leave a risk but avoids building + // everything from scratch by using the existing setup by MaterialApp and + // TextField as a focusable widget. We might want to write a better test. + + final FocusNode node = FocusNode(); + BuildContext pageContext; + final Widget widget = MaterialApp( + theme: ThemeData(), + home: Material( + child: Builder( + builder: (BuildContext context) { + pageContext = context; + return Column( + children: [ + const TextField(), + TextField(focusNode: node), + ], + ); + } + ), + ), + ); + + await tester.pumpWidget(widget); + expect(node.hasFocus, false); + + node.requestFocus(); + await tester.pumpAndSettle(); + expect(node.hasFocus, true); + + showDialog( + context: pageContext, + builder: (BuildContext _) => const Center(), + ); + await tester.pumpAndSettle(); + expect(node.hasFocus, false); + + Navigator.of(pageContext).pop(); + await tester.pumpAndSettle(); + expect(node.hasFocus, true); + }); + }); } class MockPageRoute extends Mock implements PageRoute { }