Flutter 发送短信倒计时按钮
dart
import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
typedef BooleanCallback = Future<bool> Function();
class CountdownButton extends StatefulWidget {
final int seconds;
final BooleanCallback onTap;
final Widget child;
const CountdownButton(
{Key? key, required this.onTap, required this.child, this.seconds = 60})
: super(key: key);
@override
State createState() {
return _CountdownButtonState();
}
}
class _CountdownButtonState extends State<CountdownButton> {
int _countdown = 60;
bool isCountdown = false;
late Timer _timer;
var isLoading = false;
@override
initState() {
super.initState();
_countdown = widget.seconds;
}
start() {
_countdown = 60;
setState(() {
isCountdown = true;
});
//开始倒计时,一秒钟一次
_timer = Timer.periodic(const Duration(seconds: 1), (timer) {
if (_countdown <= 0) {
_timer.cancel();
setState(() {
isCountdown = false;
});
return;
}
_countdown--;
setState(() {});
});
}
@override
void dispose() {
super.dispose();
_timer.cancel();
}
@override
Widget build(BuildContext context) {
return LayoutBuilder(
builder: (context, constraints) {
if (isCountdown) {
return Text(
'sms_retry'.trParams({"seconds": _countdown.toString()}),
style: const TextStyle(color: CupertinoColors.inactiveGray),
);
} else {
return TextButton(
onPressed: isLoading ? null : _onTap,
child: Builder(
builder: (context) {
if (isLoading) {
return Row(
children: [
const CupertinoActivityIndicator(),
const SizedBox(width: 5),
widget.child,
],
);
}
return widget.child;
},
),
);
}
},
);
}
_onTap() async {
setState(() {
isLoading = true;
});
var isSuccess = await widget.onTap();
setState(() {
isLoading = false;
});
if (isSuccess) {
start();
}
}
}
import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
typedef BooleanCallback = Future<bool> Function();
class CountdownButton extends StatefulWidget {
final int seconds;
final BooleanCallback onTap;
final Widget child;
const CountdownButton(
{Key? key, required this.onTap, required this.child, this.seconds = 60})
: super(key: key);
@override
State createState() {
return _CountdownButtonState();
}
}
class _CountdownButtonState extends State<CountdownButton> {
int _countdown = 60;
bool isCountdown = false;
late Timer _timer;
var isLoading = false;
@override
initState() {
super.initState();
_countdown = widget.seconds;
}
start() {
_countdown = 60;
setState(() {
isCountdown = true;
});
//开始倒计时,一秒钟一次
_timer = Timer.periodic(const Duration(seconds: 1), (timer) {
if (_countdown <= 0) {
_timer.cancel();
setState(() {
isCountdown = false;
});
return;
}
_countdown--;
setState(() {});
});
}
@override
void dispose() {
super.dispose();
_timer.cancel();
}
@override
Widget build(BuildContext context) {
return LayoutBuilder(
builder: (context, constraints) {
if (isCountdown) {
return Text(
'sms_retry'.trParams({"seconds": _countdown.toString()}),
style: const TextStyle(color: CupertinoColors.inactiveGray),
);
} else {
return TextButton(
onPressed: isLoading ? null : _onTap,
child: Builder(
builder: (context) {
if (isLoading) {
return Row(
children: [
const CupertinoActivityIndicator(),
const SizedBox(width: 5),
widget.child,
],
);
}
return widget.child;
},
),
);
}
},
);
}
_onTap() async {
setState(() {
isLoading = true;
});
var isSuccess = await widget.onTap();
setState(() {
isLoading = false;
});
if (isSuccess) {
start();
}
}
}