skip to Main Content

On a button tap I want to show a SnackBar for 5s.
If the user taps the button repeatedly in a short amount of time, multiple SnackBars are queued.
Instead of showing e.g. 3 SnackBars for 5s each, I want to reset the timer of the currently active SnackBar each time the button is tapped.
So if the user taps the button 3 times within 1s, I want to show one SnackBar for a total of 6s, instead of showing 3 SnackBars for a total of 15s.

I do not know how to manipulate the timer of a currently active SnackBar. The ScaffoldMessenger only provides methods such as removeCurrentSnackBar to dismiss the SnackBar and not to manipulate the SnackBar in other ways, as far as I understood.

How can I achieve the reset of the Snackbar timer?

Thanks in advance!

2

Answers


  1. To achieve the desired behavior of resetting the timer of the currently active SnackBar, you can create a custom solution using Flutter’s Timer class and a state management approach. Here’s how you can do it:

    Define a stateful widget:
    Create a stateful widget that holds the state of the currently active SnackBar and manages the timer.

    Use ScaffoldMessenger and SnackBar:
    Wrap your main widget with a Scaffold and use ScaffoldMessenger to show and hide the SnackBar.

    Create a custom SnackBar queue:
    Maintain a queue to keep track of the tapped events and manage the SnackBar display time.

    Login or Signup to reply.
  2. A simple work around. Whenever the user clicks the button clear the snackbar and launch a new one. You can use removeCurrentSnackBar to remove.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search