cmdrootaccess / another-flushbar

A flexible widget for user notification. Customize your text, button, duration, animations and much more. For Android devs, it is made to replace Snackbars and Toasts.
https://pub.dev/packages/another_flushbar
MIT License
145 stars 89 forks source link

If it already shows a Flushbar don't show another #60

Closed TheHypnoo closed 2 years ago

TheHypnoo commented 2 years ago

Hi, I have a doubt. In this case, pressing a button shows a Flushbar, but if you press it several times or something like that, it shows more times, what would be the way to not show it?

silverhairs commented 2 years ago

I think this is not in the scope of the package; for this use case, you should implement debouncing in the function/event that fires the Flushbar.

silverhairs commented 2 years ago

I think this is not in the scope of the package; for this use case, you should implement debouncing in the function/event that fires the Flushbar.

@TheHypnoo Here is an example of a simple Debouncer utility class I always use.

import 'dart:async';

typedef VoidCallback = void Function();

void main() {
  final debouncer = Debouncer(delay:const Duration(milliseconds:300));
  for (int i = 0; i < 5; i++) {
    debouncer.run(()=>print(i)); // Will print only the last value of i -> 4.
  }
}

class Debouncer{
  Debouncer({required this.delay});
  final Duration delay;

  Timer? timer;

  /// Runs the function [action]. If this method is called multiple times in less 
  /// than [delay], only the last call will be executed.
  void run(VoidCallback action){
    if(timer != null && timer!.isActive){
      timer!.cancel();
    }
    timer = Timer(delay, action);
  }

  void dispose(){
    timer?.cancel();
  }
}