Skip to content

Commit

Permalink
Expose barrierDismissible in PageRoute constructor (#133659)
Browse files Browse the repository at this point in the history
## Description

This PR exposes `barrierDismissible` in `PageRoute`, `MaterialPageRoute` and `CupertinoPageRoute` constructors.
Setting this property to true will enable the escape key binding.

## Related Issue

Fixes flutter/flutter#132138.

## Tests

Adds one test.
  • Loading branch information
bleroux authored Aug 30, 2023
1 parent 15048a6 commit b231052
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
4 changes: 4 additions & 0 deletions packages/flutter/lib/src/cupertino/route.dart
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,9 @@ mixin CupertinoRouteTransitionMixin<T> on PageRoute<T> {
/// the route is popped from the stack via [Navigator.pop] when an optional
/// `result` can be provided.
///
/// If `barrierDismissible` is true, then pressing the escape key on the keyboard
/// will cause the current route to be popped with null as the value.
///
/// See also:
///
/// * [CupertinoRouteTransitionMixin], for a mixin that provides iOS transition
Expand All @@ -334,6 +337,7 @@ class CupertinoPageRoute<T> extends PageRoute<T> with CupertinoRouteTransitionMi
this.maintainState = true,
super.fullscreenDialog,
super.allowSnapshotting = true,
super.barrierDismissible = false,
}) {
assert(opaque);
}
Expand Down
4 changes: 4 additions & 0 deletions packages/flutter/lib/src/material/page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import 'theme.dart';
/// fullscreen modal dialog. On iOS, those routes animate from the bottom to the
/// top rather than horizontally.
///
/// If `barrierDismissible` is true, then pressing the escape key on the keyboard
/// will cause the current route to be popped with null as the value.
///
/// The type `T` specifies the return type of the route which can be supplied as
/// the route is popped from the stack via [Navigator.pop] by providing the
/// optional `result` argument.
Expand All @@ -40,6 +43,7 @@ class MaterialPageRoute<T> extends PageRoute<T> with MaterialRouteTransitionMixi
this.maintainState = true,
super.fullscreenDialog,
super.allowSnapshotting = true,
super.barrierDismissible = false,
}) {
assert(opaque);
}
Expand Down
9 changes: 7 additions & 2 deletions packages/flutter/lib/src/widgets/pages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import 'routes.dart';
/// The [PageRouteBuilder] subclass provides a way to create a [PageRoute] using
/// callbacks rather than by defining a new class via subclassing.
///
/// If `barrierDismissible` is true, then pressing the escape key on the keyboard
/// will cause the current route to be popped with null as the value.
///
/// See also:
///
/// * [Route], which documents the meaning of the `T` generic type argument.
Expand All @@ -20,7 +23,8 @@ abstract class PageRoute<T> extends ModalRoute<T> {
super.settings,
this.fullscreenDialog = false,
this.allowSnapshotting = true,
});
bool barrierDismissible = false,
}) : _barrierDismissible = barrierDismissible;

/// {@template flutter.widgets.PageRoute.fullscreenDialog}
/// Whether this page route is a full-screen dialog.
Expand All @@ -39,7 +43,8 @@ abstract class PageRoute<T> extends ModalRoute<T> {
bool get opaque => true;

@override
bool get barrierDismissible => false;
bool get barrierDismissible => _barrierDismissible;
final bool _barrierDismissible;

@override
bool canTransitionTo(TransitionRoute<dynamic> nextRoute) => nextRoute is PageRoute;
Expand Down
40 changes: 40 additions & 0 deletions packages/flutter/test/material/page_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:flutter/cupertino.dart' show CupertinoPageRoute;
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';

void main() {
Expand Down Expand Up @@ -1229,6 +1230,45 @@ void main() {
expect(find.text('p1'), findsOneWidget);
expect(find.text('count: 1'), findsOneWidget);
});

testWidgets('MaterialPageRoute can be dismissed with escape keyboard shortcut', (WidgetTester tester) async {
// Regression test for https://github.com/flutter/flutter/issues/132138.
final GlobalKey scaffoldKey = GlobalKey();

await tester.pumpWidget(
MaterialApp(
home: Scaffold(
key: scaffoldKey,
body: Center(
child: ElevatedButton(
onPressed: () {
Navigator.push<void>(scaffoldKey.currentContext!, MaterialPageRoute<void>(
builder: (BuildContext context) {
return const Scaffold(
body: Center(child: Text('route')),
);
},
barrierDismissible: true,
));
},
child: const Text('push'),
),
),
),
),
);

await tester.tap(find.text('push'));
await tester.pumpAndSettle();
expect(find.text('route'), findsOneWidget);
expect(find.text('push'), findsNothing);

// Try to dismiss the route with the escape key.
await tester.sendKeyEvent(LogicalKeyboardKey.escape);
await tester.pumpAndSettle();

expect(find.text('route'), findsNothing);
});
}

class TransitionDetector extends DefaultTransitionDelegate<void> {
Expand Down

0 comments on commit b231052

Please sign in to comment.