import 'package:flutter/cupertino.dart';
import 'package:yumi/ui_kit/components/sc_tts.dart';

///防抖封装
class SCDebounceWidget extends StatefulWidget {
  final Widget child;
  final VoidCallback onTap;
  final Duration debounceTime;
  final String? tips;

  const SCDebounceWidget({
    Key? key,
    required this.child,
    required this.onTap,
    this.tips,
    this.debounceTime = const Duration(milliseconds: 550),
  }) : super(key: key);

  @override
  _SCDebounceWidgetState createState() => _SCDebounceWidgetState();
}

class _SCDebounceWidgetState extends State<SCDebounceWidget> {
  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) {
            SCTts.show(widget.tips ?? "");
          }
        }
      },
    );
  }
}
