first commit
This commit is contained in:
50
lib/components/debouncer.dart
Normal file
50
lib/components/debouncer.dart
Normal file
@@ -0,0 +1,50 @@
|
||||
// Dart imports:
|
||||
import 'dart:async';
|
||||
|
||||
// Flutter imports:
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
///
|
||||
/// Компонент для выполнения отложенных операций
|
||||
///
|
||||
class Debouncer {
|
||||
///
|
||||
/// Компонент для выполнения отложенных операций
|
||||
///
|
||||
/// Пример использования
|
||||
///
|
||||
/// ```dart
|
||||
/// final Debouncer _searchDelayer = Debouncer(
|
||||
/// delay: const Duration(milliseconds: 300),
|
||||
/// );
|
||||
///
|
||||
/// Widget _buildButton(BuildContext) {
|
||||
/// return ElevatedButton(
|
||||
/// onPressed: () {
|
||||
/// _debouncer.run(() {
|
||||
/// debugPrint('Type your code here');
|
||||
/// });
|
||||
/// },
|
||||
/// );
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
Debouncer({
|
||||
required this.delay,
|
||||
});
|
||||
|
||||
/// Время через которое необходимо вызывать функцию
|
||||
final Duration delay;
|
||||
|
||||
/// Таймер
|
||||
Timer? _timer;
|
||||
|
||||
///
|
||||
/// Запуск отложенного события
|
||||
///
|
||||
void run(VoidCallback action) {
|
||||
_timer?.cancel();
|
||||
|
||||
_timer = Timer(delay, action);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user