diff --git a/documentation/en_US/route_management.md b/documentation/en_US/route_management.md index 00ad35a03..2b5ff4438 100644 --- a/documentation/en_US/route_management.md +++ b/documentation/en_US/route_management.md @@ -488,6 +488,33 @@ Get.defaultDialog( ); ``` +To close the dialog and return a result use `Get.closeDialog` providing the `result` to return to the awaited `Get.dialog` call. +```dart +Widget buttonWithResult({ + required final String text, + required final bool result, +}) => TextButton( + onPressed: () { + Get.closeDialog(result: result); + }, + child: Text(text), + ); + +bool? delete = await Get.dialog( + AlertDialog( + content: const Text('Are you sure you would like to delete?'), + actions: [ + buttonWithResult(text: 'No', result: false), + buttonWithResult(text: 'Yes', result: true), + ], + ), + ); + +if (delete != null && delete) { + // Perform the deletion +} +``` + You can also use Get.generalDialog instead of showGeneralDialog. For all other Flutter dialog widgets, including cupertinos, you can use Get.overlayContext instead of context, and open it anywhere in your code. diff --git a/lib/get_navigation/src/extension_navigation.dart b/lib/get_navigation/src/extension_navigation.dart index 13718e4ac..2e29749ed 100644 --- a/lib/get_navigation/src/extension_navigation.dart +++ b/lib/get_navigation/src/extension_navigation.dart @@ -887,8 +887,20 @@ extension GetNavigationExt on GetInterface { } } - void closeOverlay({String? id}) { - searchDelegate(id).navigatorKey.currentState?.pop(); + /// Close the currently open dialog, returning a [result], if provided + void closeDialog({String? id, T? result}) { + // Stop if there is no dialog open + if (isDialogOpen == null || !isDialogOpen!) return; + + closeOverlay(id: id, result: result); + } + + /// Close the current overlay returning the [result], if provided + void closeOverlay({ + String? id, + T? result, + }) { + searchDelegate(id).navigatorKey.currentState?.pop(result); } void closeAllBottomSheets({ diff --git a/pubspec.yaml b/pubspec.yaml index 2d12bd35b..879d1e563 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -18,40 +18,3 @@ dev_dependencies: flutter_lints: ^4.0.0 flutter_test: sdk: flutter - -# For information on the generic Dart part of this file, see the -# following page: https://dart.dev/tools/pub/pubspec - -# The following section is specific to Flutter. -flutter: - - # To add assets to your package, add an assets section, like this: - # assets: - # - images/a_dot_burr.jpeg - # - images/a_dot_ham.jpeg - # - # For details regarding assets in packages, see - # https://flutter.dev/assets-and-images/#from-packages - # - # An image asset can refer to one or more resolution-specific "variants", see - # https://flutter.dev/assets-and-images/#resolution-aware. - - # To add custom fonts to your package, add a fonts section here, - # in this "flutter" section. Each entry in this list should have a - # "family" key with the font family name, and a "fonts" key with a - # list giving the asset and other descriptors for the font. For - # example: - # fonts: - # - family: Schyler - # fonts: - # - asset: fonts/Schyler-Regular.ttf - # - asset: fonts/Schyler-Italic.ttf - # style: italic - # - family: Trajan Pro - # fonts: - # - asset: fonts/TrajanPro.ttf - # - asset: fonts/TrajanPro_Bold.ttf - # weight: 700 - # - # For details regarding fonts in packages, see - # https://flutter.dev/custom-fonts/#from-packages diff --git a/test/navigation/dialog_test.dart b/test/navigation/dialog_test.dart index bf2db4cd9..b2de49244 100644 --- a/test/navigation/dialog_test.dart +++ b/test/navigation/dialog_test.dart @@ -34,25 +34,93 @@ void main() { expect(find.byType(YourDialogWidget), findsOneWidget); }); - testWidgets("Get.dialog close test", (tester) async { - await tester.pumpWidget( - Wrapper(child: Container()), - ); + group("Get dialog close tests", () { + /// Set up the test by opening a dialog and checking to ensure state is correct + Future setUpCloseTest(WidgetTester tester) async { + await tester.pumpWidget( + Wrapper(child: Container()), + ); + + await tester.pump(); + + Get.dialog(const YourDialogWidget()); + await tester.pumpAndSettle(); + + expect(find.byType(YourDialogWidget), findsOneWidget); + expect(Get.isDialogOpen, true); + } + + /// Tear down the test by checking after closing the dialog + Future tearDownCloseTest(WidgetTester tester) async { + await tester.pumpAndSettle(); + + expect(find.byType(YourDialogWidget), findsNothing); + expect(Get.isDialogOpen, false); + await tester.pumpAndSettle(); + } + + testWidgets("Get dialog close - with backLegacy", (tester) async { + await setUpCloseTest(tester); + // Close using backLegacy + Get.backLegacy(); + await tearDownCloseTest(tester); + }); + + testWidgets("Get dialog close - with closeDialog", (tester) async { + await setUpCloseTest(tester); + // Close using closeDialog + Get.closeDialog(); + await tearDownCloseTest(tester); + }); + }); - await tester.pump(); + group("Get.closeDialog", () { + testWidgets("Get.closeDialog - closes dialog and returns value", + (tester) async { + await tester.pumpWidget( + Wrapper(child: Container()), + ); - Get.dialog(const YourDialogWidget()); - await tester.pumpAndSettle(); + await tester.pump(); - expect(find.byType(YourDialogWidget), findsOneWidget); - // expect(Get.isDialogOpen, true); + final result = Get.dialog(const YourDialogWidget()); + await tester.pumpAndSettle(); - Get.backLegacy(); - await tester.pumpAndSettle(); + expect(find.byType(YourDialogWidget), findsOneWidget); + expect(Get.isDialogOpen, true); + + const dialogResult = "My dialog result"; + + Get.closeDialog(result: dialogResult); + await tester.pumpAndSettle(); + + final returnedResult = await result; + expect(returnedResult, dialogResult); + + expect(find.byType(YourDialogWidget), findsNothing); + expect(Get.isDialogOpen, false); + await tester.pumpAndSettle(); + }); + + testWidgets("Get.closeDialog - does not close bottomsheets", + (tester) async { + await tester.pumpWidget( + Wrapper(child: Container()), + ); + + await tester.pump(); + + Get.bottomSheet(const YourDialogWidget()); + await tester.pumpAndSettle(); + + expect(find.byType(YourDialogWidget), findsOneWidget); + expect(Get.isDialogOpen, false); + + Get.closeDialog(); + await tester.pumpAndSettle(); - expect(find.byType(YourDialogWidget), findsNothing); - // expect(Get.isDialogOpen, false); - // await tester.pumpAndSettle(); + expect(find.byType(YourDialogWidget), findsOneWidget); + }); }); }