// 倒计时
Widget getCountdownStack() {
return Stack(
alignment: Alignment.center,
children: [
Container(
width: 100,
height: 100,
decoration: BoxDecoration(
shape: BoxShape.circle,
border: Border.all(width: 2, color: Colors.blue),
),
),
Positioned(
child: Text(
'${countdownValue}S',
style: const TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
),
),
],
);
}
// 倒计时
void startCountdown() {
const oneSecond = Duration(seconds: 1);
Timer.periodic(oneSecond, (timer) async {
if (countdownValue == 0) {
timer.cancel();
} else {
weiPaiSelectDevice();
if (isConnectSuccess) {
timer.cancel();
}
setState(() {
countdownValue--;
});
}
});
}
页面定义: //倒计时 int countdownValue = 90;
页面初始化中空圆形中心展示倒计时具体值:
@override
void initState() {
//开始倒计时
startCountdown();
}