更新页面
This commit is contained in:
33
lib/controller/time/countdown_controller.dart
Normal file
33
lib/controller/time/countdown_controller.dart
Normal file
@@ -0,0 +1,33 @@
|
||||
import 'dart:async';
|
||||
import 'package:get/get.dart';
|
||||
|
||||
class CountdownController extends GetxController {
|
||||
var countdown = 0.obs;
|
||||
Timer? timer;
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
super.onInit();
|
||||
}
|
||||
|
||||
void startCountdown(int seconds) {
|
||||
timer?.cancel(); // 取消之前的定时器
|
||||
countdown.value = seconds;
|
||||
timer = Timer.periodic(Duration(seconds: 1), (timer) {
|
||||
int elapsed = timer.tick;
|
||||
int remaining = seconds - elapsed;
|
||||
if (remaining > 0) {
|
||||
countdown.value = remaining;
|
||||
} else {
|
||||
countdown.value = 0;
|
||||
timer.cancel();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void onClose() {
|
||||
timer?.cancel();
|
||||
super.onClose();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user