9

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.

4
  • As the tap method taps the center of the widget, it might be a problem. Do you have an AppBar? In that case you might tap on it. Commented Jun 14, 2019 at 22:41
  • It looks like the only solution will be to add a close button to the dialog. Commented Jun 15, 2019 at 12:34
  • Any chance flutter_driver pageBack api.flutter.dev/flutter/flutter_driver/CommonFinders/… will work here? Commented Jun 24, 2019 at 13:21
  • Can you try by fetching height or width of dialog and then making use of deltas (dx, dy) of driver.scroll to tap ? For ex : if width of dialog is 100, use driver.scroll s dx parameter and pass dx value as more than 100. Commented Jul 1, 2019 at 10:09

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Doesn't work for me with Flutter 1.17.5. Which version are you using?
0

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

4 Comments

The barrierDismissible argument is used to determine whether this route can be dismissed by tapping the modal barrier. This argument defaults to true. If barrierDismissible is true, a non-null barrierLabel must be provided. Did you provide a barrierLabel? Showing your code also really helps.
The reason why you must provide a barrierLabel is for people using screen readers. If you don't provide a way for flutter to tell the screen reader that this is how to dismiss the modal then the user will not know how. Flutter enforces this.
You wrote "Hence, I am wondering how I would tap outside of the dialog in order to close it." I provided you with the correct answer. Your issue has nothing to do with the FlutterDriver. You want to close the dialog. You are correct that barrierDismissible is set to true by default. However it's not closing when you tap outside because you have not set a barrierLabel. This is the best answer I can give you without seeing the code. Why don't you give it a try and let me know.
My answer is true for both tests and manual input. If barrierDismissible is true, a non-null barrierLabel must be provided. Do you have a barrierLabel? If not your tests will fail. If your question is how to test for this behaviour you can edit your question and maybe get the answer you're looking for. Right now the only question I see is "I am wondering how I would tap outside of the dialog in order to close it." I was just trying to help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.