Search code examples
flutterflutter-getx

Flutter Getx: Not able to navigate back when calling Get.snackbar()


I am not able to navigate back whenever I call the Get.snackbar() method using Getx. Only after the snackbar disappears I am able to navigate back. Is there any way to change this behaviour to let the user navigate back even when the snackbar is being displayed? Below is the code:

 SnackbarController snackbar({
    required String title,
    required String message,
    Color? bgColor,
    Color? textColor,
  }) {
    return Get.snackbar(

      title,
      message,
       snackPosition: SnackPosition.TOP,
      backgroundColor: bgColor?? Colors.green,
      colorText: textColor??Apc.white,
      isDismissible: true,
      dismissDirection: DismissDirection.horizontal
              
    );
  }

Solution

  • I found the actual solution. I wrote the code to navigate back in the Popscope method using Get.back().Changing the navigate back statement from Get.back() to Get.back(closeOverlays: true) did the trick.By default the closeOverlays property is set to false.This property determines whether the method should close any overlays (like snackbars, dialogs, bottom sheets) that are displayed while navigating back.

    Navigator.pop(context) will also work.The difference between the two is that pop() method will just navigate back without closing the overlay while Get.back(closeOverlays: true) will close all overlays and navigate back.