diff --git a/packages/flutter/lib/src/widgets/actions.dart b/packages/flutter/lib/src/widgets/actions.dart index 4aa17159b7..ef99513b92 100644 --- a/packages/flutter/lib/src/widgets/actions.dart +++ b/packages/flutter/lib/src/widgets/actions.dart @@ -1153,3 +1153,21 @@ class SelectIntent extends Intent {} /// This is an abstract class that serves as a base class for actions that /// select something. It is not bound to any key by default. abstract class SelectAction extends Action {} + +/// An [Intent] that dismisses the currently focused widget. +/// +/// The [WidgetsApp.defaultShortcuts] binds this intent to the +/// [LogicalKeyboardKey.escape] and [LogicalKeyboardKey.gameButtonB] keys. +/// +/// See also: +/// - [ModalRoute] which listens for this intent to dismiss modal routes +/// (dialogs, pop-up menus, drawers, etc). +class DismissIntent extends Intent { + /// Creates a const [DismissIntent]. + const DismissIntent(); +} + +/// An action that dismisses the focused widget. +/// +/// This is an abstract class that serves as a base class for dismiss actions. +abstract class DismissAction extends Action {} diff --git a/packages/flutter/lib/src/widgets/app.dart b/packages/flutter/lib/src/widgets/app.dart index 52086f635b..33e9a6146f 100644 --- a/packages/flutter/lib/src/widgets/app.dart +++ b/packages/flutter/lib/src/widgets/app.dart @@ -852,6 +852,10 @@ class WidgetsApp extends StatefulWidget { LogicalKeySet(LogicalKeyboardKey.space): const ActivateIntent(), LogicalKeySet(LogicalKeyboardKey.gameButtonA): const ActivateIntent(), + // Dismissal + LogicalKeySet(LogicalKeyboardKey.escape): const DismissIntent(), + LogicalKeySet(LogicalKeyboardKey.gameButtonB): const ActivateIntent(), + // Keyboard traversal. LogicalKeySet(LogicalKeyboardKey.tab): const NextFocusIntent(), LogicalKeySet(LogicalKeyboardKey.shift, LogicalKeyboardKey.tab): const PreviousFocusIntent(), @@ -874,6 +878,9 @@ class WidgetsApp extends StatefulWidget { // Activation LogicalKeySet(LogicalKeyboardKey.space): const ActivateIntent(), + // Dismissal + LogicalKeySet(LogicalKeyboardKey.escape): const DismissIntent(), + // Keyboard traversal. LogicalKeySet(LogicalKeyboardKey.tab): const NextFocusIntent(), LogicalKeySet(LogicalKeyboardKey.shift, LogicalKeyboardKey.tab): const PreviousFocusIntent(), @@ -893,6 +900,9 @@ class WidgetsApp extends StatefulWidget { LogicalKeySet(LogicalKeyboardKey.enter): const ActivateIntent(), LogicalKeySet(LogicalKeyboardKey.space): const ActivateIntent(), + // Dismissal + LogicalKeySet(LogicalKeyboardKey.escape): const DismissIntent(), + // Keyboard traversal LogicalKeySet(LogicalKeyboardKey.tab): const NextFocusIntent(), LogicalKeySet(LogicalKeyboardKey.shift, LogicalKeyboardKey.tab): const PreviousFocusIntent(), diff --git a/packages/flutter/lib/src/widgets/routes.dart b/packages/flutter/lib/src/widgets/routes.dart index f9019614b2..5ffd75e786 100644 --- a/packages/flutter/lib/src/widgets/routes.dart +++ b/packages/flutter/lib/src/widgets/routes.dart @@ -10,6 +10,7 @@ import 'dart:ui' as ui; import 'package:flutter/foundation.dart'; import 'package:flutter/scheduler.dart'; +import 'actions.dart'; import 'basic.dart'; import 'focus_manager.dart'; import 'focus_scope.dart'; @@ -651,6 +652,13 @@ mixin LocalHistoryRoute on Route { } } +class _DismissModalAction extends DismissAction { + @override + Object invoke(DismissIntent intent) { + return Navigator.of(primaryFocus.context).maybePop(); + } +} + class _ModalScopeStatus extends InheritedWidget { const _ModalScopeStatus({ Key key, @@ -762,6 +770,10 @@ class _ModalScopeState extends State<_ModalScope> { setState(fn); } + static final Map> _actionMap = >{ + DismissIntent: _DismissModalAction(), + }; + @override Widget build(BuildContext context) { return _ModalScopeStatus( @@ -772,44 +784,47 @@ class _ModalScopeState extends State<_ModalScope> { offstage: widget.route.offstage, // _routeSetState is called if this updates child: PageStorage( bucket: widget.route._storageBucket, // immutable - child: FocusScope( - node: focusScopeNode, // immutable - child: RepaintBoundary( - child: AnimatedBuilder( - animation: _listenable, // immutable - builder: (BuildContext context, Widget child) { - return widget.route.buildTransitions( - context, - widget.route.animation, - widget.route.secondaryAnimation, - // This additional AnimatedBuilder is include because if the - // value of the userGestureInProgressNotifier changes, it's - // only necessary to rebuild the IgnorePointer widget and set - // the focus node's ability to focus. - AnimatedBuilder( - animation: widget.route.navigator?.userGestureInProgressNotifier ?? ValueNotifier(false), - builder: (BuildContext context, Widget child) { - final bool ignoreEvents = _shouldIgnoreFocusRequest; - focusScopeNode.canRequestFocus = !ignoreEvents; - return IgnorePointer( - ignoring: ignoreEvents, - child: child, + child: Actions( + actions: _actionMap, + child: FocusScope( + node: focusScopeNode, // immutable + child: RepaintBoundary( + child: AnimatedBuilder( + animation: _listenable, // immutable + builder: (BuildContext context, Widget child) { + return widget.route.buildTransitions( + context, + widget.route.animation, + widget.route.secondaryAnimation, + // This additional AnimatedBuilder is include because if the + // value of the userGestureInProgressNotifier changes, it's + // only necessary to rebuild the IgnorePointer widget and set + // the focus node's ability to focus. + AnimatedBuilder( + animation: widget.route.navigator?.userGestureInProgressNotifier ?? ValueNotifier(false), + builder: (BuildContext context, Widget child) { + final bool ignoreEvents = _shouldIgnoreFocusRequest; + focusScopeNode.canRequestFocus = !ignoreEvents; + return IgnorePointer( + ignoring: ignoreEvents, + child: child, + ); + }, + child: child, + ), + ); + }, + child: _page ??= RepaintBoundary( + key: widget.route._subtreeKey, // immutable + child: Builder( + builder: (BuildContext context) { + return widget.route.buildPage( + context, + widget.route.animation, + widget.route.secondaryAnimation, ); }, - child: child, ), - ); - }, - child: _page ??= RepaintBoundary( - key: widget.route._subtreeKey, // immutable - child: Builder( - builder: (BuildContext context) { - return widget.route.buildPage( - context, - widget.route.animation, - widget.route.secondaryAnimation, - ); - }, ), ), ), diff --git a/packages/flutter/test/material/debug_test.dart b/packages/flutter/test/material/debug_test.dart index b2602ffdcc..0035f3b546 100644 --- a/packages/flutter/test/material/debug_test.dart +++ b/packages/flutter/test/material/debug_test.dart @@ -137,6 +137,8 @@ void main() { ' _FocusMarker\n' ' Semantics\n' ' FocusScope\n' + ' _ActionsMarker\n' + ' Actions\n' ' PageStorage\n' ' Offstage\n' ' _ModalScopeStatus\n' diff --git a/packages/flutter/test/widgets/routes_test.dart b/packages/flutter/test/widgets/routes_test.dart index fd31dcfa1e..3deb69d464 100644 --- a/packages/flutter/test/widgets/routes_test.dart +++ b/packages/flutter/test/widgets/routes_test.dart @@ -8,6 +8,7 @@ import 'dart:collection'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; +import 'package:flutter/services.dart'; import 'package:flutter/widgets.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:mockito/mockito.dart'; @@ -1319,6 +1320,28 @@ void main() { expect(tester.takeException(), null); }); }); + + testWidgets('can be dismissed with escape keyboard shortcut', (WidgetTester tester) async { + final GlobalKey navigatorKey = GlobalKey(); + await tester.pumpWidget(MaterialApp( + navigatorKey: navigatorKey, + home: const Text('dummy1'), + )); + final Element textOnPageOne = tester.element(find.text('dummy1')); + + // Show a simple dialog + showDialog( + context: textOnPageOne, + builder: (BuildContext context) => const Text('dialog1'), + ); + await tester.pumpAndSettle(); + expect(find.text('dialog1'), findsOneWidget); + + // Try to dismiss the dialog with the shortcut key + await tester.sendKeyEvent(LogicalKeyboardKey.escape); + await tester.pumpAndSettle(); + expect(find.text('dialog1'), findsNothing); + }); } double _getOpacity(GlobalKey key, WidgetTester tester) {