【问题标题】:How to forcefully close GetX Snackbar?如何强制关闭 GetX Snackbar?
【发布时间】:2021-12-08 17:15:39
【问题描述】:

我使用Get.snackbar() 来展示连接API 的过程。 我不明白如何以编程方式关闭小吃店? 我有以下代码:

@override
  Future<List> getImportantData(String inputData) async {
    try {
      final token =  basicApiAuthString;
      print("requesting API with the following request: $inputData");
      Get.snackbar(
          "Requesting very important data...",
          "",
          duration: 60.seconds, // it could be any reasonable time, but I set it lo-o-ong
          snackPosition: SnackPosition.BOTTOM,
          showProgressIndicator: true,
          isDismissible: true,
          backgroundColor: Colors.lightGreen,
          colorText: Colors.white,
        );
      List importantData = await _client.requestAPI(basicAuthString: token, body: inputData);
          .then((importantData) {
              // Here I need to dismiss the snackbar I am still showing to user
              print("I want to close snackbar here but I don't know how to do that!");
              // Then I return data for future processing... 
              return importantData;
                });

      return importantData;
    } on DioError catch (error) {
      // Here I handle all possible API errors... Also I want to close snackbar here as well.
  
    }
 } // getImportantData() function.

我需要考虑以下几点:

  1. 应用程序可能在请求期间关闭并再次打开(因此小吃店可能已关闭,但我可以通过状态回调知道它(未显示)
  2. 用户在请求期间可能会关闭快餐栏
  3. 请求可能在几毫秒内完成
  4. 可能存在 API 错误,.then() 部分将永远不会被执行。

所以,我需要一些外部方法来关闭零食。 Navigator.of(context).hide... 不是我使用 GetX 的解决方案。

———— PS:这里是snackbar的定义,没有帮助我澄清:

https://pub.dev/documentation/get/3.4.2/route_manager/GetNavigation/snackbar.html

【问题讨论】:

    标签: android flutter dart flutter-getx snackbar


    【解决方案1】:
    Use Get.back() to close the snack bar.
    

    如果您希望用户关闭小吃店,您可以在 mainButton 或 onTap 中使用Get.back

       Get.snackbar(
          "Requesting very important data...",
          "",
          duration: 60.seconds, // it could be any reasonable time, but I set it lo-o-ong
          snackPosition: SnackPosition.BOTTOM,
          showProgressIndicator: true,
          isDismissible: true,
          backgroundColor: Colors.lightGreen,
          colorText: Colors.white,
          mainButton: TextButton(
                onPressed: Get.back,
                child: const Text(
                  "Close"
          )));
       );
    

    如果关闭时出现 debugLocked 问题,请使用 SchedulerBinding

     SchedulerBinding.instance!.addPostFrameCallback((_) {
          // Your Get Snackbar
     }):
    

    【讨论】:

    • 也可以试试Get.back(closeOverlays: true);
    【解决方案2】:

    使用该方法立即关闭当前的Snackbar

    ScaffoldMessenger.of(context).removeCurrentSnackBar();
    

    【讨论】:

    • 嗯...尝试了以下方法无济于事:final X = Get.context; 和 ScaffoldMessenger.of(X!).removeCurrentSnackBar(); 看起来在 GetX 环境中应该使用另一种方法。
    • 你必须传递构建上下文而不是“Get.context”
    【解决方案3】:

    GetX 已经有一个名为closeAllSnackbars() 和closeCurrentSnackbar() 的方法。

    所以使用closeAllSnackbars() 关闭所有打开的 Snackbars。

    Get.closeAllSnackbars();
    

    要关闭当前活动的 snakbar,只需调用 closeCurrentSnackbar()。

    Get.closeCurrentSnackbar();
    

    您还可以通过致电isSnackbarOpen() 来查看小吃店是否打开/活动的天气。

    Get.isSnackbarOpen();
    

    【讨论】:

      猜你喜欢
      • 2023-01-12
      • 2021-03-29
      • 2015-07-07
      • 2021-11-09
      • 2011-02-28
      • 1970-01-01
      • 1970-01-01
      • 2019-11-20
      • 2022-01-22
      相关资源
      最近更新 更多