46 lines
1.1 KiB
Dart
46 lines
1.1 KiB
Dart
import 'package:flutter/cupertino.dart';
|
|
import 'package:aslan/chatvibe_ui/components/at_tts.dart';
|
|
|
|
///防抖封装
|
|
class ATDebounceWidget extends StatefulWidget {
|
|
final Widget child;
|
|
final VoidCallback onTap;
|
|
final Duration debounceTime;
|
|
final String? tips;
|
|
|
|
const ATDebounceWidget({
|
|
Key? key,
|
|
required this.child,
|
|
required this.onTap,
|
|
this.tips,
|
|
this.debounceTime = const Duration(milliseconds: 550),
|
|
}) : super(key: key);
|
|
|
|
@override
|
|
_ATDebounceWidgetState createState() => _ATDebounceWidgetState();
|
|
}
|
|
|
|
class _ATDebounceWidgetState extends State<ATDebounceWidget> {
|
|
DateTime? _lastClickTime;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return GestureDetector(
|
|
behavior: HitTestBehavior.opaque, // 确保透明区域也能点击
|
|
child: widget.child,
|
|
onTap: () {
|
|
DateTime now = DateTime.now();
|
|
if (_lastClickTime == null ||
|
|
now.difference(_lastClickTime!) > widget.debounceTime) {
|
|
_lastClickTime = now;
|
|
widget.onTap();
|
|
}else {
|
|
if (widget.tips != null) {
|
|
ATTts.show(widget.tips ?? "");
|
|
}
|
|
}
|
|
},
|
|
);
|
|
}
|
|
}
|