| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- import 'package:flutter/material.dart';
- import 'package:go_router/go_router.dart';
- import 'package:news_app/constant/config.dart';
- /// @author: bo.zeng
- /// @email: cnhbwds@gmail.com
- /// @date: 2025 2025/4/25 12:57
- /// @description:
- class AuthGestureDetector extends StatelessWidget {
- final Widget child;
- // === 原生 GestureDetector 参数 ===
- final GestureTapCallback? onTap;
- final GestureTapDownCallback? onTapDown;
- final GestureTapUpCallback? onTapUp;
- final GestureTapCancelCallback? onTapCancel;
- final GestureLongPressCallback? onLongPress;
- final HitTestBehavior? behavior;
- final bool excludeFromSemantics;
- // === 防抖设置 ===
- final Duration debounceDuration;
- // 用于保存最近点击时间(静态,全局防抖)
- static DateTime? _lastTapTime;
- const AuthGestureDetector({
- super.key,
- required this.child,
- this.onTap,
- this.onTapDown,
- this.onTapUp,
- this.onTapCancel,
- this.onLongPress,
- this.behavior,
- this.excludeFromSemantics = false,
- this.debounceDuration = const Duration(milliseconds: 500),
- });
- bool _canTapNow() {
- final now = DateTime.now();
- if (_lastTapTime == null ||
- now.difference(_lastTapTime!) > debounceDuration) {
- _lastTapTime = now;
- return true;
- }
- return false;
- }
- @override
- Widget build(BuildContext context) {
- return GestureDetector(
- behavior: behavior,
- excludeFromSemantics: excludeFromSemantics,
- onTap: () {
- if (!_canTapNow()) return;
- if (uuid.isNotEmpty) {
- onTap?.call();
- } else {
- context.push("/login");
- }
- },
- onTapDown: onTapDown,
- onTapUp: onTapUp,
- onTapCancel: onTapCancel,
- onLongPress: onLongPress,
- child: child,
- );
- }
- }
|