Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return value from dialog #3235

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions documentation/en_US/route_management.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 14 additions & 2 deletions lib/get_navigation/src/extension_navigation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>({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<T>({
String? id,
T? result,
}) {
searchDelegate(id).navigatorKey.currentState?.pop(result);
}

void closeAllBottomSheets({
Expand Down
37 changes: 0 additions & 37 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
96 changes: 82 additions & 14 deletions test/navigation/dialog_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> 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<void> 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);
});
});
}

Expand Down
Loading