From 00a38778d67d151ccac7c5a27bba76431e4e90c2 Mon Sep 17 00:00:00 2001 From: Jermaine McFarlane Date: Tue, 8 Oct 2024 16:10:11 +0000 Subject: [PATCH 1/7] feat(navigation): return result when closing overlay --- lib/get_navigation/src/extension_navigation.dart | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/get_navigation/src/extension_navigation.dart b/lib/get_navigation/src/extension_navigation.dart index 13718e4a..ea2b16f6 100644 --- a/lib/get_navigation/src/extension_navigation.dart +++ b/lib/get_navigation/src/extension_navigation.dart @@ -887,8 +887,12 @@ extension GetNavigationExt on GetInterface { } } - void closeOverlay({String? id}) { - searchDelegate(id).navigatorKey.currentState?.pop(); + /// Close the current overlay returning the [result], if provided + void closeOverlay({ + String? id, + T? result, + }) { + searchDelegate(id).navigatorKey.currentState?.pop(result); } void closeAllBottomSheets({ From 9dedf7a591bfb83ddcdc0d0f634547a1ecbcaf20 Mon Sep 17 00:00:00 2001 From: Jermaine McFarlane Date: Tue, 8 Oct 2024 16:14:19 +0000 Subject: [PATCH 2/7] feat(navigation): close dialog method --- lib/get_navigation/src/extension_navigation.dart | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/get_navigation/src/extension_navigation.dart b/lib/get_navigation/src/extension_navigation.dart index ea2b16f6..f2a8bd43 100644 --- a/lib/get_navigation/src/extension_navigation.dart +++ b/lib/get_navigation/src/extension_navigation.dart @@ -887,6 +887,12 @@ extension GetNavigationExt on GetInterface { } } + /// Close the currently open dialog, returning a [result], if provided + void closeDialog({ + String? id, + T? result, + }) => closeOverlay(id: id, result: result); + /// Close the current overlay returning the [result], if provided void closeOverlay({ String? id, From e00fe88b0f1644bd022143c985b336a37bdda06d Mon Sep 17 00:00:00 2001 From: Jermaine McFarlane Date: Tue, 8 Oct 2024 16:45:47 +0000 Subject: [PATCH 3/7] docs(route_management): add example of closing dialog with result --- documentation/en_US/route_management.md | 27 +++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/documentation/en_US/route_management.md b/documentation/en_US/route_management.md index 00ad35a0..2b5ff443 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. From a6bdcf86750d262dbfb1db8bc4faa22bbd1b29bb Mon Sep 17 00:00:00 2001 From: Jermaine Date: Tue, 8 Oct 2024 13:02:07 -0400 Subject: [PATCH 4/7] chore: cleanup pubspec --- pubspec.yaml | 37 ------------------------------------- 1 file changed, 37 deletions(-) diff --git a/pubspec.yaml b/pubspec.yaml index 2d12bd35..879d1e56 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 From fa25ce86bab5a46fad75729a238c0713d98471f2 Mon Sep 17 00:00:00 2001 From: Jermaine Date: Tue, 8 Oct 2024 13:31:09 -0400 Subject: [PATCH 5/7] test(navigation): update dialog tests --- test/navigation/dialog_test.dart | 57 ++++++++++++++++++++++++++++---- 1 file changed, 51 insertions(+), 6 deletions(-) diff --git a/test/navigation/dialog_test.dart b/test/navigation/dialog_test.dart index bf2db4cd..08796003 100644 --- a/test/navigation/dialog_test.dart +++ b/test/navigation/dialog_test.dart @@ -34,25 +34,70 @@ void main() { expect(find.byType(YourDialogWidget), findsOneWidget); }); - testWidgets("Get.dialog close test", (tester) async { + 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); + }); + }); + + testWidgets("Get.dialog close and return value test", (tester) async { await tester.pumpWidget( Wrapper(child: Container()), ); await tester.pump(); - Get.dialog(const YourDialogWidget()); + final result = Get.dialog(const YourDialogWidget()); await tester.pumpAndSettle(); expect(find.byType(YourDialogWidget), findsOneWidget); - // expect(Get.isDialogOpen, true); + expect(Get.isDialogOpen, true); - Get.backLegacy(); + 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(); + expect(Get.isDialogOpen, false); + await tester.pumpAndSettle(); }); } From d1a6745e7f79a6621cc97d9d6cfc50d7e854c4a8 Mon Sep 17 00:00:00 2001 From: Jermaine Date: Tue, 8 Oct 2024 16:32:34 -0400 Subject: [PATCH 6/7] refactor(navigation): close only dialogs with closeDialog --- lib/get_navigation/src/extension_navigation.dart | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/get_navigation/src/extension_navigation.dart b/lib/get_navigation/src/extension_navigation.dart index f2a8bd43..2e29749e 100644 --- a/lib/get_navigation/src/extension_navigation.dart +++ b/lib/get_navigation/src/extension_navigation.dart @@ -888,10 +888,12 @@ extension GetNavigationExt on GetInterface { } /// Close the currently open dialog, returning a [result], if provided - void closeDialog({ - String? id, - T? result, - }) => closeOverlay(id: id, result: result); + 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({ From ada3d539db73ee345c24289cc9d6efcc2fcf4fa6 Mon Sep 17 00:00:00 2001 From: Jermaine Date: Tue, 8 Oct 2024 16:47:37 -0400 Subject: [PATCH 7/7] test(dialog): closeDialog does not close bottom sheets --- test/navigation/dialog_test.dart | 57 ++++++++++++++++++++++---------- 1 file changed, 40 insertions(+), 17 deletions(-) diff --git a/test/navigation/dialog_test.dart b/test/navigation/dialog_test.dart index 08796003..b2de4924 100644 --- a/test/navigation/dialog_test.dart +++ b/test/navigation/dialog_test.dart @@ -74,30 +74,53 @@ void main() { }); }); - testWidgets("Get.dialog close and return value test", (tester) async { - await tester.pumpWidget( - Wrapper(child: Container()), - ); + group("Get.closeDialog", () { + testWidgets("Get.closeDialog - closes dialog and returns value", + (tester) async { + await tester.pumpWidget( + Wrapper(child: Container()), + ); - await tester.pump(); + await tester.pump(); - final result = Get.dialog(const YourDialogWidget()); - await tester.pumpAndSettle(); + final result = Get.dialog(const YourDialogWidget()); + await tester.pumpAndSettle(); - expect(find.byType(YourDialogWidget), findsOneWidget); - expect(Get.isDialogOpen, true); + expect(find.byType(YourDialogWidget), findsOneWidget); + expect(Get.isDialogOpen, true); - const dialogResult = "My dialog result"; + const dialogResult = "My dialog result"; - Get.closeDialog(result: dialogResult); - await tester.pumpAndSettle(); + Get.closeDialog(result: dialogResult); + await tester.pumpAndSettle(); - final returnedResult = await result; - expect(returnedResult, dialogResult); + final returnedResult = await result; + expect(returnedResult, dialogResult); - expect(find.byType(YourDialogWidget), findsNothing); - expect(Get.isDialogOpen, false); - await tester.pumpAndSettle(); + 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), findsOneWidget); + }); }); }