auth_gesture_detector.dart 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import 'package:flutter/material.dart';
  2. import 'package:go_router/go_router.dart';
  3. import 'package:news_app/constant/config.dart';
  4. /// @author: bo.zeng
  5. /// @email: cnhbwds@gmail.com
  6. /// @date: 2025 2025/4/25 12:57
  7. /// @description:
  8. class AuthGestureDetector extends StatelessWidget {
  9. final Widget child;
  10. // === 原生 GestureDetector 参数 ===
  11. final GestureTapCallback? onTap;
  12. final GestureTapDownCallback? onTapDown;
  13. final GestureTapUpCallback? onTapUp;
  14. final GestureTapCancelCallback? onTapCancel;
  15. final GestureLongPressCallback? onLongPress;
  16. final HitTestBehavior? behavior;
  17. final bool excludeFromSemantics;
  18. // === 防抖设置 ===
  19. final Duration debounceDuration;
  20. // 用于保存最近点击时间(静态,全局防抖)
  21. static DateTime? _lastTapTime;
  22. const AuthGestureDetector({
  23. super.key,
  24. required this.child,
  25. this.onTap,
  26. this.onTapDown,
  27. this.onTapUp,
  28. this.onTapCancel,
  29. this.onLongPress,
  30. this.behavior,
  31. this.excludeFromSemantics = false,
  32. this.debounceDuration = const Duration(milliseconds: 500),
  33. });
  34. bool _canTapNow() {
  35. final now = DateTime.now();
  36. if (_lastTapTime == null ||
  37. now.difference(_lastTapTime!) > debounceDuration) {
  38. _lastTapTime = now;
  39. return true;
  40. }
  41. return false;
  42. }
  43. @override
  44. Widget build(BuildContext context) {
  45. return GestureDetector(
  46. behavior: behavior,
  47. excludeFromSemantics: excludeFromSemantics,
  48. onTap: () {
  49. if (!_canTapNow()) return;
  50. if (uuid.isNotEmpty) {
  51. onTap?.call();
  52. } else {
  53. context.push("/login");
  54. }
  55. },
  56. onTapDown: onTapDown,
  57. onTapUp: onTapUp,
  58. onTapCancel: onTapCancel,
  59. onLongPress: onLongPress,
  60. child: child,
  61. );
  62. }
  63. }