Commit 76394630 authored by Adam Barth's avatar Adam Barth Committed by GitHub

Add barrierDismissable argument to showDialog (#8397)

In some situations, dialogs don't want the barrier to be dismissable.

Fixes #8354
parent b5928682
...@@ -316,9 +316,12 @@ class SimpleDialog extends StatelessWidget { ...@@ -316,9 +316,12 @@ class SimpleDialog extends StatelessWidget {
class _DialogRoute<T> extends PopupRoute<T> { class _DialogRoute<T> extends PopupRoute<T> {
_DialogRoute({ _DialogRoute({
this.child, @required this.theme,
this.theme, bool barrierDismissable: true,
}); @required this.child,
}) : _barrierDismissable = barrierDismissable {
assert(barrierDismissable != null);
}
final Widget child; final Widget child;
final ThemeData theme; final ThemeData theme;
...@@ -327,7 +330,8 @@ class _DialogRoute<T> extends PopupRoute<T> { ...@@ -327,7 +330,8 @@ class _DialogRoute<T> extends PopupRoute<T> {
Duration get transitionDuration => const Duration(milliseconds: 150); Duration get transitionDuration => const Duration(milliseconds: 150);
@override @override
bool get barrierDismissable => true; bool get barrierDismissable => _barrierDismissable;
final bool _barrierDismissable;
@override @override
Color get barrierColor => Colors.black54; Color get barrierColor => Colors.black54;
...@@ -364,10 +368,12 @@ class _DialogRoute<T> extends PopupRoute<T> { ...@@ -364,10 +368,12 @@ class _DialogRoute<T> extends PopupRoute<T> {
/// * <https://material.google.com/components/dialogs.html> /// * <https://material.google.com/components/dialogs.html>
Future<T> showDialog<T>({ Future<T> showDialog<T>({
@required BuildContext context, @required BuildContext context,
@required Widget child bool barrierDismissable: true,
@required Widget child,
}) { }) {
return Navigator.push(context, new _DialogRoute<T>( return Navigator.push(context, new _DialogRoute<T>(
child: child, child: child,
theme: Theme.of(context, shadowThemeOnly: true), theme: Theme.of(context, shadowThemeOnly: true),
barrierDismissable: barrierDismissable,
)); ));
} }
...@@ -137,4 +137,61 @@ void main() { ...@@ -137,4 +137,61 @@ void main() {
expect(await result, equals(42)); expect(await result, equals(42));
}); });
testWidgets('Barrier dismissable', (WidgetTester tester) async {
await tester.pumpWidget(
new MaterialApp(
home: new Material(
child: new Center(
child: new RaisedButton(
onPressed: null,
child: new Text('Go'),
),
),
),
),
);
BuildContext context = tester.element(find.text('Go'));
showDialog<Null>(
context: context,
child: new Container(
width: 100.0,
height: 100.0,
alignment: FractionalOffset.center,
child: new Text('Dialog1'),
),
);
await tester.pumpUntilNoTransientCallbacks(const Duration(seconds: 1));
expect(find.text('Dialog1'), findsOneWidget);
// Tap on the barrier.
await tester.tapAt(const Point(10.0, 10.0));
await tester.pumpUntilNoTransientCallbacks(const Duration(seconds: 1));
expect(find.text('Dialog1'), findsNothing);
showDialog<Null>(
context: context,
barrierDismissable: false,
child: new Container(
width: 100.0,
height: 100.0,
alignment: FractionalOffset.center,
child: new Text('Dialog2'),
),
);
await tester.pumpUntilNoTransientCallbacks(const Duration(seconds: 1));
expect(find.text('Dialog2'), findsOneWidget);
// Tap on the barrier, which shouldn't do anything this time.
await tester.tapAt(const Point(10.0, 10.0));
await tester.pumpUntilNoTransientCallbacks(const Duration(seconds: 1));
expect(find.text('Dialog2'), findsOneWidget);
});
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment