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 SnackBar
s are queued.
Instead of showing e.g. 3 SnackBar
s 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 SnackBar
s 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
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.
A simple work around. Whenever the user clicks the button clear the snackbar and launch a new one. You can use
removeCurrentSnackBar
to remove.