countdown.dart 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. () => GestureDetector(
  19. onTap: () {
  20. _timer.cancel();
  21. // return;
  22. // onFinish();
  23. },
  24. child: Container(
  25. width: 50.w,
  26. height: 30.h,
  27. alignment: const Alignment(0, 0),
  28. decoration: const BoxDecoration(
  29. borderRadius: BorderRadius.all(Radius.circular(20)),
  30. color: Color(0x77222222)),
  31. child: Text(
  32. _countdown.value.toString() + '跳过',
  33. style: TextStyle(
  34. fontSize: 13.sp,
  35. letterSpacing: 2,
  36. color: const Color(0xFFFFFFFF),
  37. ),
  38. ),
  39. ),
  40. ),
  41. );
  42. }