creativecreatorormaybenot
creativecreatorormaybenot

Reputation: 126574

How to close Dialog using FlutterDriver

Is there any way to close a dialog by "tapping it away", i.e. tapping outside of the content to close it with Flutter Driver?

My problem is that the dialog does not have any buttons that would close it. Instead the user is expected to either tap outside of it or use the back button. However, FlutterDriver does not have a "back" option.

Hence, I am wondering how I would tap outside of the dialog in order to close it.

Upvotes: 9

Views: 2213

Answers (2)

creativecreatorormaybenot
creativecreatorormaybenot

Reputation: 126574

The key that is commonly used for modals in Flutter is ModalBarrier, which is why the following should do the trick:

await driver.tap(find.byType(ModalBarrier));

This will work as long as barrierDismissible is set to true.
Essentially, when tapping away a dialog in Flutter, you are tapping on the modal barrier, which is why above code works.


Thanks to John Muchow for finding out.

Upvotes: 4

Patrick Kelly
Patrick Kelly

Reputation: 1009

You would want to set the barrierDismissible property of the dialog to true and add a barrierLabel.

This will allow you to tap outside and close the dialog

https://api.flutter.dev/flutter/widgets/showGeneralDialog.html

Upvotes: 0

Related Questions