send_sms_btn.dart
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
// 发送验证码 按钮
class SendSmsBtn extends StatefulWidget {
final Future<bool> Function()? onTap;
const SendSmsBtn({
super.key,
this.onTap,
});
@override
State<SendSmsBtn> createState() => _SendSmsBtnState();
}
class _SendSmsBtnState extends State<SendSmsBtn> {
int countdown = 60;
Timer? timer;
void sendRegisterMsgCode() {
if (countdown == 60) {
countdown--;
setState(() {});
timer?.cancel();
timer = null;
timer ??= Timer.periodic(const Duration(seconds: 1), (timer) {
countdown--;
if (countdown == 0) {
timer.cancel();
countdown = 60;
}
setState(() {});
});
}
}
@override
void dispose() {
timer?.cancel();
timer = null;
super.dispose();
}
@override
Widget build(BuildContext context) {
return countdown == 60
? InkWell(
onTap: () async {
// AppToast.showLoading();
final s = await widget.onTap?.call() ?? false;
// AppToast.closeAllLoading();
if (s) {
sendRegisterMsgCode();
}
},
child: Container(
width: 123,
height: 43,
alignment: Alignment.centerRight,
child: Text(
"发送验证码",
style: TextStyle(color: Get.theme.primaryColor, fontSize: 14),
),
),
)
: Container(
width: 123,
height: 43,
alignment: Alignment.centerRight,
child: Text(
"$countdown s重新获取",
style: TextStyle(color: Get.theme.primaryColor, fontSize: 14),
),
);
}
}