【问题标题】:How to check if flutter toast already on screen?如何检查颤振吐司是否已经在屏幕上?
【发布时间】:2020-09-19 08:38:48
【问题描述】:

只要基于某些整数值计算满足正确条件,我想在我的应用程序上显示一条 toast 消息。经常遇到这种情况,我想在屏幕上显示一条 toast 消息。我为此选择了flutter toast 插件。但有时,即使在应用程序关闭后,该方法也会发送太多的 toast,toast 消息包含。所以我想知道屏幕上是否已经有一条吐司消息,如果是,请取消。

如何检查 Flutter Toast 是否已经出现在屏幕上?

或者,请向我推荐其他关于颤振的对话。

//check if exist then cancel. Or show.
Fluttertoast.showToast(
        msg: "This is condition met Toast",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
        timeInSecForIosWeb: 1,
        backgroundColor: Colors.red,
        textColor: Colors.white,
        fontSize: 16.0
    );

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    此插件无法做到这一点,因为它不支持它。但如果您对更现代的方法感兴趣,您可以使用这个库来显示小吃吧。 如果有必要,您可以将其自定义到看起来像 Toast 的程度,并且有一个 Get.isSnackbarOpen 回调可以满足您的需求。

    https://pub.dev/packages/get

    【讨论】:

    • 它是一个很棒的图书馆。但是我收到一些错误,例如The following NoSuchMethodError was thrown while handling a gesture: The method 'dependOnInheritedWidgetOfExactType' was called on null. Receiver: null Tried calling: dependOnInheritedWidgetOfExactType<_InheritedTheme>() ___ for code: Padding( padding: const EdgeInsets.symmetric(vertical: 16.0), child: RaisedButton( onPressed: () { Get.snackbar('Hi', 'i am a modern snackbar'); }, child: Text('test'))), ___
    • 将您的 MaterialApp 更改为 GetMaterialApp 之类的文档
    【解决方案2】:

    当你调用 toast 时,请取 static bool isShowToast 并将其设为 false

    if(!isShowToast){
     isShowToast=true;
     Fluttertoast.showToast(msg: value).then((value){
     isShowToast=false;
    });
    }
    

    【讨论】:

      【解决方案3】:
      【解决方案4】:

      您可以运行Fluttertoast.cancel() 来取消所有祝酒词。

      如果您希望一次只显示一个 toast,您可以在调用 showToast 之前立即运行它。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-26
        • 1970-01-01
        • 2021-12-27
        • 1970-01-01
        • 2020-11-08
        • 2021-06-20
        相关资源
        最近更新 更多