skip to Main Content

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
              
    );
  }

2

Answers


  1. Chosen as BEST ANSWER

    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.


  2. Remove SnackbarController first and only create a function like this

    showSnackBar ({required String title, required String message, Color? bgColor, Color? textColor}){
      Get.snackbar(
          title,
          message,
          snackPosition: SnackPosition.TOP,
          backgroundColor: bgColor?? Colors.green,
          colorText: textColor??Apc.white,
          isDismissible: true,
          dismissDirection: DismissDirection.horizontal
    
      );
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search