123456789101112131415161718192021222324252627282930313233343536373839
  1. import 'dart:async';
  2. import 'package:farmer_client/utils/timer.dart';
  3. import 'package:flutter/widgets.dart';
  4. import 'package:get/get.dart';
  5. import 'package:flutter_screenutil/flutter_screenutil.dart';
  6. Widget countdown(int sec, void Function() onFinish) {
  7. var _countdown = Rx<int>(sec);
  8. late Timer _timer;
  9. _timer = setInterval((_) {
  10. if (_countdown.value <= 1) {
  11. _timer.cancel();
  12. onFinish();
  13. return;
  14. }
  15. _countdown.value -= 1;
  16. }, 1000);
  17. return Obx(
  18. () => Container(
  19. width: 20.h,
  20. height: 20.h,
  21. alignment: const Alignment(0, 0),
  22. decoration: const BoxDecoration(
  23. borderRadius: BorderRadius.all(Radius.circular(20)),
  24. color: Color(0x77222222)),
  25. child: Text(
  26. _countdown.value.toString(),
  27. style: TextStyle(
  28. fontSize: 13.sp,
  29. letterSpacing: 2,
  30. color: const Color(0xFFFFFFFF),
  31. ),
  32. ),
  33. ),
  34. );
  35. }