import 'dart:async';

import 'package:farmer_client/utils/timer.dart';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

Widget countdown(int sec, void Function() onFinish) {
  var _countdown = Rx<int>(sec);

  late Timer _timer;
  _timer = setInterval((_) {
    if (_countdown.value <= 1) {
      _timer.cancel();
      onFinish();
      return;
    }
    _countdown.value -= 1;
  }, 1000);

  return Obx(
    () => Container(
      width: 20.h,
      height: 20.h,
      alignment: const Alignment(0, 0),
      decoration: const BoxDecoration(
          borderRadius: BorderRadius.all(Radius.circular(20)),
          color: Color(0x77222222)),
      child: Text(
        _countdown.value.toString(),
        style: TextStyle(
          fontSize: 13.sp,
          letterSpacing: 2,
          color: const Color(0xFFFFFFFF),
        ),
      ),
    ),
  );
}
