import 'dart:async'; import 'package:flutter/material.dart'; class Debouncer { final int milliseconds; VoidCallback? action; Timer? _timer; Debouncer({required this.milliseconds}); void run(VoidCallback action) { // 如果_timer不为空,我们取消之前的计时器 if (_timer != null) { _timer!.cancel(); } // 创建一个新的计时器 _timer = Timer(Duration(milliseconds: milliseconds), action); } }