Ignore DismissIntent when barrier is not dismissible (#70156)
This commit is contained in:
parent
86f9ab511e
commit
f8d2f58b52
@ -650,9 +650,19 @@ mixin LocalHistoryRoute<T> on Route<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _DismissModalAction extends DismissAction {
|
class _DismissModalAction extends DismissAction {
|
||||||
|
_DismissModalAction(this.context);
|
||||||
|
|
||||||
|
final BuildContext context;
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool isEnabled(DismissIntent intent) {
|
||||||
|
final ModalRoute<dynamic> route = ModalRoute.of<dynamic>(context)!;
|
||||||
|
return route.barrierDismissible;
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Object invoke(DismissIntent intent) {
|
Object invoke(DismissIntent intent) {
|
||||||
return Navigator.of(primaryFocus!.context!)!.maybePop();
|
return Navigator.of(context)!.maybePop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -767,10 +777,6 @@ class _ModalScopeState<T> extends State<_ModalScope<T>> {
|
|||||||
setState(fn);
|
setState(fn);
|
||||||
}
|
}
|
||||||
|
|
||||||
static final Map<Type, Action<Intent>> _actionMap = <Type, Action<Intent>>{
|
|
||||||
DismissIntent: _DismissModalAction(),
|
|
||||||
};
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return AnimatedBuilder(
|
return AnimatedBuilder(
|
||||||
@ -790,8 +796,12 @@ class _ModalScopeState<T> extends State<_ModalScope<T>> {
|
|||||||
offstage: widget.route.offstage, // _routeSetState is called if this updates
|
offstage: widget.route.offstage, // _routeSetState is called if this updates
|
||||||
child: PageStorage(
|
child: PageStorage(
|
||||||
bucket: widget.route._storageBucket, // immutable
|
bucket: widget.route._storageBucket, // immutable
|
||||||
child: Actions(
|
child: Builder(
|
||||||
actions: _actionMap,
|
builder: (BuildContext context) {
|
||||||
|
return Actions(
|
||||||
|
actions: <Type, Action<Intent>>{
|
||||||
|
DismissIntent: _DismissModalAction(context),
|
||||||
|
},
|
||||||
child: FocusScope(
|
child: FocusScope(
|
||||||
node: focusScopeNode, // immutable
|
node: focusScopeNode, // immutable
|
||||||
child: RepaintBoundary(
|
child: RepaintBoundary(
|
||||||
@ -835,6 +845,8 @@ class _ModalScopeState<T> extends State<_ModalScope<T>> {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
@ -137,6 +137,7 @@ void main() {
|
|||||||
' FocusScope\n'
|
' FocusScope\n'
|
||||||
' _ActionsMarker\n'
|
' _ActionsMarker\n'
|
||||||
' Actions\n'
|
' Actions\n'
|
||||||
|
' Builder\n'
|
||||||
' PageStorage\n'
|
' PageStorage\n'
|
||||||
' Offstage\n'
|
' Offstage\n'
|
||||||
' _ModalScopeStatus\n'
|
' _ModalScopeStatus\n'
|
||||||
|
@ -1641,6 +1641,29 @@ void main() {
|
|||||||
expect(find.text('dialog1'), findsNothing);
|
expect(find.text('dialog1'), findsNothing);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
testWidgets('can not be dismissed with escape keyboard shortcut if barrier not dismissible', (WidgetTester tester) async {
|
||||||
|
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
|
||||||
|
await tester.pumpWidget(MaterialApp(
|
||||||
|
navigatorKey: navigatorKey,
|
||||||
|
home: const Text('dummy1'),
|
||||||
|
));
|
||||||
|
final Element textOnPageOne = tester.element(find.text('dummy1'));
|
||||||
|
|
||||||
|
// Show a simple dialog
|
||||||
|
showDialog<void>(
|
||||||
|
context: textOnPageOne,
|
||||||
|
barrierDismissible: false,
|
||||||
|
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'), findsOneWidget);
|
||||||
|
});
|
||||||
|
|
||||||
testWidgets('ModalRoute.of works for void routes', (WidgetTester tester) async {
|
testWidgets('ModalRoute.of works for void routes', (WidgetTester tester) async {
|
||||||
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
|
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
|
||||||
await tester.pumpWidget(MaterialApp(
|
await tester.pumpWidget(MaterialApp(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user