import 'dart:async'; import 'package:flutter/widgets.dart'; /// 短信验证码倒计时 mixin — 抽自 phone_add / old_phone_verify 重复模板。 /// /// 用法(任意 [State] 子类,包含 ConsumerState): /// ```dart /// class _MyPageState extends ConsumerState /// with SmsCountdownMixin { /// /// Future _sendCode() async { /// await repo.sendSms(phone); /// startSmsCountdown(); // 默认 60s /// } /// /// @override /// Widget build(BuildContext context) { /// final text = isSmsCountingDown ? '${smsCountdown}s 后重发' : '发送验证码'; /// ... /// } /// } /// ``` /// /// dispose 时自动 cancel Timer,子类 `super.dispose()` 即可。 mixin SmsCountdownMixin on State { int _countdown = 0; Timer? _timer; /// 当前剩余秒数(0 = 未运行) int get smsCountdown => _countdown; /// 是否正在倒计时 bool get isSmsCountingDown => _countdown > 0; /// 启动倒计时(重复调用会取消上一个 Timer 重新开始)。 void startSmsCountdown({int seconds = 60}) { _timer?.cancel(); setState(() => _countdown = seconds); _timer = Timer.periodic(const Duration(seconds: 1), (t) { if (!mounted) { t.cancel(); return; } setState(() { _countdown -= 1; if (_countdown <= 0) t.cancel(); }); }); } @override void dispose() { _timer?.cancel(); super.dispose(); } }